In shop.java, The idea is that the user should follow the sequence of setting up
ID: 3588800 • Letter: I
Question
In shop.java,
The idea is that the user should follow the sequence of setting up shop. It should utilize at least one method;
You may use more as you find necessary but this project should illustrate competence in method usage.
Setup Shop:
1) Ask for the number of items to sell
2) For each item
a) Ask the name of the item (one word)
b) Ask the price of the item
3) Discount
a) Ask for the threshold (over which amount to give discount)
b) Ask for the rate (how much % discount)
4) User can run this setup multiple times so keep the latest version.
Explanation / Answer
/************************Shop.java****************************************/
/**
* The Class Shop.
*/
public class Shop {
/** The name. */
private String name;
/** The price. */
private float price;
/** The threshold. */
private int threshold;
/** The discount rate. */
private float discountRate;
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Sets the name.
*
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets the price.
*
* @return the price
*/
public float getPrice() {
return price;
}
/**
* Sets the price.
*
* @param price the price to set
*/
public void setPrice(float price) {
this.price = price;
}
/**
* Gets the threshold.
*
* @return the threshold
*/
public int getThreshold() {
return threshold;
}
/**
* Sets the threshold.
*
* @param threshold the threshold to set
*/
public void setThreshold(int threshold) {
this.threshold = threshold;
}
/**
* Gets the discount rate.
*
* @return the discountRate
*/
public float getDiscountRate() {
return discountRate;
}
/**
* Sets the discount rate.
*
* @param discountRate the discountRate to set
*/
public void setDiscountRate(float discountRate) {
this.discountRate = discountRate;
}
}
/**********************ShopDriver.java**************************/
import java.util.Scanner;
/**
* The Class ShopDriver.
*/
public class ShopDriver {
/**
* Display.
*
* @param shopes
* the shopes
*/
public static void display(Shop[] shopes) {
for (Shop shop : shopes) {
System.out.println("Item Name: " + shop.getName() + " Price:" + shop.getPrice() + " Discount:"
+ shop.getDiscountRate());
}
}
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter number of items to sell");
int items = input.nextInt();
input.nextLine();
Shop[] products = new Shop[items];
for (int i = 0; i < items; i++) {
products[i] = new Shop();
System.out.println("Please Enter the item name");
String name = input.nextLine();
System.out.println("Price of item");
float price = input.nextFloat();
products[i].setName(name);
products[i].setPrice(price);
System.out.println("Any Discount? y or n");
String ch = input.next();
if (ch.equalsIgnoreCase("y")) {
System.out.println("Please enter the threshold");
int threshold = input.nextInt();
products[i].setThreshold(threshold);
System.out.println("Howmuch % discount");
float discountRate = input.nextFloat();
products[i].setDiscountRate(discountRate);
}
input.nextLine();
}
System.out.println("Displaying Information");
display(products);
input.close();
}
}
/**************************output******************************/
Enter number of items to sell
1
Please Enter the item name
Tea
Price of item
23.45
Any Discount? y or n
y
Please enter the threshold
21
Howmuch % discount
10
Displaying Information
Item Name: Tea Price:23.45 Discount:10.0
Thanks a lot. Please let me know if you have any doubt.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.