Create a Java application to determine the cost of several items on sale and dis
ID: 3562619 • Letter: C
Question
Create a Java application to determine the cost of several items on sale and display the information
along with the change due to the user.
SaleItem Class:
fields: priceOfItem, discountPercent,
taxRate ? a constant
Methods of SaleItem class:
The default constructor which sets the discountPercent to 0% and the taxRate to .075 and the
priceOfItem to 0
a second constructor (overloaded) which allows the user to set the initial values of the three fields.
setPriceOfItem(double)
setDiscountPercent(double)
double calcDiscountPrice( )
double calcTax( )
Driver class:
This class will have no fields. It will have the main( ) method.
The main( ) method will create an instance of SaleItem.
In a loop:
Prompt the user for the original price of an item.
Prompt the user to enter the discount percent.
Call the appropriate set methods to change the price and discount percent
Print the sales price of the item. Print the price of the item with tax.
Calculate a running sub-total of all items purchased and subtotal of all tax due.
Loop until the user decides to buy no more items.
Once the user decides to buy no more items
Display the total amount due before taxes
Display the total tax amount due and the total amount due with the
tax.
Prompt the user to enter the amount of cash tendered and calculate and display the change due.
Explanation / Answer
************************************************SALE ITEM CLASS**************************************
public class SaleItem {
private double priceOfItem,discountPercent;
private final double taxRate; // Not Clear from Your Program
public SaleItem() {
this.discountPercent=0;
this.taxRate=.075;
this.priceOfItem=0;
}
public SaleItem(double discount,double tax, double price) {
this.discountPercent=discount;
this.taxRate=tax;
this.priceOfItem=price;
}
public void setPriceOfItem(double price){
this.priceOfItem=price;
}
public void setDiscountPercent(double discount){
this.discountPercent=discount;
}
public double calcDiscountPrice(){
double discount_price=((this.priceOfItem)-(this.priceOfItem*this.discountPercent/100));
return discount_price;
} // price-(price*discount/100)
public double calcTax(){
double discount_price=((this.priceOfItem)-(this.priceOfItem*this.discountPercent/100));
double sale_tax=discount_price*this.taxRate;
return (discount_price+sale_tax);
} // (Price-Discount)+((Price - discount)*tax/100)
}
***************************************DRIVER CLASS************************************************
import java.util.Scanner;
public class Driver {
public static void main(String args[])
{
double price=0;
double tax=0;
char yes='y';
Scanner sc=new Scanner(System.in);
do
{
SaleItem s1=new SaleItem();
System.out.println("Original Price of an Item");
double original=sc.nextDouble();
System.out.println("Discount Percentage on an Item");
double discount=sc.nextDouble();
s1.setDiscountPercent(discount);
s1.setPriceOfItem(original);
price+=s1.calcDiscountPrice();
tax+=s1.calcTax();
System.out.println("Sale Price on Item : "+price);
System.out.println("Tax Price on Item : "+tax);
System.out.println("Do you Wanted To Buy More Item");
yes =sc.next().charAt(0);
}while(yes=='y' || yes=='Y');
System.out.println("Final Sale Price on Items : "+price);
System.out.println("Final Tax Price on Items : "+tax);
System.out.println("Enter The Amount of Cash Tendered");
double cash=sc.nextDouble();
System.out.println("Cash Amount : " +cash+" - Total Payable : "+tax+" = "+(cash-tax));
}
}
***************************************RESULT**********************************************
--------------------Configuration: <Default>--------------------
Original Price of an Item
1000
Discount Percentage on an Item
10
Sale Price on Item : 900.0
Tax Price on Item : 967.5
Do you Wanted To Buy More Item
y
Original Price of an Item
2000
Discount Percentage on an Item
20
Sale Price on Item : 2500.0
Tax Price on Item : 2687.5
Do you Wanted To Buy More Item
n
Final Sale Price on Items : 2500.0
Final Tax Price on Items : 2687.5
Enter The Amount of Cash Tendered
3000
Cash Amount : 3000.0 - Total Payable : 2687.5 = 312.5
Process completed.