Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

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

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.

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 */

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students explore these related Databases questions

Question

Consider ideas concerning channel flows.

Answered: 3 weeks ago