Create a Java program that uses the Purchase class, found at the bottom and name
ID: 3628815 • Letter: C
Question
Create a Java program that uses the Purchase class, found at the bottom and named Purchase.java to set the following prices:
Oranges: 10 for $2.99
Eggs: 12 for $2.69
Apples: 3 for $1.00
Watermelons: $4.39 each
Bagels: 6 for $3.50
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
(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 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 () {
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 class Purchase{ public void setName(String theName) { name = theName; } public void setPrice(int count, double costForCount) { if (count <= 0) { System.out.println("Error: Bad parameter in setPrice."); System.exit(0); } else { groupCount = count; groupPrice = costForCount; } } public void setNumber(int number) { numberBought = number; } public String nameOfItem() { return name; } public double totalCost() { return ((groupPrice/groupCount)*numberBought); } public double unitCost() { return (groupPrice/groupCount); } public int number() { return numberBought; } return numberBought; } public void input() throws IOException { SavitchIn keyboard = new SavitchIn(); String itemName; int count; double cost; int totalNumber; System.out.println("Enter name of what is being purchased:"); itemName = keyboard.readLine(); System.out.println("Enter price of item on two lines"); System.out.println("For example, 3 for $2.99 is entered as"); System.out.println("3"); System.out.println("2.99"); System.out.println("Enter price of item on two lines, now:"); count = keyboard.readLineInt(); cost = keyboard.readLineDouble(); System.out.println("Enter number of items purchased"); totalNumber = keyboard.readLineInt(); System.out.println(totalNumber + " " + itemName); System.out.println("at " + count + " for $" + cost); System.out.println("Is that correct?(y/n)"); char ans = keyboard.readLineNonwhiteChar(); while ((ans != 'y') && (ans != 'Y')) { System.out.println("Enter purchased items:"); itemName = keyboard.readLine(); System.out.println("Enter price of item on two lines"); System.out.println("For example, 3 for $2.99 is entered as"); System.out.println("3"); System.out.println("2.99"); System.out.println("Enter price of item on two lines, now:"); count = keyboard.readLineInt(); cost = keyboard.readLineDouble(); System.out.println("Enter number of items purchased"); totalNumber = keyboard.readLineInt(); System.out.println(totalNumber + " " + itemName); System.out.println("at " + count + " for $" + cost); System.out.println("Is that correct?(y/n)"); ans = keyboard.readLineNonwhiteChar(); } name = itemName; groupCount = count; groupPrice = cost; numberBought = totalNumber; } public void output() { System.out.println(numberBought + " " + name); System.out.println("at " + groupCount + " for $" + groupPrice); } private String name; private int groupCount; private double groupPrice; private int numberBought; }