2013-12-07 92 views
0

我有以下項目類:如何使用用戶輸入創建銀行賬戶?

  • 帳戶
  • 客戶
  • 名稱:FNAME和LNAME(均爲字符串字段)
  • 日期:年,月,日(均爲int字段)
  • 銀行:包含賬戶的集合
  • InputReader:從鍵盤讀取輸入

一個Account對象需要一個Customer對象和一個期初餘額。 Customer對象需要一個Name對象和一個Date對象。 名稱對象需要字符串的名字和姓氏 我需要向用戶詢問詳細信息以創建名稱和日期對象,以及期初餘額。

我必須通過從用戶那裏獲取相關信息來創建一個新帳戶,即它要求用戶輸入客戶的姓名,出生日期等。它讀入用戶響應,創建帳戶並添加它去銀行。

當我運行public void createNewAccount()方法時,我不斷收到一條錯誤消息,指出「java.lang.NullPointerException」。將不勝感激任何幫助。提前致謝。

下面是我的銀行類的源代碼。

import java.util.ArrayList; 

public class Bank 
{ 
    public static final double INTEREST_RATE = 0.012;//1.2% 
    // instance variables - replace the example below with your own 
    private ArrayList<Account> accounts; 
    private InputReader reader; 
    private Name fullName; 
    private Date dateOfBirth; 

    /** 
    * Constructor for objects of class Bank 
    */ 
    public Bank() 
    { 
     // initialise instance variables 

    } 

    /* 
    * Adds an existing Account to the bank 
    * @param account 
    */ 
    public void addAccount(Account account) 
    { 
     accounts.add(account); 
    } 

     public void createNewAccount() { 

     System.out.println("Please enter your first name: "); 
     String firstName = reader.readString(); 

     System.out.println("Hello " + firstName + ". " + "What is your last name?"); 
     String lastName = reader.readString(); 
     System.out.println("Your last name is " + lastName); 


     System.out.println("Please enter your year of birth: "); 
     int thisYear = reader.readInt(); 

     System.out.println("Please enter your month of birth: "); 
     int thisMonth = reader.readInt(); 

     System.out.println("Please enter your date of birth: "); 
     int thisDay = reader.readInt(); 

     Name theName = new Name(firstName, lastName); 
     Date theDateOfBirth = new Date(thisYear, thisMonth, thisDay); 

    } 


} 
+2

'reader'在初始化之前使用。首先完成代碼的初始化部分。 – 4J41

回答

0

您可以使用掃描儀更換閱讀器。類似的東西。

Scanner scan = new Scanner(System.in); 
    System.out.println("Please enter your first name: "); 
    String firstName = scan.next(); 

    System.out.println("Hello " + firstName + ". " + "What is your last name?"); 
    String lastName = scan.next(); 
    System.out.println("Your last name is " + lastName); 


    System.out.println("Please enter your year of birth: "); 
    int thisYear = scan.nextInt(); 

    System.out.println("Please enter your month of birth: "); 
    int thisMonth = scan.nextInt(); 

    System.out.println("Please enter your date of birth: "); 
    int thisDay = scan.nextInt(); 

    //Then do what you are supposed to do...... 
+0

感謝您的信息!我嘗試了另一種可行的方法,但你的方法也很有道理。 – user3078337

1

您必須先初始化閱讀器,然後才能嘗試讀取它。

+0

感謝您的快速回復! – user3078337

0
Scanner scanner = new Scanner(System.in); 

The scanner object has a lot of functions you can use; 
0

您的InputReader讀取器已聲明,但未初始化,因此爲NullPointerExceptin。將其更改爲較常用的:

Scanner scanner = new Scanner(System.in); 

然後你可以使用它的用戶輸入方法:

scanner.nextLine() for strings 

scanner.nextInt() for ints 

scanner.nextDouble() for doubles 

檢查文檔here

0

需要初始化您的實例變量:

public class Bank 
{ 
    public static final double INTEREST_RATE = 0.012; 

    private ArrayList<Account> accounts; 
    private InputReader reader; 
    private Name fullName; 
    private Date dateOfBirth; 

    public Bank() 
    { 
     // initialise instance variables <- where it says to 
     accounts = new ArrayList<Account>(); 
     reader = new InputReader(); 
     ... 
    } 

InputReader必須是任務包的一部分,所以我不知道它的構造是正確的。

+0

感謝您的幫助。現在它工作!:) – user3078337