Using the PurchaseDemo program and output as your guide, write a program that us
ID: 3630378 • Letter: U
Question
Using the PurchaseDemo program and output as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items as indicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemo program. Then print the total bill at the end
Use the readInput() method for the following input data
Oranges: 10 for 2.99 buy 2 dozen oranges
Eggs: 12 for 1.69 buy 3 dozen eggs
Apples: 3 for 1.00 buy 20 apples
Watermelons: 4.39 each buy 2 watermelons
Bagels: 6 for 3.50 buy 1 dozen bagels
Here is the Purchase Class:
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 price, like the 2 in 2 for $1.99.
private double groupPrice;//Part of 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( )
{
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;
}
}
PurchaseDemo:
public class PurchaseDemo
{
public static void main(String[] args)
{
Purchase Purchase( );
oneSale.readInput( );
oneSale.writeOutput( );
System.out.println("Cost each $" + oneSale.getUnitCost( ));
System.out.println("Total cost $" + oneSale.getTotalCost( ));
}
}
Explanation / Answer
Program:
import java.util.Scanner;
public class PurchaseDemo
{
public static void main(String[] args)
{
double tBill = 0.0;
Purchase>new Purchase();
oneSale.readInput();
oneSale.writeOutput();
System.out.println("Cost each $" + oneSale.getUnitCost());
System.out.println("Total cost $" + oneSale.getTotalCost());
tBill+=oneSale.getTotalCost();
oneSale.readInput();
oneSale.writeOutput();
System.out.println("Cost each $" + oneSale.getUnitCost());
System.out.println("Total cost $" + oneSale.getTotalCost());
tBill+=oneSale.getTotalCost();
oneSale.readInput();
oneSale.writeOutput();
System.out.println("Cost each $" + oneSale.getUnitCost());
System.out.println("Total cost $" + oneSale.getTotalCost());
tBill+=oneSale.getTotalCost();
oneSale.readInput();
oneSale.writeOutput();
System.out.println("Cost each $" + oneSale.getUnitCost());
System.out.println("Total cost $" + oneSale.getTotalCost());
tBill+=oneSale.getTotalCost();
oneSale.readInput();
oneSale.writeOutput();
System.out.println("Cost each $" + oneSale.getUnitCost());
System.out.println("Total cost $" + oneSale.getTotalCost());
tBill+=oneSale.getTotalCost();
System.out.println("Total bill of the purchases is $" + tBill);
}
}
/**
Class for the purchase of one kind of item, such as 3 oranges.
Prices are set supermarket style, such as 5 for $1.25.
*/
class Purchase
{
private String name;
private int groupCount; //Part of price, like the 2 in 2 for $1.99.
private double groupPrice;//Part of 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( )
{
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;
}
}
Sample Output:
Enter name of item you are purchasing:
Oranges
Enter price of item as two numbers.
For example, 3 for $2.99 is entered as
3 2.99
Enter price of item as two numbers, now:
10 2.99
Enter number of items purchased:
24
24 Oranges
at 10 for $2.99
Cost each $0.29900000000000004
Total cost $7.176000000000001
Enter name of item you are purchasing:
Eggs
Enter price of item as two numbers.
For example, 3 for $2.99 is entered as
3 2.99
Enter price of item as two numbers, now:
12 1.69
Enter number of items purchased:
36
36 Eggs
at 12 for $1.69
Cost each $0.14083333333333334
Total cost $5.07
Enter name of item you are purchasing:
Apples
Enter price of item as two numbers.
For example, 3 for $2.99 is entered as
3 2.99
Enter price of item as two numbers, now:
3 1.00
Enter number of items purchased:
20
20 Apples
at 3 for $1.0
Cost each $0.3333333333333333
Total cost $6.666666666666666
Enter name of item you are purchasing:
Watermelons
Enter price of item as two numbers.
For example, 3 for $2.99 is entered as
3 2.99
Enter price of item as two numbers, now:
1 4.39
Enter number of items purchased:
2
2 Watermelons
at 1 for $4.39
Cost each $4.39
Total cost $8.78
Enter name of item you are purchasing:
Bagels
Enter price of item as two numbers.
For example, 3 for $2.99 is entered as
3 2.99
Enter price of item as two numbers, now:
6 3.50
Enter number of items purchased:
12
12 Bagels
at 6 for $3.5
Cost each $0.5833333333333334
Total cost $7.0
Total bill of the purchases is $34.69266666666667
Hope this would help you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.