public class ATM {
public void use(Account account){//在测试类里调用,传进来参数哪个卡就操作哪个卡
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入操作编号:");
int num = sc.nextInt();
if (num == 1) {
System.out.println("请输入存款金额:");
account.deposit(sc.nextDouble());
System.out.println("您当前的余额为:" + account.getBalance());
} else if (num == 2) {
System.out.println("请输入取款金额:");
account.withdraw(sc.nextDouble());
System.out.println("您当前的余额为:" + account.getBalance());
} else if (num == 3) {
if (account instanceof CheckingAccount){
CheckingAccount c = (CheckingAccount)account;
System.out.println("您当前的余额为:" + account.getBalance() + "额度为:" + c.protect);
}else if (account instanceof SavingsAccount){
SavingsAccount s = (SavingsAccount)account;//向下转型,不然不能调用子类自己的属性和方法
System.out.println("您当前的余额为:" + account.getBalance() + "年利率为:" + s.getYearPct());
}
}
else {
System.out.println("退出成功!");
break;
}
}
}
public void menu(){
System.out.println("请选择操作");
System.out.println("--------ATM存取款机--------");
System.out.println("--------1-存款------------");
System.out.println("--------2-取款------------");
System.out.println("--------3-查看余额---------");
System.out.println("--------4-退出系统---------");
}
}
public class Person {
private String name;
private int tel;
private Account account;//包含关系
public Person(){
}
public Person(String name, int tel) {
this.name = name;
this.tel = tel;
}
public Person(String name, int tel, Account account) {
this.name = name;
this.tel = tel;
this.account = account;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTel() {
return tel;
}
public void setTel(int tel) {
this.tel = tel;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
public class TextPerson {
public static void main(String[] args) {
Person p = new Person("张三", 123456);
ATM at = new ATM();
System.out.println("用户您好,欢迎使用ATM机");
System.out.println("请选择卡类型 1.信用卡ca,2.储蓄卡sa");
Scanner sc = new Scanner(System.in);
int opr = sc.nextInt();
if (opr == 1){
p.setAccount(new CheckingAccount(5000,1000));//Account 和 person是包含关系,所以person里有Account属性,
System.out.println("当前余额为 :"+ p.getAccount().getBalance());//建立构造方法,直接可以传参,传的参 其实就是多态的体现,可以传不同的银行卡,所以实现不同账户
at.menu();
at.use(p.getAccount());//获取到传进来的卡片
}else if (opr == 2){
p.setAccount(new SavingsAccount(6000,0.025));
System.out.println("当前余额为:" + p.getAccount().getBalance());
at.menu();
at.use(p.getAccount());
}
}