Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA. Use only three classes, no more. Use enums please; for example. public enu

ID: 3822434 • Letter: J

Question

JAVA. Use only three classes, no more. Use enums please; for example.

public enum FoodCatagory { Savory (“savory”)

                      Sweet_pie (“sweet pie”)

                        Sweet_Treat (“Sweet Treat”)

                       }

Pop’s Pie Shop wants you to help computerize its ever-changing menu. Here’s an example of the delicious items that Pop’s has to offer: Here is what we are starting with today: Savory: Savory Mince & Cheese Pork & Peach BBQ Curried Lamb & Spinach Chicken & Kumara Smoked Salmon, Leek & Potato Thai Butternut Curry (vegan) Sausage Roll Spinach & Cheese Roll Beef Pasty Sweet Pie by the slice: Banoffee Blueberry Apple (vegan) Buttermilk & Raspberry Chocolate Chess Chocolate Raspberry Cream Coconut Cream Dutch Apple Lemon Meringue Mock Turtle (vegan) Sweet Treats: Anzac Biscuit Caramel Blondie Cherry Pecan Brownie Chocolate Peppermint Cookie Lamington Lemon Coconut Cake Mellowpuff Yo Yo As you can see, there are three categories in the menu each containing food items. A single, basic food item has a name, a designation if it’s suitable for vegan or other special diets, and a category (Savory, Sweet Pie, or Sweet Treat). In addition, Pop’s pies are so “pop”ular (get it?) that he would like a field to indicate if a Food Item is available or not, so that he can update the menu when an item is sold out. What data types comprise the fields in a FoodItem class? The other class that needs to be designed represents the menu of the day at Pop’s Pie Shop. The name of the Java class is Menu. The important characteristics of a Menu for Pop’s application are the three collections of FoodItems and the date. The menu to be displayed to customers is a list of all the available food items in each category, so the Menu class needs a method that will return a string representation of the current menu that includes the date. The PieShop class contains the main method and is responsible for reading in the text file that contains the list of food items for the menu. The name of the file is to be read as a command line argument. (Failure to use command line arguments will result in a severe point penalty.) The food items are listed in the text file one item per line. The line is formatted with the name, menu category, special diet information, and availability all separated by a colon (:).

For example:

Savory Mince & Cheese : savory : none : y

Blueberry Apple : sweet pie : vegan : y

Mellowpuff : sweet treat : none : n

The program will create FoodItem objects from the data in the text files and populate the day’s menu with those FoodItems that are available. (Failure to create these objects in your program will greatly reduce the number of points that you earn for the project.) Once this has been done, the program will display today’s menu with a header, the current date, and the three categories of FoodItems listed separately. After displaying the menu for the day, the program will provide the following options:

1 – Update an existing item on the menu

2 – Save the menu

3 – Close the program

For the first option, the program will prompt the user for the category and name of the item. If the item is included in the menu, then provide the option to change it from available to unavailable or from unavailable to available. Option 2 will save the current menu, including any changes made via option 1, to a file named menu.out.

Explanation / Answer

For Enums :

public enum FoodCatagory { Savory("savory"), Sweet_Pie("sweet pie"), Sweet_Treat("sweet treat");

private String value;
private FoodCatagory(String value) {
this.value=value;
}
}

For FoodItem class :

class FoodItem {

String name;

String designation;

enum FoodCatagory

boolean isAvailable;

FoodItem(String name, String designation, String category, String availability){

this.name = name;

this.designation = designation;

if(category.equals("savory")){

this.FoodCategory = FoodCategory.Savory;

}

else if(category.equals("sweet pie")){

this.FoodCategory = FoodCategory.Sweet_Pie;

}

else{

this.FoodCategory = FoodCategory.Sweet_Treat;

}

if(availibility.equals("y")){

this.isAvailable = true;

}

else{

this.isAvailable = false;

}

}

}

For Menu class :

