Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My program keeps coming out with wrong numbers when I add citrus fruit to the fruit basket and I feel like the Citrus/Nut menu overcomplicates

My program keeps coming out with wrong numbers when I add citrus fruit to the fruit basket and I feel like the Citrus/Nut menu overcomplicates the code. Do you have any suggestions on changing any of the code?

//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;

}

}

//OrderSystem.java

import java.util.Scanner;

public class OrderSystem {

public static Scanner sc=new Scanner(System.in);

public static void menu() {

System.out.println(" 1-Order Snack.");

System.out.println("2-Exit.");

System.out.print("Enter choice: ");

}

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,M,L): ");

}

public static void nutCitrusMenu() {

System.out.println(" N-Nuts");

System.out.println("C-Citrus");

System.out.print("Enter choice(N,C): ");

}

public static String input() {

String str=sc.nextLine();

return str;

}

public static void displayOrder(String type,String size, String nutCitrus) {

Snack snack;

if(type.equals("F")) {

if(nutCitrus.equals("C")) {

snack=new FruitSnack("12FF",size,true);

System.out.println(snack);

}

else if(nutCitrus.equals("N")) {

System.out.println("Nuts are not allowed in fruit snack.");

}

else

System.out.println(" Invalid input!");

}

else if(type.equals("S")) {

if(nutCitrus.equals("C")) {

System.out.println("Citrus is not allowed in salty snack.");

}

else if(nutCitrus.equals("N")) {

snack=new SaltySnack("11SS",size,true);

System.out.println(snack);

}

else

System.out.println(" Invalid input!");

}

else

System.out.println(" Invalid input!");

}

public static void order() {

String type,size,nutCitrus;

submenu();

type=input();

if(type.equals("F") || type.equals("S")) {

sizeMenu();

size=input();

if(size.equals("S") || size.equals("M") || size.equals("L")) {

nutCitrusMenu();

nutCitrus=input();

if(nutCitrus.equals("C") || nutCitrus.equals("N"))

displayOrder(type,size,nutCitrus);

else

System.out.println(" Invalid input!");

}

else

System.out.println(" Invalid input!");

}

else

System.out.println(" Invalid input!");

}

public static void run() {

String ch="";

do {

menu();

ch=input();

if(ch.equals("1"))

order();

else if(ch.equals("2"))

System.out.println(" Thank you for using the program. Goodbye! ");

System.exit(0);

}while(true);

}

public static void main(String[] args) {

run();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions

Question

Differentiate 3sin(9x+2x)

Answered: 1 week ago

Question

Compute the derivative f(x)=(x-a)(x-b)

Answered: 1 week ago

Question

4. Devise an interview strategy from the interviewers point of view

Answered: 1 week ago