Write a java program for your friend who works at the superstore is urgently in
ID: 3875877 • Letter: W
Question
Write a java program for your friend who works at the superstore is urgently in need of your help with some basic sales records. Consider a class Sales that keeps track of the sales of one item in the store. An object of this class will have the attributes Name of the item Cost of the item - Bulk quantity (to qualify for discount) - Discount (percentage) - Number sold - Total amount Total discount and the following methods - Constructor that sets the cost, bulk quantity and the discount -Get and set methods for cost, bulk quantity and discount registerSale lint n) records the sale of n items. If n is larger than the bulk quantity, the cost will be reduced by the discount displaySales displays the name of the item, number sold, the total amount, and total discount. lgnore sales tax. Implement the class and test it. For example, for Item: Shampoo Cost: 2.50 Bulk quantity (to qualify for discount): 4 Discount: 10 percent Number sold: 10 The output would be Shampoo Number sold: 10 Total amount: $ 22.50 Total discount: $ 2.50 Example Item: Toothpaste Cost: 1.99 Bulk quantity: 20 Discount: 15 Number sold: 10 The output would be Toothpaste Number sold: 10 Total amount: 19.90 Total discount: 0Explanation / Answer
class Sales {
// declaring varaibles
String name;
double cost;
int bulk;
int discount;
int sold;
double totalAmount;
double totalDiscount = 0;
// constructor for cost, bulk quantity and the discount
Sales(double c, int b, int d){
cost = c;
bulk = b;
discount = d;
}
// setters for bulk, cost and discount
void setBulk(int b){
bulk = b;
}
void setCost(double c){
cost = c;
}
void setDiscount(int d){
discount = d;
}
// getters for bulk, cost and discount
int getBulk(){
return bulk;
}
double getCost(){
return cost;
}
int getDiscount(){
return discount;
}
// registering a sale by calculating the values for totalDiscount and totalAmount
void registerSale(int n){
sold = n;
if(sold > bulk)
{
totalDiscount = cost*n*discount/100.0;
}
totalAmount = (cost*n) - totalDiscount;
}
// displaying the information of sale
void displaySales(){
System.out.println(name);
System.out.println("Number sold: "+sold);
System.out.printf("Total amount: $ %.2f ",totalAmount);
System.out.printf("Total discount: $ %.2f ",totalDiscount);
}
// sample runs
public static void main(String[] args) {
Sales item1 = new Sales(2.5, 4, 10);
item1.name = "Shampoo";
item1.registerSale(10);
item1.displaySales();
System.out.println();
Sales item2 = new Sales(1.99, 20, 15);
item2.name = "Toothpaste";
item2.registerSale(10);
item2.displaySales();
}
}
/* SAMPLE OUTPUT
Shampoo
Number sold: 10
Total amount: $ 22.50
Total discount: $ 2.50
Toothpaste
Number sold: 10
Total amount: $ 19.90
Total discount: $ 0.00
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.