Java 1 I had gotten some help on this program earlier, but the answer I got did
ID: 3859276 • Letter: J
Question
Java 1
I had gotten some help on this program earlier, but the answer I got did not work or I could not make it work. I also feel there should be a more simple way to acheive the results that are needed. Please help.
The Assignment
This program will display a menu to the user from which they can choose an option for the sale of one of three products.
Once the menu selection is made, the user will be prompted for the quantity of the product that is being purchased.
When the quantity has been entered, the program will look up the price of the item. If three or more items of the same type are purchased, a 5% discount will given on the purchase of that item.
A tally of total sales are kept as the program and can be viewed from a menu option.
2.1. Specifications
Use two separate files with a single class in each one (there could actually be more classes, but two will keep it more manageable).
Name the first file (and class) ProductsSold. This is the one that will have all of the methods other than the main method.
Name the second file TestProductsSold. This file will contain the class that has the main method and the logic which controls the program.
There are three products that are sold from the menu: Widget1, Widget2, and Widget3.
There will be a menu option for each of these items plus options to view sales totals and to exit. On the option to exit, use the System.exit(0) statement.
Use methods to accomplish each of the necessary tasks. For instance, you will need a method to calculate the sales tax, a tally method for each of the three products, a method to look up the price of the product, a method to calculate the discount, a method to display the total sales for all of the products, and a method to display the menu. You may need others as well, depending upon the logic.
Use getters and setters where necessary.
The logic flow will be something like the following:
The user makes a selection from the menu to sell one of the three products.
The user will then be prompted for the quantity of that item.
The program will look up the sales price of that item.
The program will calculate the total sale of the item.
If three or more of those items were purchased, the program will calculate a discount and give the 5% discount.
The tally for the total sales for that item will be updated for this item.
At any time, the user can take the option to display the total sales for each of the three products.
2.2. Output Format
There are several ways in which this program could be written. However, in order to grade for following specifications and to have a clear path to a solution, it is required that you write your program using the specifications below.Explanation / Answer
Hi
Please see below the classes.Please comment for any queries/feedbacks
Thanks
ProductsSold.java
public class ProductsSold {
private int item;
private double salesPrice;
private int qtySold;
private double totalSale;
//Condtructor
public ProductsSold(int item){
this.item = item;
if(1==item){
this.salesPrice =50.00;
}
else if(2== item){
this.salesPrice = 70.00;
}
else if(3== item){
this.salesPrice = 80.00;
}
}
//Getters and setters
public int getItem() {
return item;
}
public void setItem(int item) {
this.item = item;
}
public double getSalesPrice() {
return salesPrice;
}
public void setSalesPrice(double salesPrice) {
this.salesPrice = salesPrice;
}
public int getQtySold() {
return qtySold;
}
public void setQtySold(int qtySold) {
this.qtySold = qtySold;
}
public double getTotalSale() {
this.totalSale = this.salesPrice * qtySold;
return totalSale;
}
public void setTotalSale(double totalSale) {
this.totalSale = totalSale;
}
}
TestProductsSold.java
import java.util.Scanner;
public class TestProductsSold {
public static void main(String[] args) {
//The user makes a selection from the menu to sell one of the three products.
Scanner scan = new Scanner(System.in);
System.out.println("Select the item : ");
System.out.println("1. Widget1");
System.out.println("2. Widget2");
System.out.println("3. Widget3");
System.out.println("0. Exit");
Integer item = Integer.valueOf(scan.nextLine());
if(0 == item){
System.exit(0);
}
//The user will then be prompted for the quantity of that item.
System.out.println("Enter qty:");
Integer qty =Integer.valueOf(scan.nextLine());
//Creating item object
ProductsSold pdt = new ProductsSold(item);
pdt.setQtySold(qty);
//The program will look up the sales price of that item.
double getSalesPrice = pdt.getSalesPrice();
//The program will calculate the total sale of the item.
double totSale = pdt.getTotalSale();
//If three or more of those items were purchased, the program will calculate a discount and give the 5% discount.
double discount = 0.05;
double discountedPrice = totSale;
if(qty>=3){
discountedPrice = totSale * discount ;
}
//The tally for the total sales for that item will be updated for this item.
double tally = 0.0;
totSale = totSale +tally;
System.out.printf("Final sales price :%.2f",discountedPrice);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.