Question

In: Computer Science

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


Here is the 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 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; //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;
}
}

PurchaseDemo:

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

Solutions

Expert Solution

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.

*/

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

    }

}

// Definition of PurchaseDemo class.

public class PurchaseDemo

{

    // Definition of the main() method.

    public static void main(String[] args)

    {

        // Declare a double total variable to store

        // the total bill for each item purchased.

        double total = 0.0;

        Purchase oneSale = new Purchase( );

        // Use for loop to get the user input for items

        // purchased.

        for(int i=0;i<5;i++)

        {

            oneSale.readInput( );

            oneSale.writeOutput( );

            System.out.println("Cost each $" + oneSale.getUnitCost( ));

            System.out.println("Total cost $" + oneSale.getTotalCost( ));

            // Add the subtotal cost of each item

            // purchased to the total.

            total+=oneSale.getTotalCost();

        }

       

        // Display the total Bill for items purchased.

        System.out.println("Total bill of the purchases is $" + total);

   

    // End of the main() method.

    }

// End of the PurchaseDemo class.

}


Related Solutions

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. ****** //////////// ****** ***** //////////\\ ***** **** ////////\\\\ **** *** //////\\\\\\ *** ** ////\\\\\\\\ ** * //\\\\\\\\\\ * \\\\\\\\\\\\
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
For this assignment you will write a Java program using a loop that will play a...
For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project named GuessANumber and create a new Java class in that project named GuessANumber.java for this assignment. The program will randomly generate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a loop where it will prompt the user for a guess. If the user has guessed the...
ASAP PLEASE!!!! USING JAVA /* 1. When should you use a do-while loop? ** Write your...
ASAP PLEASE!!!! USING JAVA /* 1. When should you use a do-while loop? ** Write your answer as a multi-line Java comment ** */ /* 2. Identify the algorithm that matches this code snippet. Your choices are:   sum and average, counting matches, first match, prompt until match, and   comparing adjacent values.  Write your answer below the coded.        int firstNum = 0;   int number = scnr.nextInt();   while (scnr.hasNextInt())   {   int input = scnr.nextInt();   if (input == number)   {   firstNum++;   }   }...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
Write an assembly program (Data and Code) that uses loop to read 10 numbers and output...
Write an assembly program (Data and Code) that uses loop to read 10 numbers and output the largest of those numbers, you can assume any length for those numbers. 80x86 assembly language
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads three strings from the keyboard. Although the strings are in no particular order, display the string that would be second if they were arranged lexicographically.
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT