java中account的用法 java account类 您所在的位置:网站首页 创建一个名为account的类 java中account的用法 java account类

java中account的用法 java account类

2024-05-29 22:17| 来源: 网络整理| 查看: 265

实验 类的继承,super

1、写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;包含的方法:访问器方法(getter和setter方法),返回月利率的方法getMonthlyInterest(),取款方法withdraw(),存款方法deposit()。

java中account的用法 java account类_java中account的用法

写一个用户程序测试Account类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%的Account对象。使用withdraw方法提款30000元,并打印余额。

再使用withdraw方法提款2500元,使用deposit方法存款3000元,然后打印余额和月利率。提示:在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。

运行结果如图所示

java中account的用法 java account类_子类_02

2、创建Account类的一个子类CheckAccount代表可透支的账户,该账户中定义一个属性overdraft代表可透支限额。在CheckAccount类中重写withdraw方法,其算法如下:

如果(取款金额账户余额),

计算需要透支的额度

判断可透支额overdraft是否足够支付本次透支需要,如果可以

将账户余额修改为0,冲减可透支金额

如果不可以

提示用户超过可透支额的限额

要求:写一个用户程序测试CheckAccount类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%,可透支限额为5000元的CheckAccount对象。 使用withdraw方法提款5000元,并打印账户余额和可透支额。 再使用withdraw方法提款18000元,并打印账户余额和可透支额。 再使用withdraw方法提款3000元,并打印账户余额和可透支额。

提示: (1)子类CheckAccount的构造方法需要将从父类继承的3个属性和子类自己的属性全部初始化。 (2)父类Account的属性balance被设置为private,但在子类CheckAccount的withdraw方法中需要修改它的值,因此应修改父类的balance属性,定义其为protected。

运行结果如下图所示:

java中account的用法 java account类_java继承练习_03

java中account的用法 java account类_java中account的用法_04

1.

public class Account { private int id;//账号 protected double balance;//余额 private double annualInterestRate;//年利率 public int getId() { return id; } public void setId(int id) { this.id = id; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public double getAnnualInterestRate() { return annualInterestRate; } public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } public Account(int id, double balance, double annualInterestRate) { super(); this.id = id; this.balance = balance; this.annualInterestRate = annualInterestRate; } public Account() { super(); } //返回月利率的方法 public double getMonthlyInterest(){ double MonInterest=this.annualInterestRate/1200; return MonInterest; } //取款方法 public void withdraw (double amount){ double with=this.balance-amount; if(with=0){ this.balance=with; System.out.println("您的账户余额为:"+balance); } } //存款方法 public void deposit (double amount){ double dep=this.balance+amount; this.balance=dep; System.out.println("您的账户余额为:"+balance); } }public class TestAccount { public static void main(String[] args) { Account ac=new Account(1122, 20000, 4.5); ac.withdraw(30000); ac.withdraw(2500); ac.deposit(3000); System.out.println("您的余额为:"+ac.getBalance()); System.out.println("月利率为:"+ac.getMonthlyInterest()); } }

public class CheckAccount extends Account { private double overdraft; public double getOverdraft() { return overdraft; } public void setOverdraft(double overdraft) { this.overdraft = overdraft; } public CheckAccount(int id, double balance, double annualInterestRate, double overdraft) { super(id, balance, annualInterestRate); this.overdraft = overdraft; } public CheckAccount() { super(); } public void withdraw(double amount) { if (amount < getBalance()) { double am = getBalance() - amount; balance=am; System.out.println("您的账户余额为:" + balance); System.out.println("您的可透支额为:" + getOverdraft()); } if (amount >getBalance()) { if (amount < getBalance() + getOverdraft()) { double Over = getBalance() + getOverdraft() - amount; setBalance(0); overdraft=Over; System.out.println("您的账户余额为:" +getBalance()); System.out.println("您的可透支额为:" + overdraft); } else { System.out.println("超过可透支额!"); } } } }public class TestCheckAccount { public static void main(String[] args) { CheckAccount ch=new CheckAccount(1122, 20000,4.5, 5000); ch.withdraw(5000); ch.withdraw(18000); ch.withdraw(3000); } }



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有