java 面向对象封装继承多态实现简单银行卡存取功能

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());
        }

    }

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值