JAVA You have been recently hired by a candy factory. They plan to install vendi
ID: 3599854 • Letter: J
Question
JAVA
You have been recently hired by a candy factory. They plan to install vending machines in different locations in UK. The vending machines are capable to produce candy's on the spot given the ingredients. Your job is to produce the software that controls how the candies are served to the customers. Your employer has specifically requested that the last candy should be the first to get served to the customer (i.e. the last made candy which is the freshest at the moment should be the first to get served to the customer). To implement such a software for the vending machine you’ve decided to use a stack (using either a fixed size array or a resizing-array, whichever you prefer). Implement such as stack and the operations required to keep track of the candies (hint: you might use numbers or letters to keep track of the candies.
Explanation / Answer
import java.io.*;
class Stack
{
private int array[];
private int top;
Stack()
{
array=new int[100];
top = -1;
}
public void push(int a)
{
if(top<99)
{
array[++top]=a;
}
else
{
System.out.println("Sorry more orders cannnot be processed.");
}
}
public void pop()
{
if(top==-1)
{
System.out.println("We dont have any other order to process.");
}
else
{
System.out.println("Processing order number "+array[top]);
top--;
}
}
public void display()
{
if(top==-1)
{
System.out.println("We don't have any order to display");
}
else
{
int i;
System.out.println("The list of orders goes as follows : ");
for(i=0;i<=top;i++)
{
System.out.println(array[i]);
}
}
}
}
public class Candies
{
public static void main(String args[])throws IOException
{
Stack obj;
obj=new Stack();
int ch,orderId;
orderId=1000;
ch=-1;
while(ch!=4)
{
System.out.println(" Welcome to the candy services 1: Place an order 2: Recieve the order 3: Display order list 4: Exit ");
System.out.print(" Please enter your choice : ");
BufferedReader p=new BufferedReader(new InputStreamReader(System.in));
ch=Integer.parseInt(p.readLine());
switch(ch)
{
case 1:
{
obj.push(orderId++);
break;
}
case 2:
{
obj.pop();
break;
}
case 4:
{
System.out.println("Thank You !");
break;
}
case 3:
{
obj.display();
break;
}
default:
{
System.out.println("Please enter a valid choice.");
break;
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.