Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help with Java this is my code and I need my output to match with the attached screenshot: my code: import java.util.Scanner; public
I need help with Java
this is my code and I need my output to match with the attached screenshot:
my code:
import java.util.Scanner; public class StockItem { static int id; String description; double price; int quantity; public StockItem(){ id++; } public StockItem(String description, double price, int quantity) { this.description = description; this.price = price; this.quantity = quantity; } public String ToString() { // Milk: Item number: 0 is 1 Gallon of Milk has price 3.6 we currently have 15 in stock //Bread: Item number: 1 is 1 Loaf of bread has price 1.98 we currently have 30 in stock return "StockItem{" + "description='" + description + '\'' + ", price=" + price + ", stock=" + quantity + '}'; } public static int getId() { return id; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public double getPrice() { return price; } public void setPrice(double price) { if(price>=0) { this.price = price; } else{ System.out.println("Invalid Price"); } } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { if(quantity>=0) { this.quantity = quantity; } else{ System.out.println("Invalid Quantity"); } } public static void main(String[] args) { StockItem milk = new StockItem(); StockItem bread = new StockItem(); milk.setDescription("1 Gallon of Milk"); milk.setPrice(3.60); milk.setQuantity(15); bread.setDescription("1 Loaf of Bread"); bread.setPrice(1.98); bread.setQuantity(30); while(true){ System.out.println("1. Sold One Milk"); System.out.println("2. Sold One Bread"); System.out.println("3. Change price of Milk"); System.out.println("4. Change price of Bread"); System.out.println("5. Add Milk to Inventory"); System.out.println("6. Add Bread to Inventory"); System.out.println("7. See Inventory"); System.out.println("8. Quit"); Scanner sc = new Scanner(System.in); int inp = sc.nextInt(); if(inp==1){ milk.setQuantity(milk.getQuantity()-1); } else if(inp==2){ bread.setQuantity(bread.getQuantity()-1); } else if(inp==3){ System.out.println("Enter new price"); double pr = sc.nextDouble(); milk.setPrice(pr); } else if(inp==4){ System.out.println("Enter new price"); double pr = sc.nextDouble(); bread.setPrice(pr); } else if(inp==5){ milk.setQuantity(milk.getQuantity()+1); } else if(inp==6){ bread.setQuantity(bread.getQuantity()+1); } else if(inp==7){ System.out.println(milk.ToString()); System.out.println(bread.ToString()); } else{ break; } } } }
the expected output:
my output:
PLEASE AND THANK YOU!!!
Sample Output: 1. Sold One Milk 2. Sold One Bread 3. Change price of Milk 4. Change price of Bread 5. Add Milk to Inventory 6. Add Bread to Inventory 7. See Inventory 8. Quit 7 Milk: Item number: 0 is 1 Gallon of Milk has price 3.6 we currently have 15 in stock Bread: Item number: 1 is 1 Loaf of bread has price 1.98 we currently have 30 in stock 1. Sold One Milk 2. Sold One Bread 3. Change price of Milk 4. Change price of Bread 5. Add Milk to Inventory 6. Add Bread to Inventory 7. See Inventory 8. Quit 1 Milk: Item number: 0 is 1 Gallon of Milk has price 4.0 we currently have 13 in stock Bread: Item number: 1 is 1 Loaf of bread has price 1.98 we currently have 29 in stock 1. Sold One Milk 2. Sold One Bread 3. Change price of Milk 4. Change price of Bread 5. Add Milk to Inventory 6. Add Bread to Inventory 7. See Inventory 8. Quit 6 How many bread did we get? 5 1. Sold One Milk 2. Sold One Bread 3. Change price of Milk 4. Change price of Bread 5. Add Milk to Inventory 6. Add Bread to Inventory 7. See Inventory 8. Quit 7 Milk: Item number: 0 is 1 Gallon of Milk has price 4.0 we currently have 13 in stock Bread: Item number: 1 is 1 Loaf of bread has price 1.98 we currently have 34 in stock 1. Sold One Milk 2. Sold One Bread 3. Change price of Milk 4. Change price of Bread 5. Add Milk to Inventory 6. Add Bread to Inventory 7. See Inventory 8. Quit 8 1. Sold One Milk 2. Sold One Bread 3. Change price of Milk 4. Change price of Bread 5. Add Milk to Inventory 6. Add Bread to Inventory 7. See Inventory 8. Quit StockItem\{description='1 Gallon of Milk', price=3.6, stock=15\} StockItem\{description='1 Loaf of Bread', price=1.98, stock=30\} 1. Sold One Milk 2. Sold One Bread 3. Change price of Milk 4. Change price of Bread 5. Add Milk to Inventory 6. Add Bread to Inventory 7. See Inventory 8. Quit 1. Sold One Milk 2. Sold One Bread 3. Change price of Milk 4. Change price of Bread 5. Add Milk to Inventory 6. Add Bread to Inventory 7. See Inventory 8. Quit 1. Sold One Milk 2. Sold One Bread 3. Change price of Milk 4. Change price of Bread 5. Add Milk to Inventory 6. Add Bread to Inventory 7. See Inventory 8. Quit 1. Sold One Milk 2. Sold One Bread 3. Change price of Milk 4. Change price of Bread 5. Add Milk to Inventory 6. Add Bread to Inventory 7. See Inventory 8. Quit 3 Enter new price 4.00 1. Sold One Milk 2. Sold One Bread 3. Change price of Milk 4. Change price of Bread 5. Add Milk to Inventory 6. Add Bread to Inventory 7. See Inventory 8. Quit StockItem\{description=' 1 Gallon of Milk', price=4. , stock=13\} StockItem { description='1 Loaf of Bread', price =1.98, stock=29\} 1. Sold One Milk 2. Sold One Bread 3. Change price of Milk 4. Change price of Bread 5. Add Milk to Inventory 6. Add Bread to Inventory 7. See Inventory 8. Quit 1. Sold One Milk 2. Sold One Bread 3. Change price of Milk 4. Change price of Bread 5. Add Milk to Inventory 6. Add Bread to Inventory 7. See Inventory 8. Quit StockItem\{description=' 1 Gallon of Milk', price=4. , stock=13\} StockItem\{description='1 Loaf of Bread', price=1.98, stock=30\} 1. Sold One Milk 2. Sold one Bread 3. Change price of Milk 4. Change price of Bread 5. Add Milk to Inventory 6. Add Bread to Inventory 7. See Inventory 8. Quit 8 Process finished with exit codeStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started