In: Computer Science
JAVA CODE
Define a method called changeGroupPrice that could be added to the definition of the class Purchase. This method has one parameter that is of type double, and is named salePercent. This number represents the percent reduction in the groupPrice amount. The method uses this number to change the groupPrice. The code of the method should check to make sure the range of salePercent is between 0 and 50%. If it is, the groupPrice should be adjusted accordingly. If the salePercent is out of range of the accepted values an error message should be printed and the program terminated.
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;
}
}
Purchase.java
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();
}
}
public boolean changeGroupPrice(double salePercent)
{
if(salePercent>=0 &&
salePercent<=50) {
this.groupPrice
= this.groupPrice*(1-(salePercent/100));
return
true;
}
else {
System.out.println("SalePercent is not in the range [0,50]");
return
false;
}
}
/**
* 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.java
import java.util.Scanner;
public class PurchaseDemo
{ public static void main(String[] args)
{
Purchase oneSale = new Purchase();
oneSale.readInput();
System.out.println("Enter the salePercent (It must be between 0
and 50) : ");
Scanner scan = new Scanner(System.in);
double sp = scan.nextDouble();
boolean flag = oneSale.changeGroupPrice(sp);
if(flag) {
oneSale.writeOutput();
System.out.println("Cost each $" + oneSale.getUnitCost());
System.out.println("Total cost $" +
oneSale.getTotalCost());
}
}
}
Output
TestRun-1
Enter name of item you are purchasing:
apple
Enter price of item as two numbers.
For example, 3 for $2.99 is entered as
3 2.99
Enter price of item as two numbers, now:
2 3.00
Enter number of items purchased:
2
Enter the salePercent (It must be between 0 and 50) :
50
2 apple
at 2 for $1.5
Cost each $0.75
Total cost $1.5
TestRun-2
Enter name of item you are purchasing:
apple
Enter price of item as two numbers.
For example, 3 for $2.99 is entered as
3 2.99
Enter price of item as two numbers, now:
2 3.00
Enter number of items purchased:
2
Enter the salePercent (It must be between 0 and 50) :
51
SalePercent is not in the range [0,50]
Sreenshot
Feel free to ask any doubts, if you face any difficulty in understanding.
Please upvote the answer if you find it helpful