Java学习之Account类

清疚 2022-11-17 10:16 354阅读 0赞

案例介绍:

定义一个名为Account的类实现账户管理,它的UML图如下图所示:
在这里插入图片描述

编写一个应用程序测试Account的类的使用。

案例代码:

  1. import java.util.Date;
  2. public class Account{
  3. private int id;
  4. private double balance;
  5. private double annulInterestRate;
  6. private Date dateCreated;
  7. public Account(){
  8. super();
  9. }
  10. public Account(int id, double balance){
  11. super();
  12. this.id = id;
  13. this.balance = balance;
  14. dateCreated = new Date();
  15. }
  16. public int getId(){
  17. return id;
  18. }
  19. public void setId(int id){
  20. this.id = id;
  21. }
  22. public double getBalance(){
  23. return balance;
  24. }
  25. public void setBalance(double balance){
  26. this. balance = balance;
  27. }
  28. public double getAnnulInterestRate(){
  29. return annulInterestRate;
  30. }
  31. public void setAnnulInterestRate(double annulInterestRate){
  32. this.annulInterestRate = annulInterestRate;
  33. }
  34. public Date getDateCreated(){
  35. return dateCreated;
  36. }
  37. public void withdraw(double amount){
  38. balance = balance - amount;
  39. }
  40. public void deposit(double amount){
  41. balance = balance + amount;
  42. }
  43. public static void main( String[ ] args){
  44. Account myAccount = new Account( 101,1000.00);
  45. myAccount.deposit( 100);
  46. myAccount.withdraw( 200) ;
  47. System.out.println("The balance = " + myAccount.getBalance());
  48. }
  49. }

案例运行图:
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,354人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java学习Account

    案例介绍: 定义一个名为Account的类实现账户管理,它的UML图如下图所示: ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVp

    相关 java学习匿名内部

    匿名内部类:就是内部类的简写格式。 前提: 内部类必须继承或者实现一个外部类或者接口 下图中5-11行定义了一个匿名内部类,该类是Object类的一个子类