【问题标题】:How to create a bank account with user input?如何使用用户输入创建银行帐户?
【发布时间】:2013-12-07 19:40:15
【问题描述】:

我在项目中有以下课程:

  • 帐号
  • 客户
  • 名称:fName 和 lName(都是字符串字段)
  • 日期:年、月、日(都是 int 字段)
  • 银行:包含帐户集合
  • InputReader:从键盘读取输入

Account 对象需要 Customer 对象和期初余额。 Customer 对象需要一个 Name 对象和一个 Date 对象。 Name 对象需要名字和姓氏的字符串 我需要向用户询问创建 Name 和 Date 对象的详细信息,以及期初余额。

我必须通过从用户那里获取相关信息来创建一个新帐户,即它要求用户输入客户的姓名、出生日期等。它会读取用户的回复,创建帐户并将其添加到银行。

当我运行 public void createNewAccount() 方法时,我不断收到一条错误消息“java.lang.NullPointerException”。将不胜感激任何帮助。提前致谢。

下面是我的 Bank 类的源代码。

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 在初始化之前使用。首先完成代码的初始化部分。

标签: java


【解决方案1】:

您必须先初始化阅读器,然后才能尝试从中读取。

【讨论】:

    【解决方案2】:

    您可以使用扫描仪更改阅读器。类似的东西。

        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......
    

    【讨论】:

    • 感谢您的信息!我尝试了另一种可行的方法,但您的方法也很有意义。
    【解决方案3】:
    Scanner scanner = new Scanner(System.in);
    
    The scanner object has a lot of functions you can use;
    

    【讨论】:

      【解决方案4】:

      您的 InputReader 阅读器已声明,但未初始化,因此为 NullPointerExceptin。改成更常用的:

      Scanner scanner = new Scanner(System.in);
      

      然后您可以使用它的方法进行用户输入:

      scanner.nextLine() for strings
      
      scanner.nextInt() for ints
      
      scanner.nextDouble() for doubles
      

      等等

      查看文档here

      【讨论】:

        【解决方案5】:

        你需要初始化你的实例变量:

        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 必须是赋值包的一部分,所以我不知道它的构造函数到底是什么。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-04-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-29
          • 1970-01-01
          相关资源
          最近更新 更多