JAVA
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; //Total number being purchased.
public Purchase ()
{ name = "no name";
groupCount = 0;
groupPrice = 0;
numberBought = 0;
}
public Purchase (String name, int groupCount,
double groupPrice, int numberBought)
{
this.name = name;
this.groupCount = groupCount;
this.groupPrice = groupPrice;
this.numberBought = numberBought;
}
public void setName(String name)
{
this.name = name;
}
/** 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;
}
public String getName( )
{
return name;
}
public double getTotalCost( )
{
return ((groupPrice/groupCount)*numberBought);
}
public double getUnitCost( )
{ return (groupPrice/groupCount);
}
public int getNumberBought( )
{ return numberBought;
}
/** Gets price and number being purchased from keyboard. */
public void readInput( )
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter name of item you are purchasing:");
name = scan.nextLine( );
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:");
groupCount = scan.nextInt( );
groupPrice = scan.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 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:");
groupCount = scan.nextInt( );
groupPrice = scan.nextDouble( );
}
System.out.println("Enter number of items purchased:");
numberBought = Scan.nextInt( );
while (numberBought <= 0)
{//Try again:
System.out.println(
"Number must be positive. Try again.");
System.out.println("Enter number of items purchased:");
numberBought = scan.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 Purchase testQuestion(Purchase p1, int newX)
{
newX = 20;
this.setName("pears");
p1.setName("oranges");
p1 = new Purchase("kiwi", 6, 3.00, 3);
Purchase p2 = new Purchase("banana", 5, 4.50, 10);
return p2;
}
}
public class PurchaseDemo
{
public static void main(String[] args)
{
Purchase oneSale = new Purchase( );
oneSale.readInput( );
oneSale.writeOutput( );
System.out.println("Cost each $" + oneSale.getUnitCost( ));
System.out.println("Total cost $" + oneSale.getTotalCost( ));
}
}
4) (8 points) Assume that the class Purchase has get() and set() methods for all the instance variables. Using the class Purchase and the class PurchaseDemo on the top of page 5 as a guide, write a program that uses the Purchase class and the following data as input. The program is to determine which input value generates the highest total cost to the consumer. The output(you may ignore the output associated with the readInput() method) will consist of listing the names of the input items and their total cost and then a line that states the item which cost the most and its cost. You may use integer, double, and String variables as needed but you must use only one object of the Purchase class and a for loop. This question does not use arrays.
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
SAMPLE OUTPUT:( this ignores the lines of output generated by readInput())
You bought these five items:
AAAAA $$$$$
BBBBB $$$$$
CCCCC $$$$$
DDDDD $$$$$
EEEEE $$$$$
The most expensive item you bought was XXXXX and it cost $$$$$.
Here is the solution to the given problem. Solution.java is the main file containing the answer to your question
Purchase.java (given by you)
import java.io.*;
import java.util.Scanner;
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; //Total number being purchased.
public Purchase() {
name = "no name";
groupCount = 0;
groupPrice = 0;
numberBought = 0;
}
public Purchase(String name, int groupCount, double groupPrice, int
numberBought) {
this.name = name;
this.groupCount = groupCount;
this.groupPrice = groupPrice;
this.numberBought = numberBought;
}
public void setName(String name) {
this.name = name;
}
/** 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;
}
public String getName() {
return name;
}
public double getTotalCost() {
return ((groupPrice / groupCount) * numberBought);
}
public double getUnitCost() {
return (groupPrice / groupCount);
}
public int getNumberBought() {
return numberBought;
}
/** Gets price and number being purchased from keyboard. */
public void readInput() {
Scanner scan = new Scanner(System.in);
System.out.println("Enter name of item you are purchasing:");
name = scan.nextLine();
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:");
groupCount = scan.nextInt();
groupPrice = scan.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 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:");
groupCount = scan.nextInt();
groupPrice = scan.nextDouble();
}
System.out.println("Enter number of items purchased:");
numberBought = scan.nextInt();
while (numberBought <= 0) { //Try again:
System.out.println("Number must be positive. Try again.");
System.out.println("Enter number of items purchased:");
numberBought = scan.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 Purchase testQuestion(Purchase p1, int newX) {
newX = 20;
this.setName("pears");
p1.setName("oranges");
p1 = new Purchase("kiwi", 6, 3.00, 3);
Purchase p2 = new Purchase("banana", 5, 4.50, 10);
return p2;
}
}
Solution.java
public class Solution {
public static void writeOutput(Purchase oneSale)
{
System.out.println(oneSale.getName() + "\t" +
oneSale.getTotalCost());
}
public static void main(String[] args) {
String mostExpensiveName =
"";
double highestTotalCost = 0;
System.out.println("You bought
these five items:");
// One Purchase object to read
input for each iteration of the loop
Purchase oneSale = new Purchase(
);
for(int i=1; i<=5; i++) {
oneSale.readInput( );
writeOutput(oneSale);
if(oneSale.getTotalCost() > highestTotalCost) {
highestTotalCost = oneSale.getTotalCost();
mostExpensiveName = oneSale.getName();
}
}
System.out.println("The most
expensive item you bought was "
+ mostExpensiveName + " and it costs "
+ highestTotalCost);
}
}
Get Answers For Free
Most questions answered within 1 hours.