java語言程式設計第八版 習題10.7 ATM機
阿新 • • 發佈:2019-02-04
用Account類來模擬一臺ATM機
Account類原始碼:
import java.util.Date; public class Account { private int id = 0; private double balance = 0; private static double annualInterestRate = 0; private Date dateCreated; 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 Date getDateCreated() { return dateCreated; } public Account() { } public Account(int id, double balance) { this.id = id; this.balance = balance; } public static double getMonthlyInterestRate() { return annualInterestRate / 12; } public void withDraw(double money) { balance -= money; } public void deposit(double money) { balance += money; } /** * @param args */ // public static void main(String[] args) { // // TODO Auto-generated method stub // Account account = new Account(1122, 20000); // account.dateCreated = new Date(); // account.setAnnualInterestRate(4.5); // account.withDraw(2500); // account.deposit(3000); // System.out.println("餘額為:" + account.getBalance()); // System.out.println("月利息為" + getMonthlyInterestRate()); // System.out.println("開戶日期:" + account.getDateCreated()); // } }
ATM類原始碼:
package object10; import java.util.*; public class ATM { private static ArrayList<Account> accounts = new ArrayList<Account>(); private static int numList[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; private static Scanner scanner; private static boolean isLogin = false; public static void showMenu() { System.out.println("Main menu"); System.out.println("1.check balance"); System.out.println("2.withdraw"); System.out.println("3.deposit"); System.out.println("4.exit"); System.out.print("Enter a choice:"); } public static void processMenu(int id, int command) { switch (command) { case 1: System.out.println("" + accounts.get(id).getBalance()); break; case 2: System.out.println("Enter an amount to withdraw:"); double withdrawMoney = scanner.nextDouble(); accounts.get(id).withDraw(withdrawMoney); break; case 3: System.out.println("Enter an amount to deposit"); double depositMoney = scanner.nextDouble(); accounts.get(id).deposit(depositMoney); break; case 4: isLogin = false; break; default: System.out.println("請輸入正確的指令:"); break; } } public static void main(String[] args) throws Exception { // TODO Auto-generated method stub for (int i = 0; i <= 9; i++) { Account acc = new Account(i, 100); accounts.add(acc); } while (true) { System.out.println("請輸入ID:"); scanner = new Scanner(System.in); int id = scanner.nextInt(); for (int num : numList) { if (id == num) { isLogin = true; } } if (!isLogin) { System.out.println("請輸入正確的id!"); } while (isLogin) { showMenu(); scanner = new Scanner(System.in); int command = scanner.nextInt(); processMenu(id, command); } } } }
出現過的問題:
1.private static ArrayList<Account> accounts= new ArrayList<Account>();
剛開始寫的是
private static ArrayList<Account> accounts;
然後編譯器報錯Exception in thread "main" java.lang.NullPointerException
at object10.ATM.main(ATM.java:53)因為沒有對arrayList進行初始化
2.
case 4:
isLogin = false;
break;
要記得把isLogin設為false,這樣才可以從處理選單跳出到輸入id