将名称和帐户分配给银行帐户项目

时间:2014-11-06 21:20:37

标签: java

我几乎在这个项目的主场,并且已经遇到了难以逾越的减速带。我的主要和下面的方法看起来像这样:

import java.util.Scanner;
public class Proj3
{
    public static void main (String []args)
    { 
        BankAccount cust1 = new Proj3().createAccount();
        BankAccount cust2 = new Proj3().createAccount();
        BankAccount cust3 = new Proj3().createAccount();
        
             
    }
    
    public BankAccount createAccount() 
    {
        Scanner kb = new Scanner (System.in);   //input for Strings
        Scanner kb2 = new Scanner (System.in);  //input for numbers
        String strName;                         //Holds account name
        String strAccount;                      //Holds account number
        String strResponse;                     //Holds users response to account creation
        double dDeposit;                        //Holds intial deposit into checking
        BankAccount b1;
        
       
        
       
        {
            System.out.print ("\nWhat is the name of the account? ");
            strName = kb.nextLine();
            b1.setName(strName); 
            while (strName.length()==0)
            {
                System.out.print ("\nPlease input valid name.");

                System.out.print ("\nWhat is the name of the account?");
                strName = kb.nextLine();
            }

        System.out.print ("Would you like to create this account? (Y or N)");
        strResponse = kb.nextLine();
        strResponse = strResponse.toUpperCase();

        if (strResponse.equals("Y"))
        {
            BankAccount b1 = new BankAccount(strName, strAccount, dDeposit);
        }
        else 
        {
            b1 = null;
        }

我有一个BankAccount类,看起来像这样:

import java.util.Scanner;

public class BankAccount
{
   private String name;                       //Holds name of customer
   private String account;                    //Holds account number of 
   private double checkingAccount;            //Holds balance for checking
   private double savingsInterestRate;        //Holds annual interest rate on savings
   private double savingsAccount;             //Holds balance for savings
   private int savingsDays;                   //Holds days for calc. savings interest
   private double totalEarnedInterest;        //Holds total interest earned
   
   public BankAccount (String name, String account, double amount)
   {
       setName(name);
       setAccount(account);
       setChecking(amount);
   }
   
   public void setName (String name)
   {
       this.name = name;
   }

如何将所有用户输入的信息传递到我的BankAccount类中的相应方法?最后,我想使用toString方法在屏幕上显示所有信息。

我知道问题是b1.setName(strName)因为我没有初始化b1,但我该如何去做呢?任何有关此事的帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

在创建对象之前,您正在调用BankAcound的setName方法。由于您要将strName传递给构造函数,因此不必调用setName。

答案 1 :(得分:0)

最简单的方法是将参数传递给BankAccount构造函数并分配成员变量。您不需要在构造函数中调用setter方法。

public BankAccount(String name, String account, double amount) {
    this.name = name;
    this.account = account;
    this.amount = amount;
}

如果您的参数很少,这种方式就可以,但如果您想将所有银行帐户属性传递给参数列表中的构造函数,代码将变得丑陋且难以理解。如果它们属于同一类型,则很容易错误地交换调用参数。

更优雅的方法是使用流畅的界面构建器模式as described here。然后创建一个银行帐户对象将如下所示:

BankAccount b1 = BankAccountBuilder.newBankAccount
            .witName(strName)
            .withAccountNumber(strAccount)
            .havingAmount(dDeposit)
            .withSavingDays(days)
            .build();