class Menu{

Map<String, FoodItem> savory = new HashMap<String, FoodItem>();

Map<String, FoodItem> sweetPie = new HashMap<String, FoodItem>();

Map<String, FoodItem> sweetTreat = new HashMap<String, FoodItem>();

Date date = new Date();

public String displayMenu(){

String savories = "";

String sweetPies = "";

String sweetTreats = "";

for(Map.Entry m:savory.entrySet()){

savories.concat(m.getKey() + "/n");

}

for(Map.Entry m:sweetPie.entrySet()){

sweetPies.concat(m.getKey() + "/n");

}

for(Map.Entry m:sweetTreat.entrySet()){

sweetTreats.concat(m.getKey() + "/n");

}

return this.date.toString("yyyy-MM-dd HH:mm:ss") + "/n--------------------MENU--------------------/nSavory:-" + Savories + "/nSweet Pie:-/n" + sweetPies + "/nSweet Treat:-/n" + sweetTreats;

}

}

For PieShop class :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class PieShop {

public static void main(String[] args) {
Menu menu = new Menu();
String fileName = args[0]
FileReader freader = new FileReader(fileName);
BufferedReader br = new BufferedReader(freader);
String s;
while((s = br.readLine()) != null) {
String temp[] = s.split(":");
FoodItem f = new FoodItem(temp[0], temp[1], temp[2], temp[3]);
if(temp[1].equals("savory")){
menu.savory.put(temp[0], f);
}
else if(temp[1].equals("sweet pie")){
menu.sweetPie.put(temp[0], f);
}
else{
menu.sweetTreat.put(temp[0], f);
}
}
freader.close();
menu.dispalyMenu();

while(true){
System.out.println("Please choose your option:");
System.out.println("1 – Update an existing item on the menu/n2 – Save the menu/n3 – Close the program");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
if(choice==1){
FoodItem f;
System.out.println("Please choose category");
String category = sc.next();
System.out.println("Please choose name");
String name = sc.next();
if(category.equals("savory")){
f = (FoodItem) menu.savory.get(name);
}
else if(category.equals("sweet pie")){
f = (FoodItem) menu.sweetPie.get(name);
}
else{
f = (FoodItem) menu.sweetTreat.get(name);
}
if(f.isAvailable){
while(true){
System.out.println("Food Item is available. Please press 1 to change item to unavailable");
int c = sc.nextInt();
if(c==1){
f.isAvailable=false;
}
else{
System.out.println("Invalid choice.");
}
}
}
else{
while(true){
System.out.println("Food Item is unavailable. Please press 1 to change item to available");
int c = sc.nextInt();
if(c==1){

}
else{
System.out.println("Invalid choice.");
}
}
}
if(category.equals("savory")){
menu.savory.put(name, f);
}
else if(category.equals("sweet pie")){
menu.sweetPie.put(name, f);
}
else{
menu.sweetTreat.put(name, f);
}
}
else if(choice==2){
FoodItem fi;
String savory = "";
String sweetPie = "";
String sweetTreat = "";
try (BufferedWriter bw = new BufferedWriter(new FileWriter("menu.out"))) {
for(Map.Entry m:savory.entrySet()){
fi = m.getValue();
savory = savory + fi.name + ":" + fi.designation + ":" + fi.FoodCategory.value + ":" + Boolean.toString(fi.isAvailable) + "/n";
}
for(Map.Entry m:sweetPie.entrySet()){
fi = m.getValue();
sweetPie = sweetPie + fi.name + ":" + fi.designation + ":" + fi.FoodCategory.value + ":" + Boolean.toString(fi.isAvailable) + "/n";
}
for(Map.Entry m:sweetTreat.entrySet()){
fi = m.getValue();
sweetTreat = sweetTreat + fi.name + ":" + fi.designation + ":" + fi.FoodCategory.value + ":" + Boolean.toString(fi.isAvailable) + "/n";
}
String content = savory + sweetPie + sweetTreat;
bw.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
else if(choice==3){
System.exit();
}
else{
System.out.println("Invalid choice.");
}
}


}
}