Question

In: Computer Science

JAVA CODE Using the PurchaseDemo program and output from the previous problem as your guide, write...

JAVA CODE

Using the PurchaseDemo program and output from the previous problem as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items as indicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemo program. Then print the total bill at the end. You may use integer, double, and String variables as needed but you may use only one object of the Purchase class and a for loop.

(Use the readInput() method for the following input data, you may assume the prompts and Scanner for the readInput() method takes place but you need not include either in your program output. )

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

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());

}

}

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;
}
}

Solutions

Expert Solution

The code to calculate subtotal and total bill amount is as follows:

import java.util.Scanner;

public class PurchaseImpl {
   public static void main (String []args)
   {
       Scanner input = new Scanner(System.in);
       System.out.println("Enter number of items you are purchasing:");
       int num = input.nextInt(); //taking number of items as input which in the given question is 5
       Purchase pur= new Purchase(); //Object of Purchase class
       int count=1;
       double total=0; //total bill amount
       for(int i=0;i<num;i++)
       {
           System.out.println("Adding Product: "+ count++);
           pur.readInput();
           pur.writeOutput();
           double itemCost=pur.getTotalCost();
           System.out.println("Cost of "+ pur.getNumberBought() + " " +pur.getName() + ": $" + itemCost);
           total+=itemCost;          
       }
       System.out.println("Total Cost: $"+total);
   }
}

The output of the above program excluding the prompts of readInput() method is as follows:

Enter number of items you are purchasing:5
Cost of 24 Oranges: $7.176000000000001
Cost of 36 Eggs: $5.07
Cost of 20 Apples: $6.666666666666666
Cost of 2 Watermelons: $8.78
Cost of 12 Bagels: $7.0
Total Cost: $34.69266666666667

For the given use case i.e. use only one object of Purchase class to purchase multiple items, i have taken only one object and inside for loop the data members of Purchase class are overriden in each iteration of for loop, hence enabling us to use one object to purchase multiple items.


Related Solutions

JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items as indicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemo program. Then print the total bill at the end Use the readInput() method for the following input data Oranges: 10 for...
Write a program that produces the following output using nested for loop in Java. ****** ////////////...
Write a program that produces the following output using nested for loop in Java. ****** //////////// ****** ***** //////////\\ ***** **** ////////\\\\ **** *** //////\\\\\\ *** ** ////\\\\\\\\ ** * //\\\\\\\\\\ * \\\\\\\\\\\\
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program that will print the number of words, characters, and letters read as input. It is guaranteed that each input will consist of at least one line, and the program should stop reading input when either the end of the file is reached or a blank line of input is provided. Words are assumed to be any non-empty blocks of text separated by spaces, and...
WRITE CODE IN JAVA it is now your turn to create a program of your choosing....
WRITE CODE IN JAVA it is now your turn to create a program of your choosing. If you are not sure where to begin, think of a task that you repeat often in your major that would benefit from a program. For example, chemistry unit conversions, finding the area for geometric shapes, etc. You can also create an interactive story that changes based on the user input/decisions. The possibilities are endless. The program must include instructions for the user. Be...
write a java code program using loops to compute the sum of the 30 terms of...
write a java code program using loops to compute the sum of the 30 terms of the series below. Find sum of all integers between 100 and 200 which are divisible by 9 or 13 Write a program to find the sum of the first 8 terms of the series 1 + 11 + 111 + 1111 + . . . Output: Term Ratio Sum
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester.    The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Please code by Java. The following preemptive program with this problem after sample output(fill random number,...
Please code by Java. The following preemptive program with this problem after sample output(fill random number, recursive search, non-recursive search, exit), You just need to modify this code to add sorting and time calculations. Problem Description: Expand on Lab3 by adding methods that implement Bubble Sort, Selection Sort, Insertion Sort, Merge Sort and Quick Sort for displaying the sorted integers from the randomly generated array. NOTE: Create separate methods for each type of sort. Run Tests for all 4 types...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT