Hello java friends, I'm new to the concept of ArrayList and I want to solve this problem in my code.
my main problem:
I don't know how to sort itemA and itemB to appear only once then It will just add the total prices together.

this is the behavior of my code:

C:\Documents and Settings\Drew\Desktop>java ReceiptCode
Enter Company Name
ABC Co
Enter STREET ADDRESS
123 Main Street
Enter CITY, STATE, ZIP
City, State, 123456
How many items did you order?
4
Enter Item Name
itemA
Enter Quantity?
2
Enter Price?
2
Enter Item Name
itemB
Enter Quantity?
1
Enter Price?
4
Enter Item Name
itemA
Enter Quantity?
4
Enter Price?
2
Enter Item Name
itemB
Enter Quantity?
1
Enter Price?
4
ABC Co
123 Main Street
City, State, 123456     10/26/2014
16:29
------------------------------
2 x itemA : 2.0$
------------------------------
1 x itemB : 4.0$
------------------------------
4 x itemA : 2.0$
------------------------------
1 x itemB : 4.0$
------------------------------

I don't know how to sort itemA and itemB to appear only once then It will just add the total prices together.

here is my sourcecode:

import java.util.Scanner;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;


import java.util.*;


class ReceiptCode {
private static final char[] ItemPrice = null;

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    //Font f = new Font("Calibri", Font.BOLD, 20);

    @SuppressWarnings("resource")
    Scanner scan= new Scanner(System.in);
    System.out.println("Enter Company Name");
    String companyName= scan.nextLine();

    System.out.println("Enter STREET ADDRESS");
    String street=scan.nextLine();

    System.out.println("Enter CITY, STATE, ZIP");
    String CSZ=scan.nextLine();

    //System.out.println(companyName + "\n" + street + "\n" + CSZ);


    String breaker = "------------------------------";
    List <Items> invList = new ArrayList<Items>();
    System.out.println("How many items did you order?");
    int counter = scan.nextInt();
    double totalPrice = 0;
    for (int i=0; i<counter; i++)
    {
        System.out.println("Enter Item Name");
        String fName = scan.next();
        System.out.println("Enter Quantity?");
        int fType = scan.nextInt();
        System.out.println("Enter Price?");
        double fPrice = scan.nextDouble();
        Items inv = new Items(fName, fType, fPrice);
        double x = (fType * fPrice);
        totalPrice += x;
        invList.add(inv);
        //System.out.println(totalPrice);
    }

    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    DateFormat timeFormat = new SimpleDateFormat ("HH:mm");
    Date date = new Date();
    Date time = new Date();
    System.out.printf("%-15s %n", companyName);
    System.out.printf("%-15s %14s %n",street + "\n" + CSZ,dateFormat.format(date));
    System.out.printf("%-15s %n", timeFormat.format(time));
    System.out.println(breaker);
    for (Items c : invList) {

        System.out.println (c.getItemQTY() + " x " + c.getItemName() + " : " +       c.getItemPrice() + "$");
           System.out.println (breaker);

}   
}
}

Items.java

public class Items {

    private String ItemName;
    private int ItemQTY;
    private double ItemPrice;

public Items (String fdType, int fdAmount, double fdPrice)
{
    ItemName = fdType;
    ItemQTY = fdAmount;
    ItemPrice = fdPrice;
}
public String getItemName()
{
    return ItemName;
}
public int getItemQTY()
{
    return ItemQTY;
}
public double getItemPrice()
{
    return ItemPrice;
}
    }

When you are entering the item names etc, check to see if there is already an item in the ArrayList with that name and price. If there is, add the quantity to the existing Item, otherwize create a new Item like you do now

if u compare only names, in the clase Items (Item name is recommended) maybe can be use an

@Override
public String toString(){
    return itemName;
}

In the same way that youre using an foreach, you can make an search of this item...

    boolean exist = false;
    for(Items itemExistent : invLst){
        if(itemExistent.equals(newItem)){
        //then retrieve an put values if exists, after that u dont save the newItem in the List
            itemExistent.setAmount(itemExistent.getAmount() + newItem.getAmount());
            itemExistent.setQty(itemExistent.getQty() + newItem.getQty());//etc
            exist = true;
        }//end of for
        //then  if NOT exist save this item in the List
        if(exist){
            invLst.add(newItem)
        }
    }

//... u can refactor it! here is an some basice example ;) regards

Don't hijack toString() for a specific problem like this. Save it for a complete description of the Item object. getName() would be better.

equals(newItem) isn't going to help unless you define equals for the Item class.

Line 8: that close bracket closes the if block, not the for loop (closed at line 13)

You add the new item only if it matches and you updated the existing item. That's the opposite of what you need.

...Sorry zolymo, this isn't one of your best posts :(

sure ;) ... an refactor is good? i make it in this page (dont make an solution for obvius reason), sorry.

In the same way that youre using an foreach, you can make an search of this item...

boolean exist = false;
for(Items itemExistent : invLst){
    if(itemExistent.getName().equals(newItem.getName())){
    //then retrieve an put values if exists, after that u dont save the newItem in the List
        itemExistent.setAmount(itemExistent.getAmount() + newItem.getAmount());
        itemExistent.setQty(itemExistent.getQty() + newItem.getQty());//etc
        exist = true;
    }
 }end of for
    //then  if NOT exist save this item in the List
    if(exist){
        invLst.add(newItem)
    }

//... u can refactor it! here is an some basice example ;)

That's a lot better, but you still have line 11 wrong - where's the NOT?

hahahaha... is hard! but first @edrewc need post :D ... in line 11
put if(!exist)
ty JamesCherrill

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.