1

I am trying to finish my home assignment for programming class, unfortunately getting stuck through half of it ,due to my stupidity. ArrayList that I have created overwrites 0 index constantly.

Here are my classes for creating invoices:

import java.util.ArrayList;

public class Order 
{
    private String customerName;
    private ArrayList<LineItem> items = new ArrayList<LineItem>();

    public Order(String customerName)
    {
        this.customerName = customerName;
    }

    public String getCustomerName()
    {
        return customerName;
    }

    public ArrayList<LineItem> getItems()
    {   
        return this.items;
    }

    public double getOrderTotal()
    {

        double totalOrder = items.get(0).getTotalPrice();
        return totalOrder;
    }

    public void addItem(String description,double unitPrice, int quantity)
    {
        LineItem object = new LineItem(description,unitPrice,quantity);
        items.add(object);
    }

    public String toString()
    {
        String.format("%n%-20s%-15s%-15f%-15f",items.get(0).getDescription(),
    items.get(0).getQuantity(),items.get(0).getUnitPrice(),getOrderTotal());

        return p;`                           

    }

}

public class LineItem
{
    private String description;
    private double unitPrice;
    private int quantity;

    public LineItem(String description,double unitPrice, int quantity)
    {
        this.description = description;
        this.unitPrice = unitPrice;
        this.quantity = quantity;
    }
    public String getDescription()
    {
        return this.description;
    }

    public double getUnitPrice()
    {
        return this.unitPrice;
    }

    public int getQuantity()
    {
        return this.quantity;
    }

    public double getTotalPrice()
    {
        return this.unitPrice * this.quantity;
    }
    public String toString()
    {
        return  String(this.description,this.unitPrice,this.quantity)     
    }
}

AND a part of ... while LOOP for main class

do
{

    customerName = AssignmentHelper.getRequiredStringInput("Please enterthe customer's name: ","A customer name must be entered!");

    newOrder = new Order(customerName);
    newOrder.addItem(description,unitPrice,quantity);

} while(Character.toString(userAnswer).equalsIgnoreCase ("Y"));

2 Answers 2

4

Your loop creates a new Order object in each iteration :

newOrder = new Order(customerName);

Each Order object has a new empty ArrayList, which is why it seems the first index is always overwritten.

If you want a single ArrayList to hold all the items, you should create a single Order object prior to the do-while loop.

newOrder = new Order(customerName);
do {
    ...
    newOrder.addItem(description,unitPrice,quantity);
} while(Character.toString(userAnswer).equalsIgnoreCase ("Y"));
Sign up to request clarification or add additional context in comments.

3 Comments

do you know how can i itirate through ArrayList insie the toString().Ideally i guess it suppose to override the TO String method from LineItem.
@DenysFiialko What exactly do you want to do? Print the contents of the ArrayList?
Program should display conents of Array using Order class with template from LineItem class. Customer: Name ORDER ITEMS -------------------------------------------------------------------------------- Item Description 000 00,000.00 000,000.00 {...} --------------------------------------------------------------------------------
0

In the loop, a new order object is created which has two fields (customer name and an Arraylist of line items). Initially the ArrayList will be empty.The call to addItem object creates a new Lineitem object and add its to the ArrayList items. Therefore the lineItem object will always be added to index zero.

Create a new object only when a new customer is added.You can have a HashMap where key is the customer name. If the key already exists add to the same object else create a new order object and it to the Map.

1 Comment

Thank you!!Appreciate fast response

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.