[英]Why doesn't the code recognize the accounts I've created?
這是家庭作業的一部分。 我正在使用教程來開發銀行應用程序。 我盡可能地遵循在線代碼。 我在教程的同一點測試了它。 程序員的版本會讓他存錢。 我的不會,而且我看不出我在哪里犯了錯誤。 我使用了 eclipse 給我的建議,但它們沒有用。 我嘗試過應該初始化account
的行,但它沒有用。 它一直告訴我這是一個無效的選擇。 我再次遵循了代碼,甚至讓它在早期的構建中工作。
import java.util.ArrayList;
import java.util.Scanner;
public class Menu {
Scanner keyboard = new Scanner(System.in);
Bank bank = new Bank();
boolean exit;
public static void main(String[] args) {
Menu menu = new Menu();
menu.runMenu();
}
public void runMenu() {
printHeader();
while(!exit) {
printMenu();
int choice = getInput();
performAction(choice);
}
}
private void printHeader() {
System.out.println("******");
System.out.println("My Bank");
System.out.println("Java Bank App");
System.out.println("******");
}
private void printMenu() {
System.out.println("Please Select an Option");
System.out.println("1. Open an Account");
System.out.println("2. Close an Account");
System.out.println("3. Make a Deposit");
System.out.println("4. Make a Withdrae");
System.out.println("5. Check Balance");
System.out.println("6. Check Interest");
System.out.println("7. Log on as Administrator");
System.out.println("0. Exit");
}
private int getInput() {
int choice = -1;
do {
System.out.println("Please enter Your Choice");
try {
choice = Integer.parseInt(keyboard.nextLine());
}
catch(NumberFormatException e) {
System.out.println("Invalid Selection");
}
if (choice <0 || choice > 7 ) {
System.out.println("Please make choice from Menu");
}
}while (choice <0 || choice > 7 );
return choice;
}
private void performAction(int choice) {
switch(choice) {
case 0:
System.out.println("Thank You for using our App: ");
System.exit(0);
break;
case 1:
createAccount();
break;
case 2:
closeAccount();
break;
case 3:
makeDeposit();
break;
case 4:
makeWithDrawal();
break;
case 5:
listBalance();
break;
case 6:
checkInterest();
break;
case 7:
logAdmin();
break;
default:
System.out.println("Error has Occured");
}
}
private void createAccount() {
String firstName, lastName, accountType = "";
double intialDeposit = 0;
boolean valid = false;
while (!valid) {
System.out.print("Please Enter Account Type you wish to open: ");
accountType = keyboard.nextLine();
if(accountType.equalsIgnoreCase("checking")|| accountType.equalsIgnoreCase("savings") ||
accountType.equalsIgnoreCase("CDSavings") ) {
valid = true;
}
else {
System.out.println("Please re-enter account type");
}
}
System.out.println("Please Enter Your Frist Name ");
firstName = keyboard.nextLine();
System.out.println("Please Enter Your Last Name ");
lastName = keyboard.nextLine();
valid = false;
while(!valid ) {
System.out.println("Please Enter an inital amount ");
try {
intialDeposit = Double.parseDouble(keyboard.nextLine());
}
catch(NumberFormatException e ) {
System.out.println("Please Enter anumerical value ");
}
if(accountType.equalsIgnoreCase("checking")) {
if (intialDeposit < 100) {
System.out.println("Intial Deposit must be $100 or more ");
} else {
valid = true;
}
}
else if(accountType.equalsIgnoreCase("savings")) {
if (intialDeposit < 100) {
System.out.println("Intial Deposit must be $100 or more ");
} else {
valid = true;
}
}
else if(accountType.equalsIgnoreCase("CD Savings")) {
if (intialDeposit < 100) {
System.out.println("Intial Deposit must be $100 or more ");
} else {
valid = true;
}
}
}
Account account;
if (accountType.equalsIgnoreCase("checking")) {
if (intialDeposit < 100) {
System.out.print("Must be a min of $100");
}
else {
valid = true;
}
}
else if (accountType.equalsIgnoreCase("savings")) {
if (intialDeposit < 100) {
System.out.print("Must be a min of $100");
}
else {
valid = true;
}
}
else if (accountType.equalsIgnoreCase("cd")) {
if (intialDeposit < 100) {
System.out.print("Must be a min of $100");
}
else {
valid = true;
}
Customer customer = new Customer(firstName, lastName, account);
bank.addCustomer(customer);
}
}
private void closeAccount() {
}
private void makeDeposit() {
int account = selectAccount();
if(account >=0) {
System.out.print("Add Amount to Deposit");
double amount = 0;
try {
amount = Double.parseDouble(keyboard.nextLine());
}
catch(NumberFormatException e) {
amount = 0;
}
bank.getCustomer(account).getAccount().deposit(amount);
}
}private int selectAccount() {
ArrayList<Customer>customers = bank.getCustomers();
if(customers.size() <= 0) {
System.out.println ("Please create account");
return -1;
}
System.out.println ("Select an account: ");
for (int i = 0; i < customers.size(); i++) {
System.out.println((i +1) + " " + customers.get(i).basicInfo());
}
int account = 0;
System.out.print("Please Enter your selection");
try {
account = Integer.parseInt(keyboard.nextLine()) -13;
}
catch (NumberFormatException e) {
account = 0;
}
if(account <=0 || account > customers.size()) {
System.out.println("Invalid");
account = 0;
}
return account;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.