Welcome to Mobilarian Forum - Official Symbianize forum.

Join us now to get access to all our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, and so, so much more. It's also quick and totally free, so what are you waiting for?

Java purchase program

A 0

ampaosampanotoy

Transcendent
Member
Joined
Jul 24, 2014
Messages
19
Reaction score
3
Points
1
Age
31
grants
₲620
10 years of service
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( ));
}
}:tan:
 
S 0

shienso

Transcendent
Member
Access
Joined
Sep 1, 2014
Messages
31
Reaction score
4
Points
8
grants
₲471
10 years of service
try daw... with interface :challenge: .. hehe
 
Crius 0

Crius

Squaddie
Member
Access
Joined
Mar 29, 2015
Messages
318
Reaction score
363
Points
63
grants
₲4,216
10 years of service
pa copy ng code mo boss ah,,maganda to boss kung may OOP na, interface :3
 
jughead3716 50

jughead3716

Alpha and Omega
Contributor
Access
Joined
Jun 28, 2014
Messages
5,120
Reaction score
11,325
Points
113
grants
₲128,617
11 years of service
nice share punkz...magagamit talaga ito ng mga coders dito sa katz... :D
 
P 0

pizvet

Squaddie
BANNED
Member
Access
Joined
Sep 25, 2022
Messages
384
Reaction score
10
Points
18
Age
27
Location
manila
grants
₲699
2 years of service
hayst salamat talaga hahahaha may pagkukunan na rin ako ng ideya
 
D 0

dietrich1

Transcendent
BANNED
Member
Access
Joined
Nov 8, 2023
Messages
35
Reaction score
0
Points
6
grants
₲110
1 years of service
will test this code if okay, thank you
 
Top Bottom