【Java 基础实例—Bank 项目1】

bank要求

(上图Wie任务要求的UML结构)

Account.java 文件:

 1 package Banking_1;
 2 
 3 public class Account {
 4     private double balance;//余额  ,uml前该变量是 '-'
 5     public Account(double init_balance){
 6         balance=init_balance;
 7     }
 8     public double getBalance() {
 9         return balance;
10     }
11     //存钱
12     public void deposit(double amt){
13         this.balance+=amt;
14     }
15     //取钱
16     public void withdraw(double amt){
17         this.balance-=amt;
18     }
19 
20 }
View Code

再写一个测试类:

TestBanking.java的文件:

 1 package Banking_1;
 2 /*
 3  * This class creates the program to test the banking classes.
 4  * It creates a new Bank, sets the Customer (with an initial balance),
 5  * and performs a series of transactions with the Account object.
 6  */
 7 
 8 public class TestBanking {
 9 
10     public static void main(String[] args) {
11 
12 
13         // Create an account that can has a 500.00 balance.
14         System.out.println("Creating an account with a 500.00 balance.");
15         Account  account=new Account(500.00);
16         //code
17         System.out.println("Withdraw 150.00");
18         account.withdraw(150);
19 
20         System.out.println("Deposit 22.50");
21         account.deposit(22.5);
22         //code
23         System.out.println("Withdraw 47.62");
24         account.withdraw(47.62);
25         //code
26         // Print out the final account balance
27         System.out.println("The account has a balance of " + account.getBalance());
28     }
29 }
View Code

运行结果:

 

 

转载于:https://www.cnblogs.com/zhazhaacmer/p/9762560.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值