Java黑皮书课后题第10章:**10.7(游戏:ATM机)使用编程练习题9.7中创建的Account类来模拟一台ATM级
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第10章:**10.7(游戏:ATM机)使用编程练习题9.7中创建的Account类来模拟一台ATM级
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
**10.7(游戲:ATM機)使用編程練習題9.7中創建的Account類來模擬一臺ATM級
- 題目
- 程序
- 代碼
- Test7.java
- Test7_Account.java
- 運行示例
題目
程序
Test7.java:測試程序
Test7_Account.java:構造程序
點擊這里查看Account類所在的編程練習題9.7
代碼
Test7.java
import java.util.Scanner;public class Test7 {public static void main(String[] args) {// 新建Test7_Account數數組Test7_Account[] arr = new Test7_Account[10];for (int i = 0 ; i < 10 ; i++){arr[i] = new Test7_Account(i, 100);}// 相關操作Scanner input = new Scanner(System.in);int id;while (true){System.out.print("\nEnter an id: ");id = input.nextInt();if (id < 0 || id > 10){System.out.println("輸入正確id");continue;}boolean bool = true;while (bool){System.out.println("\nMain menu");System.out.println("1: check balance\n2: withdraw\n3: deposit\n4: exit");System.out.print("Enter a choice: ");int choice = input.nextInt();if (choice == 1) {System.out.printf("The balance is %.1f\n", arr[id].getBalance());}if (choice == 2) {System.out.print("Enter an amount to withdraw: ");double withdraw = input.nextDouble();arr[id].withDraw(withdraw);}if (choice == 3){System.out.print("Enter an amount to deposit: ");double deposit = input.nextDouble();arr[id].deposit(deposit);}if (choice == 4){bool = false;}}}} }Test7_Account.java
public class Test7_Account {// 四個私有數據域private int id = 0;private double balance = 0.0;private double annualInterestRate = 0.0;private Date dateCreated;// 無參構造方法public Test7_Account(){}// 有參構造方法public Test7_Account(int id, double balance){this.id = id;this.balance = balance;}// id balance annualInterestRate的setter和getterpublic 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;}// dateCreated的訪問器方法public Date getDateCreated(){return dateCreated;}// getMonthlyInterestRate方法public double getMonthlyInterestRate(){return annualInterestRate / 1200;}// getMonthlyInterest方法public double getMonthlyInterest(){return annualInterestRate * balance / 1200;}// withDraw方法public void withDraw(double num){if (num <= balance) balance -= num;}// deposit方法public void deposit(double num){balance += num;} }運行示例
Enter an id: 4Main menu 1: check balance 2: withdraw 3: deposit 4: exit Enter a choice: 1 The balance is 100.0Main menu 1: check balance 2: withdraw 3: deposit 4: exit Enter a choice: 2 Enter an amount to withdraw: 3Main menu 1: check balance 2: withdraw 3: deposit 4: exit Enter a choice: 1 The balance is 97.0Main menu 1: check balance 2: withdraw 3: deposit 4: exit Enter a choice: 3 Enter an amount to deposit: 10Main menu 1: check balance 2: withdraw 3: deposit 4: exit Enter a choice: 1 The balance is 107.0Main menu 1: check balance 2: withdraw 3: deposit 4: exit Enter a choice: 4Enter an id:總結
以上是生活随笔為你收集整理的Java黑皮书课后题第10章:**10.7(游戏:ATM机)使用编程练习题9.7中创建的Account类来模拟一台ATM级的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第10章:*10.6
- 下一篇: java美元兑换,(Java实现) 美元