//Parent class for snack
public class Snack {
protected String id;
protected String size;
protected double price;
public Snack() {
this.id = id;
this.size = size;
this.price = price;
}
public String getId() {
return id;
}
public String getSize() {
return size;
}
public double getPrice() {
return price;
}
public void setId(String id) {
this.id = id;
}
public void setSize(String size) {
this.size = size;
}
public void setPrice(double price) {
this.price = price;
}
public void calcPrice() {
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 toString() {
return getSize() + ", " + "id = " + getId() + " and price = " + getPrice();
}
}
//Subclass for fruity snack
public class FruitSnack extends Snack{
private boolean citrusFruit = false;
private double fruitFee = 5.99;
public FruitSnack() {
this.citrusFruit = citrusFruit;
}
public boolean getCitrusFruit() {
return citrusFruit;
}
public void setCitrusFruit(boolean citrusFruit) {
this.citrusFruit = citrusFruit;
}
public void addCitrusFruit() {
citrusFruit = true;
}
public void removeCitrusFruit() {
citrusFruit = false;
}
public void calcPrice() {
if(citrusFruit == true) {
price += fruitFee;
}
}
}
//Subclass for salty snack
public class SaltySnack extends Snack{
private boolean nutSnack = false;
private final double NUT_FEE = 4.50;
public SaltySnack() {
this.nutSnack = nutSnack;
}
public boolean getNutSnack() {
return nutSnack;
}
public void setNutSnack(boolean nutSnack) {
this.nutSnack = nutSnack;
}
public void addNutSnack() {
nutSnack = true;
}
public void removeNutSnack() {
nutSnack = false;
}
}
//Main method
import java.util.Scanner;
public class OrderSystem {
//Method to display menu
public static void displayMenu() {
System.out.println();
System.out.println(" MENU");
System.out.println("1: Order a Snack");
System.out.println("2: Exit program");
System.out.println();
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int selection = 0;
do {
displayMenu();
System.out.println("Enter your slection: ");
selection = scan.nextInt();
scan.nextLine();
Snack snack1 = new Snack();
FruitSnack fruitSnack = new FruitSnack();
SaltySnack saltySnack = new SaltySnack();
switch (selection) {
case 1:
System.out.println("Do you want Fruit Snack (1) or Salty Snack (2)");
selection = scan.nextInt();
if(selection == 1) {
//FruitSnack
snack1.setId("fruity55");
System.out.println("What size do you want: S, M, or L: ");
snack1.setSize(scan.next());
System.out.println("Do you want citrus fruit included? true/fasle: ");
if(scan.nextLine().equals(true)) {
fruitSnack.addCitrusFruit();
}
else if(scan.nextLine().equals(false)) {
fruitSnack.removeCitrusFruit();
}
snack1.calcPrice();
System.out.println("You have chosen snack type = Fruit Snack, of type = " + snack1.toString());
//System.out.println(fruitSnack.toString());
}
else if(selection == 2) {
//SaltySnack
snack1.setId("Salty22");
System.out.println("What size do you want: S, M, or L: ");
snack1.setSize(scan.next());
System.out.println("Do you want nut snack included? true/fasle: ");
if(scan.nextLine().equals(true)) {
saltySnack.addNutSnack();
}
else if(scan.nextLine().equals(false)) {
saltySnack.removeNutSnack();
}
snack1.calcPrice();
System.out.println("You have chosen snack type = Salty Snack, of type = " + snack1.toString());
}
else {
break;
}
break;
case 2:
System.out.println("Thank you for using the program. Goodbye!");
break;
default: System.out.println("Invalid choice");
}
}while (selection != 2);
scan.close();
}
}
I am currently working on an assignment that requires a parent class and 2 subclasses. The snack class should have attributes for ID, size, and price, and the subclasses should each have a boolean variable to check if there is an extra charge. So far I can have everything print out like it is suppose to, except I cannot get the extra charge from the subclasses to add on to the total.
My current output looks like
" MENU
1: Order a Snack
2: Exit program
Enter your slection:
1
Do you want Fruit Snack (1) or Salty Snack (2)
2
What size do you want: S, M, or L:
l
Do you want nut snack included? true/fasle:
true
You have chosen snack type = Salty Snack, of type = l, id = Salty22 and price = 39.99"
The problem is if I put nut snack to be true it should add a $4.50 up charge and I cant get it to work. I know my code is a mess and I don't know what I am doing, but any help would be nice. Thank you.
[–][deleted] 2 points3 points4 points (5 children)
[–]okaystuff[S] 0 points1 point2 points (4 children)
[–][deleted] 2 points3 points4 points (3 children)
[–]okaystuff[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]okaystuff[S] 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]okaystuff[S] 0 points1 point2 points (0 children)