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 main directly 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.
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 */
}
}
Solution:
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;
}
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;
}