Question

Here a snip of a java code:

PrintWriter outputFile = new PrintWriter(DATA);
        do {

            ...
           ...
           ...

                listOfItems += String.format("%10d %-15s $%.2f\n", quantity, productspec.getDescription(), quantity * productspec.getPrice());

          ...
          ...
      

            // Putting it in output file
            outputDataLine = "----------------------------" + "\nItems list:";
            outputFile.println(outputDataLine);
            outputFile.println(listOfItems);
            outputFile.printf("Subtotal $%.2f\n", subtotal);
          
          ....
          ....


        } while (choice.equals("y") || choice.equals("Y"));

         outputFile.close();

My Questions is, how do you remove the duplicates in the listOfItems and combine the number of quantity and the price of a particular items in the txt output file

Here is the sample output that stored in txt file.

File Edit Format View Help Items list: 2 candy $1.96 $0.45 1 gum Subtotal $2.41 Total with Tax (6%) $ 2.55 Tender Amount: 5.0

For instance, Chocolate were listed twice, just need to listed once all the items that is duplicates.

Like this

3   Chocolate 5.37

.....so on

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here's one of the ways how you can achieve that.

Create 2 hashtables for storing quantity and the total price of the product.

Replace the line which updates listOfItems with lines which update the hashtables.

Later on, in the section where the print functions are being called to print the data, update the listOfItems by going through every entry through the hashtables and the print the list. You can see in the following snippet how you can go about doing it. Look at the screenshot in case the text looks confusing to you. Take help of the comments I wrote about which lines serve what purpose. Feel free to ask any doubts by commenting on the answer.

Hashtable<String, Integer> ht = new Hashtable<String, Integer>(); // ht store quantity of the item
Hashtable<String, double> hp = new Hashtable<String, double>(); // hp stores the total price of the item
PrintWriter outputFile = new PrintWriter(DATA);
do {
...
...
...
String str =productspec.getDescription(); // str = name of product
// If the product is already in table ht, increase the quantity and total price
if (ht.containsKey(str))
{
ht.put(str, ht.get(str) + quantity);
hp.put(str, hp.get(str) + quantity*productspec.getPrice());
}
// Else insert str with quantity in table ht, and str with price in table hp
else
{
ht.put(str, quantity);
hp.put(str, quantity*productspec.getPrice());
}
...
...
  
// Putting it in output file
outputDataLine = "----------------------------" + "\nItems list:";
outputFile.println(outputDataLine);
listOfItems = "";
Enumeration names = ht.keys(); // Extract all the keys from the table ht
while(names.hasMoreElements()) // Go over every key
{
String key = (String) names.nextElement(); // Convert the next key to String form
listOfItems += String.format("%10d %-15s $%.2f\n", ht.get(key), key, hp.get(key)); // Add all the values to listOfItems
}
ht.clear(); // Clears out the hashtable for next iteration
hp.clear(); // Clears out the hashtable for next iteration
  
outputFile.println(listOfItems);
outputFile.printf("Subtotal $%.2f\n", subtotal);
....
....
} while (choice.equals("y") || choice.equals("Y"));
outputFile.close();

1 2 Hashtable<String, Integer> ht = new Hashtable<string, Integer>(); // ht store quantity of the item Hashtable<String, doub

Add a comment
Know the answer?
Add Answer to:
Here a snip of a java code: PrintWriter outputFile = new PrintWriter(DATA);         do {            ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT