Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

At First , when I input \"Sentra\" and quantity for \"2\" and ask whethere conti

ID: 3622159 • Letter: A

Question


At First , when I input "Sentra" and quantity for "2" and ask whethere continue then

Input "y" for continue then I input "sonata" and quantity for "2"...

then my result is like

Item Price Qty Amount
sonata 100.0 4 1000.0
--------------------------------------------------

However, I do want the result like under...



Item Price Qty Amount
Sentra 400.0 2 800.0
sonata 100.0 2 200.0

-----------------------------------------------------------
I am attaching my code so please help me out...

Thank you in advance..

import java.util.Scanner;
import java.util.Hashtable;

public class posting
{
public static void main(String[] args)
{
char cont;
String input;
String orderDate;
int cashierNumber;
int qtyOfItem;
String order;
int totalQtyOfItem = 0;
Object obj;
int[] array1;
double totalPrice = 0.0;

Scanner keyboard = new Scanner(System.in);

Hashtable ht = new Hashtable();

ht.put("sonata", new Double(100));
ht.put("Elantra", new Double(200));
ht.put("Excel", new Double(300));
ht.put("Sentra", new Double(400));
ht.put("Genesis", new Double(500));
ht.put("Cube", new Double(500));
ht.put("camry", new Double(500));

System.out.println("Enter the order date(mm/dd/yy) :");
orderDate = keyboard.nextLine();



do
{
System.out.println("Which car do you want to buy... ");
order = keyboard.nextLine();
obj = ht.get(order);
//System.out.println(obj);
obj = obj.toString();
double d = Double.valueOf((String) obj).doubleValue();


System.out.println("How many car? ");
qtyOfItem = keyboard.nextInt();
totalPrice += (d * qtyOfItem);

totalQtyOfItem += qtyOfItem;

keyboard.nextLine();

System.out.println("Do you want to continue(Y/N))");
input = keyboard.nextLine();
cont = input.charAt(0);


}
while(cont == 'Y' || cont == 'y');


System.out.println("Date: " + orderDate);
System.out.println("");
System.out.println("");
System.out.println("Item Price Qty Amount");
System.out.println(order + " " + obj + " " +totalQtyOfItem +" " + totalPrice );


}

}

Explanation / Answer

Each iteration of the while loop, save the output in a ArrayList of Strings or similar. Also, you need to reset totalPrice and totalQtyOfItem each iteration.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote