JAva言語プログラム設計第8版練習問題10.7 ATM
3736 ワード
AccountクラスでATMをシミュレート
Accountクラスソース:
ATM類ソース:
問題:
1.private static ArrayList accounts= new ArrayList(); 最初に書いたのはprivate static ArrayList accountsです.そして、コンパイラはException in thread"main"javaを間違えました.lang.NullPointerExceptionat object10.ATM.main(ATM.java:53)arrayListを初期化していないため2.case 4:isLogin = false;break; isLoginをfalseに設定しておくと、処理メニューから入力idにジャンプできます.
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 accounts = new ArrayList();
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 accounts= new ArrayList(); 最初に書いたのはprivate static ArrayList accountsです.そして、コンパイラはException in thread"main"javaを間違えました.lang.NullPointerExceptionat object10.ATM.main(ATM.java:53)arrayListを初期化していないため2.case 4:isLogin = false;break; isLoginをfalseに設定しておくと、処理メニューから入力idにジャンプできます.