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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么代码无法识别我创建的帐户?相关的知识,希望对你有一定的参考价值。

这是家庭作业的一部分。我正在使用教程来处理银行应用程序。我尽可能地在线跟踪代码。我在本教程的同一点对其进行了测试。程序员的版本可以让他存款。我不会,而且我看不到我在哪里犯错。我使用了日食给我的建议,但是它们没有用。我尝试过的那行是应该初始化帐户帐户,但这没有用。它一直告诉我这是一个无效的选择。再次,我遵循了代码,甚至在较早的版本上也进行了工作。

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 ammount ");
    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;

帐户变量没有任何内容。因此您的代码已损坏。

您是否参加过Account课?然后像这样修复您的代码。

AS-IS

Account account;

TO-BEAccount account = new Account();

并且如果您想了解declaration,请访问此链接。reference link 1reference link 2

以上是关于为什么代码无法识别我创建的帐户?的主要内容,如果未能解决你的问题,请参考以下文章

私人以太坊网络无法识别发件人帐户

如何从片段中调用 getSupportFragmentManager()?

无法创建 iOS 代码签名证书

无法通过使用 paypal 沙箱创建的个人测试 paypal 帐户测试付款(在 paypal.com 公共站点上似乎也无法识别电子邮件)

无法在片段内创建对话框

如何为没有附加外部通信方法的帐户创建密码重设系统?