(上图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 }
再写一个测试类:
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 }
运行结果: