面向物件章節練習(實現使用者和密碼一致)
public class User{
// 成員屬性
private String user;
private String code;
// 帶參構造方法
public User(String user, String code) {
this.user = user;
this.code = code;
}
// 宣告get/set方法
public String getuser() {
return user;
}
public void setuser(String user) {
this.user = user;
}
public String getcode() {
return code;
}
public void setcode(String code) {
this.code = code;
}
}
public class UserManager{
import com.object.project.User;
public String checkUser(User one, User two) {
if (!one.getuser().equals(two.getuser())) {
System.out.println("使用者名稱:" + one.getuser() + "\n" + "密碼:" + one.getcode());
System.out.println("使用者名稱:" + two.getuser() + "\n" + "密碼:" + two.getcode());
System.out.println("======================");
return " 使用者名稱不一致!";
} else if (!one.getcode().equals(two.getcode())) {
System.out.println("使用者名稱:" + one.getuser() + "\n" + "密碼:" + one.getcode());
System.out.println("使用者名稱:" + two.getuser() + "\n" + "密碼:" + two.getcode());
System.out.println("======================");
return " 密碼不一致!";
} else {
System.out.println("使用者名稱:" + one.getuser() + "\n" + "密碼:" + one.getcode());
System.out.println("使用者名稱:" + two.getuser() + "\n" + "密碼:" + two.getcode());
System.out.println("======================");
}
return " 使用者名稱和密碼驗證通過!";
}
}
public class Test{
import com.object.project.User;import com.object.project.UserManager;
public static void main(String[] args) {
User x = new User("Reggy", "123456");
User y = new User("Reggy", "123");
UserManager z = new UserManager();
System.out.println(z.checkUser(x, y));
}
}