In: Computer Science
Write a program that uses the Purchase class in Listing 5.13 (found in the Sakai Week 6 Source Code folder) to create 5 Purchase objects and set the following prices for them:
Oranges: 10 for $2.99
Eggs: 12 for $1.69
Apples: 3 for $1.00
Watermelons: $4.39 each
Bagels: 6 for $3.50
Also use the 5 Purchase objects to set the number bought and then calculate the cost of each of the following five items and the total bill:
2 dozen oranges
3 dozen eggs
20 apples
2 watermelons
1 dozen bagels
You may use the readInput method to read these values in from the user, including the number of items bought (shown above), or you may just call each of the objects'setName and setPrice methods in maindirectly to set their names, group prices, and number bought by using literal values like "oranges", 10, 2.99, and 24 (the number of oranges bought, 2 dozen = 2 * 12 == 24).
The program should output exactly the following lines with the letters a through o and t replaced by the correct values - use the appropriate Purchase object setters and getters to generate these lines and fill in the correct values based on the purchases given above:
a *items* at $b each cost $c d *items* at $e each cost $f g *items* at $g each cost $i j *items* at $k each cost $l m *items* at $n each cost $o total cost: $t
In the first line above, items is oranges, and a is 24, b is the unit cost of an orange, and c is the total cost of buying 24 of them. Similarly, items in the next 4 lines will be eggs, apples, watermelons, and bagels with appropriate values in letters d through o. t in the last line is the sum c+f+i+l+o from the 5 lines above. Use the 5 Purchase objects to fill in all of these values.
IN JAVA
public class Purchase
{
private String name;
private int groupCount; // Number of items in a group, like the 2 in 2 for $1.99.
private double groupPrice;
// Price per item in a group, like the 1.99 in 2 for $1.99.
private int numberBought; // Total number being purchased by customer.
public void setName(String newName)
{
name = newName;
}
/**
Sets price to groupCount pieces for $costForCount. E.g., 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;
}
/**
Gets price and number being purchased from keyboard.
*/
public void readInput()
{
System.out.println("Enter name of item you are purchasing on a separate line:");
name = Keyboard.nextLine(); // Keyboard is a special class that only reads from the keyboard
System.out.println("Enter price of item on one or two lines.");
System.out.println("For example, 3 for $2.99 is entered as either");
System.out.println("3");
System.out.println("2.99");
System.out.println("or: 3 2.99");
System.out.println("Enter price of item on one or two lines, 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 on one or two lines as shown above:");
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();
}
}
/**
Outputs price and number being purchased to screen.
*/
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;
}
public static void main(String[] args)
{
Purchase oranges = new Purchase();
Purchase eggs = new Purchase();
Purchase apples = new Purchase();
Purchase watermelons = new Purchase();
Purchase bagels = new Purchase();
double totalCost = 0;
// write code to fill in the appropriate values and
// calculate the costs and total using the methods
// from the Purchase class; do not calculate the
// values on your own, use the methods to do that!
// you may read values from the user using readInput
// or you can just fill in the values given above as
// literal values, like "oranges", 10, and 2.99, and,
// later, the number of oranges bought, 2 * 12 == 24
// (2 dozen)
/* your code goes here */
}
}
// Purchase.java
import java.util.Scanner;
public class Purchase
{
private String name;
private int groupCount; // Number of items in a group, like the 2 in 2 for
// $1.99.
private double groupPrice;
// Price per item in a group, like the 1.99 in 2 for $1.99.
private int numberBought; // Total number being purchased by customer.
// for the sake of compilation, I have declared Keyboard as a Scanner object
// to read inputs. If you have a Keyboard class, then please remove below
// line, the program will work fine.
private static Scanner Keyboard = new Scanner(System.in);
public void setName(String newName)
{
name = newName;
}
/**
* Sets price to groupCount pieces for $costForCount. E.g., 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;
}
/**
* Gets price and number being purchased from keyboard.
*/
public void readInput()
{
System.out
.println("Enter name of item you are purchasing on a separate line:");
name = Keyboard.nextLine(); // Keyboard is a special class that only
// reads from the keyboard
System.out.println("Enter price of item on one or two lines.");
System.out.println("For example, 3 for $2.99 is entered as either");
System.out.println("3");
System.out.println("2.99");
System.out.println("or: 3 2.99");
System.out.println("Enter price of item on one or two lines, 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 on one or two lines as shown above:");
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();
}
}
/**
* Outputs price and number being purchased to screen.
*/
public void writeOutput()
{
// updated to print the details in proper format as shown in sample
// output. Note that values are rounded to 2 digits after decimal point,
// so values like 0.299 may appear as 0.30
System.out.printf("%d %s at $%.2f each cost $%.2f\n", numberBought,
name, getUnitCost(), getTotalCost());
}
public String getName()
{
return name;
}
public double getTotalCost()
{
return ((groupPrice / groupCount) * numberBought);
}
public double getUnitCost()
{
return (groupPrice / groupCount);
}
public int getNumberBought()
{
return numberBought;
}
public static void main(String[] args)
{
Purchase oranges = new Purchase();
Purchase eggs = new Purchase();
Purchase apples = new Purchase();
Purchase watermelons = new Purchase();
Purchase bagels = new Purchase();
double totalCost = 0;
// write code to fill in the appropriate values and
// calculate the costs and total using the methods
// from the Purchase class; do not calculate the
// values on your own, use the methods to do that!
// you may read values from the user using readInput
// or you can just fill in the values given above as
// literal values, like "oranges", 10, and 2.99, and,
// later, the number of oranges bought, 2 * 12 == 24
// (2 dozen)
// updating name, number bought and price of each item using hardcoding
// as it is allowed
oranges.setName("oranges");
oranges.setNumberBought(2 * 12);
oranges.setPrice(10, 2.99);
eggs.setName("eggs");
eggs.setNumberBought(3 * 12);
eggs.setPrice(12, 1.69);
apples.setName("apples");
apples.setNumberBought(20);
apples.setPrice(3, 1.00);
watermelons.setName("watermelons");
watermelons.setNumberBought(2);
watermelons.setPrice(1, 4.39);
bagels.setName("bagels");
bagels.setNumberBought(12);
bagels.setPrice(6, 3.50);
// printing output for oranges
oranges.writeOutput();
// adding oranges cost to total
totalCost += oranges.getTotalCost();
// doing the same for other purchases as well
eggs.writeOutput();
totalCost += eggs.getTotalCost();
apples.writeOutput();
totalCost += apples.getTotalCost();
watermelons.writeOutput();
totalCost += watermelons.getTotalCost();
bagels.writeOutput();
totalCost += bagels.getTotalCost();
// displaying total cost formatted to 2 digits after decimal point
System.out.printf("total cost: $%.2f\n", totalCost);
}
}
/*OUTPUT*/
24 oranges at $0.30 each cost $7.18
36 eggs at $0.14 each cost $5.07
20 apples at $0.33 each cost $6.67
2 watermelons at $4.39 each cost $8.78
12 bagels at $0.58 each cost $7.00
total cost: $34.69