• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help:( I need additional coding ideas for java arraylist in printing shopping cart receipts project  RSS feed

 
Edrew Cardano
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

ReceiptCode.java

Items.java
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lists allow duplicates. Sets don't.

If you add itemA 2 times a list, your list will have 2 itemA elements.

If you add itemA 2 times to a set, your set will have 1 itemA element. What makes it unique? The set internally uses a hash to determine if the first itemA is the same as the second itemA you add.

Have you learned the concept of equals and hashcode methods?
 
Edrew Cardano
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Lists allow duplicates. Sets don't.

If you add itemA 2 times a list, your list will have 2 itemA elements.

If you add itemA 2 times to a set, your set will have 1 itemA element. What makes it unique? The set internally uses a hash to determine if the first itemA is the same as the second itemA you add.

Have you learned the concept of equals and hashcode methods?


not yet, i will study it now and will try to give update. thank you.

ps. please enlighten me a bit in their difference(equals() and hashcode())
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The concept of equals and hashcode is pretty simple. Say you have a class with 3 attributes.



Now say you have 2 users named "John" and "Jane". Are these users the same? From the name attribute, they are different.

Consider "John with dob=Jan 4 2000" and "John with dob=Jan 4 2005". Looking at this example, using the name attribute is not enough because both are "John". Yet if the date of birth attribute is also used, then these 2 "John"s are different.

What makes up the equals and hashcode methods? The attributes you want or need to check. So the code will look like:



These methods can usually be generated with the help of the IDE.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!