Instructions
Requirements and Specifications
Source Code
FRUIT SNACK
/**
* Class representing Fruit Snack instances
*/
public class FruitSnack extends Snack {
/**
* Citrus additional fee
*/
private static final double CITRUS_FEE = 5.99;
/**
* Counter for fruit snacks
*/
private static int fruitSnackCounter = 0;
/**
* Flag, showing if snack contains nut
*/
private boolean hasCitrus;
/**
* Constructor, creating fruit snack of given size without citrus
* @param size fruit snack size
*/
public FruitSnack(Size size) {
super(String.format("FS%04d", ++fruitSnackCounter), size);
hasCitrus = false;
}
/**
* Method for adding citrus to fruit snack
*/
public void addCitrus() {
if (!hasCitrus) {
price += CITRUS_FEE;
}
hasCitrus = true;
}
/**
* Overridden toString method to get String representation of fruit snack object
*/
@Override
public String toString() {
return "type = Fruit Snack, of " + super.toString();
}
}
ORDER SYSTEM
import java.util.Scanner;
/**
* Driver class of application
*/
public class OrderSystem {
/**
* Main driver class of the application
* @param args command line arguments (not required)
*/
public static void main(String[] args) {
try(Scanner scanner = new Scanner(System.in)) {
System.out.println("MENU");
System.out.println("1: Order a Snack");
System.out.println("2: Exit program");
System.out.print("Enter your selection: ");
int choice = Integer.parseInt(scanner.nextLine());
if (choice == 1) {
System.out.print("Do you want Fruit Snack (1) or Salty Snack (2): ");
choice = Integer.parseInt(scanner.nextLine());
System.out.print("What size do you want: S, M, or L: ");
Snack.Size size = Snack.Size.valueOf(scanner.nextLine().trim());
boolean option = false;
Snack snack;
if (choice == 1) {
System.out.print("Do you want citrus fruit included? true/false: ");
option = Boolean.parseBoolean(scanner.nextLine().trim());
FruitSnack fsnack = new FruitSnack(size);
if (option) {
fsnack.addCitrus();
}
snack = fsnack;
}
else {
System.out.print("Do you want nut snack included? true/false: ");
option = Boolean.parseBoolean(scanner.nextLine().trim());
SaltySnack ssnack = new SaltySnack(size);
if (option) {
ssnack.addNut();
}
snack = ssnack;
}
System.out.println("You have chosen snack " + snack.toString());
}
else {
System.out.println("Thank you for using the program. Goodbye!");
}
}
}
}
SALTY SNACK
/**
* Class representing Salty Snack instances
*/
public class SaltySnack extends Snack {
/**
* Nut additional fee
*/
private static final double NUT_FEE = 4.99;
/**
* Counter for salty snacks
*/
private static int saltySnackCounter = 0;
/**
* Flag, showing if snack contains nut
*/
private boolean hasNut;
/**
* Constructor, creating salty snack of given size without nuts
* @param size salty snack size
*/
public SaltySnack(Size size) {
super(String.format("SS%04d", ++saltySnackCounter), size);
hasNut = false;
}
/**
* Method for adding nut to salty snack
*/
public void addNut() {
if (!hasNut) {
price += NUT_FEE;
}
hasNut = true;
}
/**
* Overridden toString method to get String representation of salty snack object
*/
@Override
public String toString() {
return "type = Salty Snack, of " + super.toString();
}
}
SNACK
/**
* Abstract parent class, representing Snack objects
*/
public abstract class Snack {
/**
* Enumeration, containing possible sizes of snack
*/
public enum Size{
/**
* Small Size
*/
S(19.99),
/**
* Middle Size
*/
M(29.99),
/**
* Large Size
*/
L(39.99);
/**
* Size flat fee
*/
private final double fee;
/**
* Private constructor, creating enum instance with given flat fee
* @param fee flat fee of size
*/
private Size(double fee) {
this.fee = fee;
}
/**
* Getter for fee value
* @return fee of size
*/
public double getFee() {
return fee;
}
};
/**
* Snack ID. It has format XX####, where
* - XX is FS for fruit snack and is SS for salty snack;
* - #### four digits, representing index of snack object of that type
*/
private final String id;
/**
* Snack size
*/
private final Size size;
/**
* Snack prize
*/
protected double price;
/**
* Constructor of abstract type. Assigns snack's id and size
* @param id snack id to set
* @param size snack size to set
*/
protected Snack(String id, Size size) {
this.id = id;
this.size = size;
this.price = size.getFee();
}
/**
* Overridden toString method to get String representation of snack object
*/
@Override
public String toString() {
return "type = " + size.name() + ", id = " + id + " and price = " + String.format("$%.2f", price);
}
}