用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