定义一个人类:字段:name姓名 zh账号 password密码 money钱数要求:每一个字段必须加访问控制 和setset getter方法要求:再写一个存钱的方法:存5000 元输入密码必须和password一致 你输入的账户必须和zh一致限制1:密码必须是6位数字 错误的密码输入不得超过3次否则吞卡
人类的定义如下:
public class Person {
private String name;
private String zh;
private String password;
private double money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getZh() {
return zh;
}
public void setZh(String zh) {
this.zh = zh;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
if (password.length() == 6 && password.matches("\\d+")) {
this.password = password;
} else {
throw new IllegalArgumentException("密码必须是6位数字");
}
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public void deposit(double amount, String inputZh, String inputPassword) {
if (inputZh.equals(this.zh) && inputPassword.equals(this.password)) {
this.money += amount;
} else {
throw new IllegalArgumentException("账户或密码错误");
}
}
}
使用方法:
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.setName("张三");
person.setZh("123456");
person.setPassword("123456");
person.setMoney(1000.0);
try {
person.deposit(5000.0, "123456", "123456");
System.out.println("存钱成功");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
``
原文地址: https://www.cveoy.top/t/topic/ibcL 著作权归作者所有。请勿转载和采集!