gpt4 book ai didi

java - 为什么代码无法识别我创建的帐户?

转载 作者:行者123 更新时间:2023-12-01 17:15:29 25 4
gpt4 key购买 nike

这是家庭作业的一部分。我正在使用教程来开发银行应用程序。我尽可能地遵循在线代码。我在教程的同一点测试了它。程序员的版本会让他存款。我的不会,而且我看不出我在哪里犯了错误。我使用了 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;
}

最佳答案

此代码Customer customer = new Customer(firstName, lastName, account); 需要帐户。

但是,你只写了Account账号;

帐户变量没有任何内容。所以你的代码被破坏了。

你创建了Account类吗?然后像这样修复你的代码。

原样

账户账户;

future 账户账户 = new Account();

如果您想了解声明,请访问此链接。 reference link 1 reference link 2

关于java - 为什么代码无法识别我创建的帐户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61400167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com