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

I HAVE TO MODIFY THIS PROGRAM BY INSERTING A LOOP THAT WILL ALLOW ME TO ENTER MU

ID: 3646272 • Letter: I

Question

I HAVE TO MODIFY THIS PROGRAM BY INSERTING A LOOP THAT WILL ALLOW ME TO ENTER MULTIPLE TRANSACTIONS, RATHER THEN JUST ONE. IT SHOULD ALSO GIVE A GRAND TOTAL AT THE END. COULD ANYONE GIVE HELP ME WITH THE STATEMENTS FOR THE WHILE LOOP?

THANK YOU
import java.text.DecimalFormat;
import java.util.Scanner;
/**
Class for the purchase of one kind of item, such as 3 oranges.
Prices are set supermarket style, such as 5 for $1.25.
*/
public class Purchase
{
private String name;
private int groupCount; //Part of a price, like the 2 in 2 for $1.99.
private double groupPrice; //Part of a price, like the $1.99
// in 2 for $1.99.
private int numberBought; //Number of items bought.

public void setName (String newName)
{
name = newName;
}


/**
Sets price to count pieces for $costForCount.
For example, 2 for $1.99.
*/
public void setPrice (int count, double costForCount)
{
if ((count <= 0) || (costForCount <= 0))
{
System.out.println ("Error: Bad parameter in setPrice.");
System.exit (0);
}
else
{
groupCount = count;
groupPrice = costForCount;
}
}


public void setNumberBought (int number)
{
if (number <= 0)
{
System.out.println ("Error: Bad parameter in setNumberBought.");
System.exit (0);
}
else
numberBought = number;
}


/**
Reads from keyboard the price and number of a purchase.
*/
public void readInput ()
{
Scanner keyboard = new Scanner (System.in);
System.out.println ("Enter name of item you are purchasing:");
name = keyboard.nextLine ();
System.out.println ("Enter price of item as two numbers.");
System.out.println ("For example, 3 for $2.99 is entered as");
System.out.println ("3 2.99");
System.out.println ("Enter price of item as two numbers, now:");
groupCount = keyboard.nextInt ();
groupPrice = keyboard.nextDouble ();

while ((groupCount <= 0) || (groupPrice <= 0))
{ //Try again:
System.out.println (
"Both numbers must be positive. Try again.");
System.out.println ("Enter price of item as two numbers.");
System.out.println ("For example, 3 for $2.99 is entered as");
System.out.println ("3 2.99");
System.out.println (
"Enter price of item as two numbers, now:");
groupCount = keyboard.nextInt ();
groupPrice = keyboard.nextDouble ();
}
System.out.println ("Enter number of items purchased:");
numberBought = keyboard.nextInt ();
while (numberBought <= 0)
{ //Try again:
System.out.println ("Number must be positive. Try again.");
System.out.println ("Enter number of items purchased:");
numberBought = keyboard.nextInt ();
}
}


/**
Displays price and number being purchased.
*/
public void writeOutput ()
{
DecimalFormat formatter = new DecimalFormat("0.00");

System.out.println (numberBought + " " + name);
System.out.println ("at " + groupCount +
" for $" + groupPrice);
}


public String getName ()
{
return name;
}


public double getTotalCost ()
{
return (groupPrice / groupCount) * numberBought;
}


public double getUnitCost ()
{
return groupPrice / groupCount;
}


public int getNumberBought ()
{
return numberBought;
}
}

Explanation / Answer

public void readInput () { Scanner keyboard = new Scanner (System.in); System.out.println ("Enter name of item you are purchasing:"); name = keyboard.nextLine (); System.out.println ("Enter price of item as two numbers."); System.out.println ("For example, 3 for $2.99 is entered as"); System.out.println ("3 2.99"); System.out.println ("Enter price of item as two numbers, now:"); groupCount = keyboard.nextInt (); groupPrice = keyboard.nextDouble (); ------>System.out.println ("Would like to have another entry?"); decision = keyboard.value(); return decision After that write a new loop public void readmultipleinput () { do while(decision) /*will first execute while for one time and later it will check for decision is 0 or not.Enters loop second time if decision is non zero. { readinput() /* call readinput() method here..it will execute as many times as the user wants to have entries } } I think you have got a base idea of what to do. Please instantiate new variables or a group of arrays to store the entry variables