Java实验

假定银行的一个存取款系统有两类客户,一类是现金用户,一类是信用卡用户。银行对每个客户都要登记其姓名name,并为之分配一个唯一的账户号码aid,现金用户还要记录其卡的类型(工资卡、借记卡、理财卡),而信用卡用户则根据其信用级别有一定的透支限额lineOfCredit(A级10000元、B级5000元、C级2000元、D级1000元)。每种客户都可以实现存deposit、取withdraw、和查询余额getBalance,信用卡用户还可以查询透支情况findOverdraw。对于现金用户,每次取款操作只能在账户实际额度balance内操作,允许现金用户改变自己的帐户类型。

  1. 分析有哪些属性和方法可以作为两个子类的共同属性和方法,然后写出抽象类Account的定义。
  2. 分析CashAccount有那些新增的属性和方法,定义一个继承于Account的子类CashAccount。
  3. 分析CreditAccount有那些新增的属性和方法,然后定义一个继承于Account的子类CreditAccount,添加增加的属性和方法。
  4. 请按照要求编写一个程序Test,用你所定义的类完成下列业务操作。
    (1) 用Account作为类型定义两个变量credit和debit,分别引用CreditAccount和CashAccount的对象,并完成存款500元的操作。
    (2) 每个对象完成取款200元的操作后再次取款400元,请输出各自的余额。
    (3) 可以通过credit查看引用对象的透支额吗,如果不能,怎样修改可以查看?

第一题

package Bank;
import java.util.Random;

public abstract class Account {
    Random random = new Random();
    public String name;
    public int aid=random.nextInt(9999);
    public int deposit;
    public int withdraw;
    public int Balance = 0;


    public Account(){
        super();
    }

    public Account(String name, int Balance){
        this.name = name;
        this.aid = random.nextInt(9999);
        this.Balance = Balance;
    }

    public void setName(String name){
        this.name = name;
    }

    public int getAid(){

        return this.aid;
    }

    public void addDeposit(int deposit){
        this.Balance += deposit ;
        System.out.println(Balance);
    }

    public abstract int subWithdraw(int withdraw);

    public int getBalance(){
        return this.Balance;
    }

}

第二题

package Bank;

public class CashAccount extends Account{
    private int card ;
    public void changeCard(int card){

        switch (card){
            case 0:
                this.card = 0;
                System.out.println("工资卡");
                break;
            case 1:
                this.card = 1;
                System.out.println("借记卡");
                break;
            case 2:
                this.card = 2;
                System.out.println("工资卡");
                break;
        }
    }

    public int getCardType() {
        return card;
    }

    public int subWithdraw(int withdraw) {
        if(withdraw > Balance){
            System.out.println("余额不足");
        }
        else if (withdraw < Balance){
            Balance -=withdraw;
            System.out.println(Balance);
        }
        return Balance;
    }

    public CashAccount(String name,int Balance,int card){
        super(name,Balance);
        this.card = card;
    }
}

第三题

package Bank;

public class CreditAccount extends Account{
    public char trustLevel;

    public CreditAccount(String name,int Balance,char trustLevel){
        super(name,Balance);
        this.trustLevel = trustLevel;
    }

    public int subWithdraw(int withdraw){
        if(trustLevel == 'A'&& withdraw<Balance+10000){
            Balance -= withdraw;
            System.out.println(Balance);
        }
        else if(trustLevel == 'B'&& withdraw<Balance+5000){
            Balance -= withdraw;
            System.out.println(Balance);
        }
        else if(trustLevel == 'C'&& withdraw<Balance+2000){
            Balance -= withdraw;
            System.out.println(Balance);
        }
        else if(trustLevel == 'D'&& withdraw<Balance+1000){
            Balance -= withdraw;
            System.out.println(Balance);
        }
        else {
            System.out.println("余额不足");
        }
        return Balance;
    }

    public void gettouzhi(){
        if(trustLevel == 'A'){

            System.out.println(10000);
        }
        else if(trustLevel == 'B'){

            System.out.println(5000);
        }
        else if(trustLevel == 'C'){

            System.out.println(2000);
        }
        else if(trustLevel == 'D'){

            System.out.println(1000);
        }
    }

}


第四题

package Bank;

public class Test {



    public static void main(String[] args) {
        CreditAccount debit = new CreditAccount("伟哥",0,'A');
        CashAccount credit = new CashAccount("爷子哥",0,0);
        System.out.println(credit.getAid() + "\n"+debit.getAid());
        credit.addDeposit(500);
        debit.addDeposit(500);
        credit.subWithdraw(200);
        debit.subWithdraw(200);
        credit.subWithdraw(400);
        debit.subWithdraw(400) ;
        debit.gettouzhi();



    }
}

帮一个百万博主引流,他做的比我牛逼,他的编程能力甚至连我都要暂避锋芒。
再乐

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值