2013-12-07 68 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