In Java, please write the COMPLETE code for the prompt in the following TWO pict
ID: 3864750 • Letter: I
Question
In Java, please write the COMPLETE code for the prompt in the following TWO pictures.
ANSWER WITH THE COMPLETE PROGRAM OR DONT ANSWER AT ALL!!!!!!!
-Please write the program as simply as possible.
-Please read and follow the prompt closely
-THE STEP-BY-STEP INPUT/OUTPUT OF THE PROGRAM IS LOCATED IN THE FIRST PICTURE! Once again, READ THE PICTURES! This is what I mean by "Read and follow the prompt closely"!
-Your program MUST implement and use STACK class methods. That means there must be peek(), push(), pop(), and remove() methods that interact with the stack.
-We have not learned hashmap yet, so please DONT use hashmap.
-DO NOT USE COLLECTIONS.
-MUST use a separate class to represent Objects for Stack.
-MUST search through Stack at some point.
All of the details of the program can be found in the following pictures. PLEASE DON'T WASTE MY QUESTION ALLOWANCE WITH PARTIAL CODE OR CODE THAT DOES NOT FOLLOW THESE DIRECTIONS. This is the EIGHTH time posting this question and I'm running out, BECAUSE PEOPLE ARENT READING THESE DIRECTIONS. READ THE DIRECTIONS!!!
THANK YOU!
Explanation / Answer
FoodItem.java:
public class FoodItem {
public enum MealType {
BREAKFAST, LUNCH, SNACKS, DINNER
}
private String name;
private int quantity, calorie;
private MealType mealType;
public FoodItem(String name, int quantity, int calorie, MealType mealType) {
this.name = name;
this.quantity = quantity;
this.calorie = calorie;
this.mealType = mealType;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
public int getCalorie() {
return calorie;
}
public MealType getMealType() {
return mealType;
}
public String toString() {
return "Food: " + name + " Quantity: " + quantity + " Meal Type: " + mealType;
}
@Override
public boolean equals(Object item) {
if(item instanceof FoodItem) {
FoodItem f = (FoodItem) item;
if(f.getName().equals(name))
return true;
}
return false;
}
}
Client.java:
import java.util.Stack;
public class Client {
private String clientId;
private String password;
private String firstName;
private String lastName;
private Stack<FoodItem> itemsAte;
public Client(String clientId, String password, String firstName,
String lastName) {
this.clientId = clientId;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
itemsAte = new Stack<>();
}
public String getClientId() {
return clientId;
}
public String getPassword() {
return password;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String toString() {
return "FirstName: " + firstName + " LastName: " + lastName + " ClientId: " + clientId;
}
public void addItem(FoodItem item) {
itemsAte.push(item);
}
public Stack<FoodItem> getItems() {
return itemsAte;
}
}
FoodDemo.java:
import java.security.SecureRandom;
import java.util.Scanner;
import java.util.Stack;
public class FoodDemo {
public static Scanner scanner = new Scanner(System.in);
// to store maximum 100 clients
public static Client clients[] = new Client[100];
public static int no_of_clients = 0;
public static int getChoice(String s, int min, int max) {
int choice = 0;
System.out.println(s);
String error = "";
while (choice < min || choice > max) {
System.out.print(error);
try {
choice = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("Invalid Choice");
}
error = "Invalid Choice. ";
}
return choice;
}
public static String getMenu() {
StringBuilder sb = new StringBuilder();
sb.append("+++++++++++++++ Menu +++++++++++++++++ ");
sb.append("1. Register ");
sb.append("2. Login ");
sb.append("3. Add a food item that you ate ");
sb.append("4. History of items eaten in a day ");
sb.append("5. List of diffrent items eaten in a day ");
sb.append("6. Total Calorie Eaten in a day ");
sb.append("7. Food with max calorie in a day ");
sb.append("8. Food eaten maximum no of times in a day ");
sb.append("9. Log out ");
return sb.toString();
}
public static void main(String args[]) {
String menu = getMenu();
int choice = getChoice(menu, 1, 9);
Client loggedUser = null;
while (choice != 9) {
switch (choice) {
case 1:
if(loggedUser==null)
register();
break;
case 2:
if(loggedUser==null) {
loggedUser = login();
if(loggedUser != null)
System.out.println("Login Successful. Welcome " + loggedUser.getFirstName() + "!! ");
else
System.out.println("ClienId / Passowrd doesn't match or exist. ");
} else {
System.out.println("You are already logged in. Please Log out. ");
}
break;
case 3:
if(loggedUser!=null) {
logFoodItemForClient(loggedUser);
} else {
System.out.println("Please login. ");
}
break;
case 4:
if(loggedUser!=null) {
showDietHistory(loggedUser);
} else {
System.out.println("Please login. ");
}
break;
case 5:
if(loggedUser!=null) {
showDiffrentItems(loggedUser);
} else {
System.out.println("Please login. ");
}
break;
case 6:
if(loggedUser!=null) {
calculateTotalCalories(loggedUser);
} else {
System.out.println("Please login. ");
}
break;
case 7:
if(loggedUser!=null) {
findMaxCalorieItem(loggedUser);
} else {
System.out.println("Please login. ");
}
break;
case 8:
if(loggedUser!=null) {
findMaxEatenItem(loggedUser);
} else {
System.out.println("Please login. ");
}
break;
case 9:
if(loggedUser!=null) {
loggedUser = null;
System.out.println("Logged Out Successfully. ");
} else {
System.out.println("Please login. ");
}
break;
}
choice = getChoice(menu, 1, 9);
}
}
private static void findMaxEatenItem(Client loggedUser) {
FoodItem[] itemArray = getItemArray(loggedUser);
System.out.println("++++++++++++++++ List of Different Items in a day ++++++++++++++");
FoodItem maxCountFood = null;
int maxCount = 0;
for(int i=0; i< itemArray.length; i++) {
int count = 0;
for(int j=0; j<itemArray.length; j++) {
if(itemArray[j].equals(itemArray[i])) {
count++;
}
}
if(count > maxCount) {
maxCount = count;
maxCountFood = itemArray[i];
}
}
if(maxCount > 0) {
System.out.println("You ate " + maxCount + " times: " + maxCountFood + " " );
}
}
private static void findMaxCalorieItem(Client loggedUser) {
FoodItem[] itemArray = getItemArray(loggedUser);
if(itemArray.length > 0) {
FoodItem maxCalorieItem = itemArray[0];
for(FoodItem f: itemArray) {
if(maxCalorieItem.getCalorie() < f.getCalorie())
maxCalorieItem = f;
}
System.out.println("The max calorie item you ate is: " + maxCalorieItem + " ");
}
}
private static void calculateTotalCalories(Client loggedUser) {
FoodItem[] itemArray = getItemArray(loggedUser);
int total = 0;
for(FoodItem f: itemArray) {
total += f.getCalorie();
}
System.out.println("You ate " + total + " calories in a day. ");
}
private static void showDiffrentItems(Client loggedUser) {
FoodItem[] itemArray = getItemArray(loggedUser);
System.out.println("++++++++++++++++ List of Different Items in a day ++++++++++++++");
for(int i=0; i< itemArray.length; i++) {
boolean newItem = true;
for(int j=0; j<i; j++) {
if(itemArray[j].equals(itemArray[i])) {
newItem = false;
break;
}
}
if(newItem) {
System.out.println(itemArray[i]);
}
}
System.out.println("");
}
private static void showDietHistory(Client loggedUser) {
FoodItem itemArray[] = getItemArray(loggedUser);
System.out.println("++++++++++++++ Diet History ++++++++++++++++");
for(FoodItem item: itemArray) {
System.out.println(item);
}
System.out.println("");
}
private static FoodItem[] getItemArray(Client loggedUser) {
Stack<FoodItem> itemsStack = loggedUser.getItems();
FoodItem[] items = new FoodItem[itemsStack.size()];
Object itemArray[] = itemsStack.toArray();
int i=0;
for(Object o: itemArray) {
items[i++] = (FoodItem) o;
}
return items;
}
private static void logFoodItemForClient(Client loggedUser) {
char choice = 'y';
while(choice == 'y' || choice == 'Y') {
System.out.print("Enter Name of food: ");
String name = scanner.nextLine();
System.out.print("Enter Quantity: ");
int quantity = Integer.parseInt(scanner.nextLine());
System.out.print("Enter Calories: ");
int calories = Integer.parseInt(scanner.nextLine());
FoodItem.MealType foodType = null;
String s = "Select Meal Type: 1. BreakFast 2. Lunch 3. Snacks 4. Dinner";
int type = getChoice(s, 1, 4);
if(type==1)
foodType = FoodItem.MealType.BREAKFAST;
if(type==2)
foodType = FoodItem.MealType.LUNCH;
if(type==3)
foodType = FoodItem.MealType.SNACKS;
if(type==3)
foodType = FoodItem.MealType.DINNER;
FoodItem foodItem = new FoodItem(name, quantity, calories, foodType);
loggedUser.addItem(foodItem);
System.out.println("Food registered successfully.");
System.out.println(foodItem + " ");
System.out.print("Do you want to enter more items(y/n): ");
choice = scanner.nextLine().charAt(0);
}
}
private static Client login() {
System.out.print("Enter clientId: ");
String clientId = scanner.nextLine();
System.out.print("Enter Password: ");
String password = scanner.nextLine();
return loginClient(clientId, password);
}
private static void register() {
System.out.print("Enter first name: ");
String firstName = scanner.nextLine();
System.out.print("Enter last name: ");
String lastName = scanner.nextLine();
System.out.print("Enter clientId: ");
String clientId = scanner.nextLine();
int counter = 1;
while(doesCLientIdExists(clientId)) {
if(counter==3) {
clientId = randomClientId(lastName);
break;
}
System.out.print("Client Id exist. Enter clientId again: ");
clientId = scanner.nextLine();
counter++;
}
System.out.print("Enter Password: ");
String password = scanner.nextLine();
while(password.trim().length() < 8) {
System.out.println("Password is less than 8 characters: Enter Again: ");
password = scanner.nextLine();
}
Client c = new Client(clientId, password, firstName, lastName);
// enter this client into our list of clients
clients[no_of_clients++] = c;
System.out.println("You are registered successfully. Details:");
System.out.println(c + " ");
}
private static String randomClientId(String lastName) {
SecureRandom rnd = new SecureRandom();
// to make alphanumeric clientId
lastName = lastName + "0123456789";
StringBuilder sb = new StringBuilder(8);
for (int i = 0; i < 8; i++)
sb.append(lastName.charAt(rnd.nextInt(lastName.length())));
return sb.toString();
}
private static boolean doesCLientIdExists(String clientId) {
for (Client c : clients) {
if (c != null) {
if (c.getClientId().equals(clientId))
return true;
}
}
return false;
}
private static Client loginClient(String clientId, String password) {
for (Client c : clients) {
if (c != null) {
if (c.getClientId().equals(clientId) && c.getPassword().equals(password))
return c;
}
}
return null;
}
}
Sample Output:
+++++++++++++++ Menu +++++++++++++++++
1. Register
2. Login
3. Add a food item that you ate
4. History of items eaten in a day
5. List of diffrent items eaten in a day
6. Total Calorie Eaten in a day
7. Food with max calorie in a day
8. Food eaten maximum no of times in a day
9. Log out
1
Enter first name: A
Enter last name: B
Enter clientId: AB
Enter Password: 12345678
You are registered successfully. Details:
FirstName: A
LastName: B
ClientId: AB
+++++++++++++++ Menu +++++++++++++++++
1. Register
2. Login
3. Add a food item that you ate
4. History of items eaten in a day
5. List of diffrent items eaten in a day
6. Total Calorie Eaten in a day
7. Food with max calorie in a day
8. Food eaten maximum no of times in a day
9. Log out
2
Enter clientId: AB
Enter Password: 12345678
Login Successful. Welcome A!!
+++++++++++++++ Menu +++++++++++++++++
1. Register
2. Login
3. Add a food item that you ate
4. History of items eaten in a day
5. List of diffrent items eaten in a day
6. Total Calorie Eaten in a day
7. Food with max calorie in a day
8. Food eaten maximum no of times in a day
9. Log out
3
Enter Name of food: Food1
Enter Quantity: 3
Enter Calories: 23
Select Meal Type:
1. BreakFast
2. Lunch
3. Snacks
4. Dinner
1
Food registered successfully.
Food: Food1
Quantity: 3
Meal Type: BREAKFAST
Do you want to enter more items(y/n): y
Enter Name of food: Food2
Enter Quantity: 4
Enter Calories: 84
Select Meal Type:
1. BreakFast
2. Lunch
3. Snacks
4. Dinner
3
Food registered successfully.
Food: Food2
Quantity: 4
Meal Type: DINNER
Do you want to enter more items(y/n): y
Enter Name of food: Food1
Enter Quantity: 4
Enter Calories: 23
Select Meal Type:
1. BreakFast
2. Lunch
3. Snacks
4. Dinner
2
Food registered successfully.
Food: Food1
Quantity: 4
Meal Type: LUNCH
Do you want to enter more items(y/n): Food3
+++++++++++++++ Menu +++++++++++++++++
1. Register
2. Login
3. Add a food item that you ate
4. History of items eaten in a day
5. List of diffrent items eaten in a day
6. Total Calorie Eaten in a day
7. Food with max calorie in a day
8. Food eaten maximum no of times in a day
9. Log out
4
++++++++++++++ Diet History ++++++++++++++++
Food: Food1
Quantity: 3
Meal Type: BREAKFAST
Food: Food2
Quantity: 4
Meal Type: DINNER
Food: Food1
Quantity: 4
Meal Type: LUNCH
+++++++++++++++ Menu +++++++++++++++++
1. Register
2. Login
3. Add a food item that you ate
4. History of items eaten in a day
5. List of diffrent items eaten in a day
6. Total Calorie Eaten in a day
7. Food with max calorie in a day
8. Food eaten maximum no of times in a day
9. Log out
6
You ate 130 calories in a day.
+++++++++++++++ Menu +++++++++++++++++
1. Register
2. Login
3. Add a food item that you ate
4. History of items eaten in a day
5. List of diffrent items eaten in a day
6. Total Calorie Eaten in a day
7. Food with max calorie in a day
8. Food eaten maximum no of times in a day
9. Log out
5
++++++++++++++++ List of Different Items in a day ++++++++++++++
Food: Food1
Quantity: 3
Meal Type: BREAKFAST
Food: Food2
Quantity: 4
Meal Type: DINNER
+++++++++++++++ Menu +++++++++++++++++
1. Register
2. Login
3. Add a food item that you ate
4. History of items eaten in a day
5. List of diffrent items eaten in a day
6. Total Calorie Eaten in a day
7. Food with max calorie in a day
8. Food eaten maximum no of times in a day
9. Log out
7
The max calorie item you ate is: Food: Food2
Quantity: 4
Meal Type: DINNER
+++++++++++++++ Menu +++++++++++++++++
1. Register
2. Login
3. Add a food item that you ate
4. History of items eaten in a day
5. List of diffrent items eaten in a day
6. Total Calorie Eaten in a day
7. Food with max calorie in a day
8. Food eaten maximum no of times in a day
9. Log out
8
++++++++++++++++ List of Different Items in a day ++++++++++++++
You ate 2 times: Food: Food1
Quantity: 3
Meal Type: BREAKFAST
+++++++++++++++ Menu +++++++++++++++++
1. Register
2. Login
3. Add a food item that you ate
4. History of items eaten in a day
5. List of diffrent items eaten in a day
6. Total Calorie Eaten in a day
7. Food with max calorie in a day
8. Food eaten maximum no of times in a day
9. Log out
9
I could not put much comments into the code, becayse question is quite lengthy and we get just 2 hours to complete. in case, you don't understand something, feel free to write in comments.
Hope you got your answer correct this time. your question is too long buddy.. it took me almost 2 hours to complete, may be that is the reason, many persons may have given you incomplete answers. my suggestion would be to post small questions if possible. Yeah i understand, there are times, you can't do much.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.