Using only one object of the Purchase class and a for loop, write a program that
ID: 664921 • Letter: U
Question
Using only one object of the Purchase class and a for loop, write a program that uses the Purchase class to set the following prices and buy the number of items as indicated:
Orange : 10 for 2.99 buy 2 dozen
Eggs: 12 for 1.69 buy 3 dozen
Apples: 3 for 1.00 buy 20
Watermelon: 4.39 each buy 2
Bagels: 6 for 3.50 buy 1 dozen
Use integer, String and double as needed. Calculate the subtotals and total bill called total. Using the writeOutput() method to display the subtotals as they are generated. Then print total bill.
Purchase Class:
import java.util.Scanner;
public class Purchase {
private String name;
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;
}
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;
}
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 ();
}
}
public void writeOutput () {
System.out.println (numberBought + " " + name);
System.out.println ("at " + groupCount +
" for $" + groupPrice);
}
public void setGroupCount(int newGroupcount){
groupCount = newGroupcount;
}
public int getGroupCount(){
return groupCount;
}
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
Code:
public class UsePurchase
{
public static void main(String args[])
{
int count_pur_items;
double sub_total,total=0;
Purchase Current_purchase=new Purchase();
for(count_pur_items=1;count_pur_items<=5;count_pur_items++)
{
Current_purchase.readInput();
System.out.println("Cost of each "+Current_purchase.getName() + " is "+ Current_purchase.getUnitCost());
System.out.println("Cost of" + Current_purchase.getNumberBought()+" is "+Current_purchase.getTotalCost());
total=total+Current_purchase.getTotalCost();
Current_purchase.writeOutput();
}
System.out.println("Total Cost $ " + total);
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.