2012-05-10 3 views
1

forループのクラスからいくつかのオブジェクトを作成したいとします。私はそれをコード化する方法を知らない。私が書いたものは新しいオブジェクトを作成しますが、前のオブジェクトを上書きします。Javaのfor-loop内にいくつかの新しいオブジェクトを作成します。

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); 

、あなたは各ループに新しいBillを作成し、任意の場所にそれらをオフに保存することはありませんので、あなたがそれらをオーバーライドしているあなたのArrayListに

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

をタイプを追加します。私はあなたのArrayListにそれらを追加したいと考えている:

まず、あなたがあなたのArrayListにタイプを追加する必要があります

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

次に、あなたが追加するか否かのユーザからの入力を取得する前に、 Bill新しい、あなたがこのリストに現在の1を追加する必要があります

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

ここにBillクラスを置いて、あなたが言ったことをしましたが、このエラーを返します:スレッド "main"の例外java.lang.IndexOutOfBoundsException:インデックス:1、サイズ:1 \t at java.util.ArrayList.RangeCheck不明なソース) \t at java.util.ArrayList.get(不明なソース) \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; 
} 
} 
関連する問題