我有以下项目类:如何使用用户输入创建银行账户?
- 帐户
- 客户
- 名称: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);
}
}
'reader'在初始化之前使用。首先完成代码的初始化部分。 – 4J41