Question
Where and how would I add code that allows the user to choose if they want to include nuts or citrus instead of automatically choosing?
Where and how would I add code that allows the user to choose if they want to include nuts or citrus instead of automatically choosing?
//Snack.Java
public class Snack {
private String id;
private String size;
private double price;
public Snack(String id, String size) {
setId(id);
setSize(size);
this.size = size;
if(size.equals("S"))
setPrice(19.99);
else if(size.equals("M"))
setPrice(29.99);
else if(size.equals("L"))
setPrice(39.99);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return "ID: "+id+" Size: "+size+" Price: "+price;
}
}
//SaltySnack.java
public class SaltySnack extends Snack {
private boolean isNutSnack;
public SaltySnack(String id, String size,boolean isNutSnack) {
super(id, size);
setNutSnack(isNutSnack);
if(isNutSnack)
setPrice(getPrice()+4.50);
}
public boolean isNutSnack() {
return isNutSnack;
}
public void setNutSnack(boolean isNutSnack) {
this.isNutSnack = isNutSnack;
}
public String toString() {
return "Salty Snack "+super.toString()+" Is nut snack: "+isNutSnack;
}
}
//FruitSnack.java
public class FruitSnack extends Snack {
private boolean isCitrus;
public FruitSnack(String id, String size,boolean isCitrus) {
super(id, size);
setCitrus(isCitrus);
if(isCitrus)
setPrice(getPrice()+5.99);
}
public boolean isCitrus() {
return isCitrus;
}
public void setCitrus(boolean isCitrus) {
this.isCitrus = isCitrus;
}
public String toString() {
return "Fruit Snack "+super.toString()+" Is citrus fruit: "+isCitrus;
}
}
//Main.java
import java.util.Scanner;
public class OrderSystem {
public static Scanner sc=new Scanner(System.in);
public static void menu() {
System.out.println(" O-Order Snack.");
System.out.println("E-Exit.");
System.out.print("Enter choice(O,E): ");
}
public static void submenu() {
System.out.println(" F-Fruit Snack");
System.out.println("S-Salty Snack");
System.out.print("Enter choice(F,S): ");
}
public static void sizeMenu() {
System.out.println(" S-Small");
System.out.println("M-Medium");
System.out.println("L-Large");
System.out.print("Enter choice(S,L,M): ");
}
public static String input() {
String str=sc.nextLine();
return str;
}
public static void displayOrder(String type,String size) {
Snack snack;
if(type.equals("F")) {
snack=new FruitSnack("12FF",size,true);
System.out.println(snack);
}
else if(type.equals("S")) {
snack=new SaltySnack("11SS",size,false);
System.out.println(snack);
}
else
System.out.println(" Invlid input!");
}
public static void order() {
String type,size;
submenu();
type=input();
if(type.equals("F") || type.equals("S")) {
sizeMenu();
size=input();
if(size.equals("S") || size.equals("M") || size.equals("L"))
displayOrder(type,size);
else
System.out.println(" Invlid input!");
}
else
System.out.println(" Invlid input!");
}
public static void run() {
String ch="";
do {
menu();
ch=input();
if(ch.equals("O"))
order();
else if(ch.equals("E"))
break;
else
System.out.println(" Invlid input!");
}while(true);
}
public static void main(String[] args) {
run();
}
}
Step 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