2012-05-10 76 views
1

我想在for循環中從一個類創建幾個對象。但我不知道如何編碼。我寫的內容創建了一個新對象,但它覆蓋了前一個對象。在Java for循環中創建幾個新對象

package assginment1_version4; 

import java.util.*; 

public class Client { 

public static void main (String[] args) { 
    System.out.println ("this is a bill database"); 
    System.out.println ("add a user?(Y/N)"); 

    Scanner input = new Scanner(System.in); 
    String answer = input.nextLine(); 
    ArrayList ary = new ArrayList(); 

    for (int i=1 ; i < 100; i++) { 
     if (answer.equalsIgnoreCase("y")) { 
      Bill bill1 = new Bill(); 
      System.out.println("user first name:"); 
      bill1.setFname (input.nextLine()); 
      System.out.println("user Last name:"); 
      bill1.setLname (input.nextLine()); 
      System.out.println ("add a user?(Y/N)"); 
      answer = input.nextLine(); 
     } else if (answer.equalsIgnoreCase ("n")) { 
      if (Bill.getBillCounter() == 0) { 
       System.out.println ("the Database is empty"); 
       break; 
      } else { 
       System.out.println ("Number of Users: " 
         + Bill.getBillCounter()); 
       break; 
      } 
     } else { 
      while (!answer.equalsIgnoreCase ("n") 
        && !answer.equalsIgnoreCase ("y")) { 
       System.out.println ("add a user?(Y/N)"); 
       answer = input.nextLine(); 
       } 
      } 
     } 
    } 
} 

請幫我完成這段代碼。

+1

你究竟想要做什麼? –

+0

我想向這個數據庫添加新的對象(bill2,bill3,...),但是我的代碼將新對象寫入到前一個對象中。我想將所有對象信息保留在我的數據庫中。 – msc87

+0

@ msc87如果您將幫助解決問題的答案標記爲已接受的答案(加上您獲得2個業力),這很有幫助! – jbranchaud

回答

1

您還沒有使用過ArrayList,您需要在for循環的末尾添加Bill's對象。

ary.add(bill1); 

,並添加一個類型ArrayList中

ArrayList<Bill> ary = new ArrayList<Bill>(); 
7

,因爲你創建的每個循環新Bill,從不保存過的任何地方你覆蓋它們。我相信你想將它們添加到您的ArrayList

首先,你要一個類型添加到您的ArrayList

ArrayList<Bill> ary = new ArrayList<Bill>(); 

然後,你會得到來自用戶的輸入之前就是否添加新Bill,你應該添加當前這份名單:

... 
System.out.println("user Last name:"); 
bill1.setLname(input.nextLine()); 
ary.add(bill1); 
... 
+0

做了你的建議,現在我可以保存新的對象,但有另一個問題....我無法檢索對象的信息。例如; System.out.println(bill1.getFname(ary.get(index))) – msc87

+1

@ msc87改爲使用'ary.get(index).getFname()'(儘管我無法確定沒有看到Bill類信息。 –

+0

我在這裏把比爾類和我沒有你說什麼,但它返回此錯誤:在線程異常「主要」 java.lang.IndexOutOfBoundsException:指標:1,尺寸:1 \t在java.util.ArrayList.RangeCheck(未知源) \t at java.util.ArrayList.get(Unknown Source) \t at assginment1_version2.Client.main(Client.java:46) – msc87

0

這是比爾類.....

package assginment1_version2; 

public class Bill { 

/** 
* Attributes of a bill 
*/ 
private String firstName; 
private String lastName; 
private int paymentDeadline; 
private int paymentCode; 
private int billCode; 

/** 
* Attribute of Bill Class 
*/ 

     private static int BillCounter=0; 

/** 
* Methods of Bill class 
* @return number of users 
*/ 
/*public static int getBillCounter(){ 
    return BillCounter; 
}*/ 


/** 
* Class Constructor 
* @param Fname is the first name of user 
* @param Lname is the last name of user 
* @param Pdeadline is the deadline of paying the bill 
* @param Pcode introduces the payment uniquely 
* @param Bcode introduces the bill uniquely 
*/ 
    public Bill(){ 
     BillCounter++; 
    } 

/** 
* FirstName methods 
* method to set FirstName 
* @param n is the input of setname method as a user name 
*/ 
public void setFname (String n){ 
    firstName=n; 
} 
// method to get FirstName 
public String getFname(){ 
    return firstName; 
} 


/** 
* LastName methods 
* method to set LastName 
*/ 
public void setLname (String m){ 
    lastName=m; 
} 
// method to get LastName 
public String getLname(){ 
    return lastName; 
} 


/** 
* PaymentDeadline methods 
* method to set PaymentDeadline  
*/ 
public void setPaymentDeadline(int m){ 
    paymentDeadline= m; 
} 
//method to get PaymentDeadline 
public int getPaymentDeadline(){ 
    return paymentDeadline; 
} 

/* 
* PaymentCode methods 
* Method to set PaymentCode 
*/ 
public void setPaymentCode (int m){ 
    paymentCode=m; 
} 
//method to get PaymentCode 
public int getPaymentCode(){ 
    return paymentCode; 
} 

/* 
* Methods of BillCode 
* method to set BillCode 
*/ 
public void setBcode(int Bcode){ 
    billCode=Bcode; 
} 
//method to get BillCode 
public int getBcode(){ 
    return billCode; 
} 
}