Using Java, please write the code for the following program as simply as possibl
ID: 3808018 • Letter: U
Question
Using Java, please write the code for the following program as simply as possible. Please make sure to follow the prompt exactly Use of collections is NOT ALLOWED. Comments are appreciated. Thank you!
A. Objectives 1. Implement and use Stack class and methods (20 points 2. Use a separate class to represent Objects for Stack (10 points) 3. Write additional methods to search through a Stack (10 points) 4. Use methods to do separate and repetitive work. The main method should not have more than 20 lines of code (10 points 5. Implement the project correctly. (40 points) 10 points will be awarded for use of meaningful identifiers, consistent indentation, explanatory comments in your code and properly formatted output. B. Description For a variety of reasons, human beings track the food they intake. Assume you are working for a healthcare analytics company, and need to gather information about the eating habits of your clients. You need to create a software that helps in doing your work. For this purpose, yo write a meni driven program with the following options: 1. Register client and create client login (clicnt supplies the clientID and password, and the clientID must be unique 2. Login existing client 3. Add a food item client ate 4. Check the complete history of food a client ate in a day in reverse order (all food items listed here from dinner to breakfast 5. Check all the different types of food items a client ate in a day (all food items listed here only once 6. Total calories eaten in a day 7. Food with the maximum calorie in a day 8. Food that the client ate maximum number of times in a day 9. LogoutExplanation / Answer
Solution:
Implemented first and second objective:
Stack.java
package foodtrack;
import java.util.Arrays;
public class Stack {
private Food[] fd= new Food[10];
private int top = 0;
public void push( Food f )
{
if (top == fd.length)
{
fd = Arrays.copyOf( fd, 2*fd.length );
}
fd[top] = f;
top++;
}
public Food pop()
{
if ( top == 0 )
throw new IllegalStateException("Can't pop from an empty stack.");
Food f = fd[top - 1];
top--;
return f;
}
public boolean isEmpty() {
return (top == 0);
}
public boolean search(Food f)
{
if(this.isEmpty())
{
return false;
}
else
{
for(int i=0;i<fd.length;i++ )
{
if(fd[i].Name==f.Name)
{
return true;
}
}
return false;
}
}
}
Food.java
package foodtrack;
public class Food {
String Name;
String Quantity;
float Calorie;
String Meal_type;
Food()
{}
}
FoodTrack.java
package foodtrack;
import java.util.ArrayList;
import java.util.Scanner;
public class FoodTrack {
static ArrayList username=new ArrayList();
static ArrayList password=new ArrayList();
public static void menu()
{
System.out.println("1: Register");
System.out.println("2: Login");
System.out.println("3: Logout");
}
public static void Register()
{
Scanner sc=new Scanner(System.in);
boolean correct=false;
String user="";
while(!correct)
{
System.out.print("Enter username: ");
user=sc.next();
if(username.isEmpty())
break;
for(int i=0;i<username.size();i++)
{
if(username.get(i)==user)
{
correct=false;
break;
}
else
correct=true;
}
}
System.out.println();
System.out.print("Enter password: ");
String pass=sc.next();
username.add(user);
password.add(pass);
}
public static void Login()
{
Scanner sc=new Scanner(System.in);
String user;
System.out.print("Enter username: ");
user=sc.next();
System.out.println();
boolean correct=false;
while(!correct)
{
System.out.print("Enter password: ");
String pass=sc.next();
for(int i=0;i<username.size();i++)
{
if(username.get(i)==user)
{
if( password.get(i)==pass)
{
correct=true;
break;
}
else
correct=false;
}
else
correct=false;
}
}
}
public static void choice(int ch)
{
switch(ch)
{
case 1:Register();
break;
case 2:Login();
break;
case 3:System.exit(0);
break;
}
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
while(true)
{
menu();
System.out.println("Enter the choice");
int ch=sc.nextInt();
choice(ch);
}
}
}
Output:
run:
1: Register
2: Login
3: Logout
Enter the choice
1
Enter username: Jack
Enter password: Jill
1: Register
2: Login
3: Logout
Enter the choice
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.