Please Help with below JAVA Assignment//Also Please provide Screenshot of Output
ID: 3904285 • Letter: P
Question
Please Help with below JAVA Assignment//Also Please provide Screenshot of Output
Stack of "Sausage"
The objective of this assignment is to allow you to work with the Stack data structure and explore the idea of first-in, last-out (FILO) servicing.
Imagine that you're visiting a local all-you-can-eat breakfast bar; among the many items on the bar is a pan of sausages. Your task is to simulate the sausage pan. Note that when new sausages are added to the pan, they're always added to the top (the worker just dumps new sausages on top of the old sausages); also note that whenever someone takes a sausage to eat, they pick the top-most sausage. Thus we've got a stack (first-in, last-out) and the bottom-most sausage is always the oldest. For sake of simplicity, imagine that the pan only allows you to select a "top" sausage (i.e. you can't pick among many).
Your task is to utilize a stack collection (the Stack or Vectorclass is appropriate). Your program should allow the user to add a sausage, remove a sausage, and print the status of the stack. The stack should store the time in which the sausage was added; and when a sausage is removed, the program should display how long the sausage "lived" in the pan (i.e. the difference from now to when it was added to the pan).
Important items to consider:
(1) Don't allow the user to remove (pop) a sausage if the pan is empty
(2) Displaying the status of the stack should show how many sausages are in the stack and the "age" of the topmost sausage
(3) Use the Stack or Vector class to make your job a LOT easier (otherwise, you must implement the stack class yourself).
(4) To get the time in Java, import java.util.* and then make use of the Date class
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// SausageStack.java
import java.util.Iterator;
import java.util.Scanner;
import java.util.Stack;
public class SausageStack {
// Stack of sausage
static Stack<Sausage> sausages;
// scanner to read input
static Scanner scanner;
/**
* method to add a sausage to the stack
*/
static void addSausage() {
System.out.println("Enter sausage name: ");
String name = scanner.nextLine();
Sausage sausage = new Sausage(name);
sausages.push(sausage);
}
/**
* method to remove the top sausage from the stack
*/
static void removeSausage() {
if (sausages.isEmpty()) {
System.out.println("Sausage stack is empty!");
} else {
Sausage sausage = sausages.pop();
// calculating the time in seconds,in which the sausage has lived
long currentTime = System.currentTimeMillis();
double liveTime = (currentTime - sausage.createdTime) / 1000.0;
System.out.println(sausage.name
+ " is removed from the stack. It has lived for "
+ liveTime + " seconds.");
}
}
/**
* method to display the sausage stack
*/
static void printStatus() {
Iterator<Sausage> iterator = sausages.iterator();
System.out.println("Current Stack: ");
while (iterator.hasNext()) {
System.out.print(iterator.next().name);
if (iterator.hasNext()) {
System.out.print("->");
}
}
System.out.println();
}
/**
* method to display the menu
*/
static void showMenu() {
System.out.println("1. Add sausage");
System.out.println("2. Remove sausage");
System.out.println("3. Print status");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
}
public static void main(String[] args) {
sausages = new Stack<Sausage>();
scanner = new Scanner(System.in);
int choice = 0;
// looping until user wishes to quit
while (choice != 4) {
showMenu();
choice = Integer.parseInt(scanner.nextLine());
// performing operations based on choice
switch (choice) {
case 1:
addSausage();
break;
case 2:
removeSausage();
break;
case 3:
printStatus();
break;
case 4:
System.out.println("Bye!");
break;
default:
System.out.println("Invalid choice");
break;
}
}
}
}
/**
* an inner class defined within SausageStack.java which represents a single
* sausage
*
*/
class Sausage {
long createdTime; // time in milliseconds in which the sausage is created
String name;//name of sausage
public Sausage(String name) {
this.name = name;
//setting created time as current time in millis
createdTime = System.currentTimeMillis();
}
}
/*OUTPUT*/
1. Add sausage
2. Remove sausage
3. Print status
4. Exit
Enter your choice: 1
Enter sausage name:
Sausage1
1. Add sausage
2. Remove sausage
3. Print status
4. Exit
Enter your choice: 1
Enter sausage name:
Sausage2
1. Add sausage
2. Remove sausage
3. Print status
4. Exit
Enter your choice: 3
Current Stack:
Sausage1->Sausage2
1. Add sausage
2. Remove sausage
3. Print status
4. Exit
Enter your choice: 1
Enter sausage name:
Sausage3
1. Add sausage
2. Remove sausage
3. Print status
4. Exit
Enter your choice: 3
Current Stack:
Sausage1->Sausage2->Sausage3
1. Add sausage
2. Remove sausage
3. Print status
4. Exit
Enter your choice: 2
Sausage3 is removed from the stack. It has lived for 7.405 seconds.
1. Add sausage
2. Remove sausage
3. Print status
4. Exit
Enter your choice: 2
Sausage2 is removed from the stack. It has lived for 24.233 seconds.
1. Add sausage
2. Remove sausage
3. Print status
4. Exit
Enter your choice: 2
Sausage1 is removed from the stack. It has lived for 34.816 seconds.
1. Add sausage
2. Remove sausage
3. Print status
4. Exit
Enter your choice: 2
Sausage stack is empty!
1. Add sausage
2. Remove sausage
3. Print status
4. Exit
Enter your choice: 4
Bye!