JAVA高级程序设计求解
时间有限我先把题一帮你解了,晚上在帮你解题二。题一代码如下,望采纳!
共三个java文件测试截图也在后面!
//定义User类
public class User {
private String username;
private String password;
//定义默认构造方法
public User() {
this.username = "admin";
this.password = "1234";
}
public static User checkUser(String username, String password) throws NoSuchUserException, PasswordDontMatchException {
User user = new User();
if (!user.username.equals(username)) {
throw new NoSuchUserException("用户名不匹配!");
}
if (!user.password.equals(password)) {
throw new PasswordDontMatchException("密码不匹配!");
}
return user;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
//自定义异常
public class NoSuchUserException extends Exception {
public NoSuchUserException(String msg) {
super(msg);
}
}
public class PasswordDontMatchException extends Exception {
public PasswordDontMatchException(String msg) {
super(msg);
}
}
/**
* 2-1
*/
public class ShiXun1 {
}
class User {
private String username;
private String password;
public User() {
this.username = "admin";
this.password = "1234";
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public void checkUser(String username, String password)
throws NoSuchUserException, PasswordDontMatchException {
if (username == null || !username.equals(this.username)) {
throw new NoSuchUserException();
}
if (password == null || !password.equals(this.password)) {
throw new PasswordDontMatchException();
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
class NoSuchUserException extends Exception {
private static final long serialVersionUID = 1258383727357385168L;
public NoSuchUserException() {
super("用户名错误!");
}
}
class PasswordDontMatchException extends Exception {
private static final long serialVersionUID = -1436055365129526177L;
public PasswordDontMatchException() {
super("密码错误!");
}
}
/**
* 2-2
*/
public class ShiXun2 {
}
class Account {
private double balance;
public boolean saveMoney(double money) throws InvalidDepositException {
if (money > balance) {
throw new InvalidDepositException();
}
balance += money;
return true;
}
public boolean drawMoney(double money) throws AccountOverdrawnException {
if (money > balance) {
throw new AccountOverdrawnException();
}
balance -= money;
return true;
}
public double checkBalance() {
return balance;
}
}
class AccountOverdrawnException extends Exception {
private static final long serialVersionUID = -2676581895309749293L;
public AccountOverdrawnException() {
super("余额不足!");
}
}
class InvalidDepositException extends Exception {
private static final long serialVersionUID = -5014651032942602026L;
public InvalidDepositException() {
super("存款金额错误!");
}
}