1

I want to create several objects from a class in a for loop. but I don't know how to code it. What I have written creates a new object but it overwrites the previous object.

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

please help me to complete this code.

3
  • 1
    What exactly are you trying to do? Commented May 10, 2012 at 22:08
  • I want to add new objects (bill2, bill3,...) to this database, but my code writes the new object over the previous one.I want to keep all objects information in my database. Commented May 10, 2012 at 22:17
  • @msc87 It is helpful if you mark the answer that helped solve the problem as the accepted answer (plus you get 2 karma)! Commented Jun 7, 2012 at 18:22

3 Answers 3

7

You're overriding them because you create a new Bill on each loop and never save them off anywhere. I believe you want to add them to your ArrayList:

First, you should add a type to your ArrayList:

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

Then, before you get the input from the user on whether or not to add a new Bill, you should add the current one to this list:

...
System.out.println("user Last name:");
bill1.setLname(input.nextLine());
ary.add(bill1);
...
Sign up to request clarification or add additional context in comments.

5 Comments

did what you suggested and now I can save new objects but there is anther problem....I cannot retrieve the objects' information. for example; System.out.println(bill1.getFname(ary.get(index)))
@msc87 Use ary.get(index).getFname() instead (though I can't be sure without seeing the Bill class information.
I put the the Bill class here and I did what you said but it returns this error: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.RangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at assginment1_version2.Client.main(Client.java:46)
@msc87 Since this is a new problem, you should either, accept an answer and then ask a new question for this new problem or edit your original question with your new problem. Also, when updating, don't post as an answer, you should edit your original question.
@msc87 Arrays are 0 based in java. If the length of your arraylist is 1, then you get Bill by: ary.get(0).getFname().
1

You haven't used the ArrayList, you need to add the Bill's objects at the end of the for loop.

ary.add(bill1);

and add a type to your ArrayList

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

Comments

0

This is the Bill class.....

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.