Please answer in Java This project is to ease and hasten the ordering process in
ID: 3857788 • Letter: P
Question
Please answer in Java
This project is to ease and hasten the ordering process in a restaurant or a café.
The process that I propose is that these restaurants use a system, by inputting customer’s name, food, and the time the food will be done.
This process will reduce the people waiting in line, and quicken the ordering process. To do that, I will be using:
Queue Data Structure
MergeSort sorting method
The reason that I will be using Queue is because in an ordering process, people line up to order their food. The food will be on a first ready first out basis. I will be putting indexes to people’s order depending on if they order first, second, or so on. The MergeSort will sort by the time it takes for the food to get ready. Therefore, the food that takes the least time will go out first.
Include a driver to run the program
Explanation / Answer
The answer is as follows:
import java.io.*;
import java.util.*;
public class Order{
public String name;
public String food;
public int token;
public int time;
}
public class Queue{
public static final int MAX_ORDER = 100;
private Order[] order;
private int size;
public Queue(){
order = new Order[MAX_ORDER];
size = 0;
}
public int getSize(){
return size;
}
boolean isFull(){
if (size == MAX_ORDER)
return true;
else
return false;
}
public add(String name, String food,int token, int time){
if (isFull())
System.out.println("Order list is Full");
else{
order[size].token = token;
order[size].time = time;
order[size].name = name;
order[size].food = food
size = size + 1;
}
}
public remove(int index){
if (index == size-1){
size = size -1;
}
if (index < size-1 && index >=0){
for (int i=index; i < size-1; i++){
order[i].token = order[i+1].token;
order[i].time = order[i+1].time;
}
size = size -1;
}
}
public void merge(l,m,r){
int i,j,k;
int size1 = m - l + 1;
int size2 = r - m;
Order[] Order[size1];
Order[] two = new Order[size2];
for (i = 0; i < size1; i++)
one[i] = order[l + i];
for (j = 0; j < size2; j++)
two[j] = order[m + 1+ j];
i = 0;
j = 0;
k = l;
while (i < size1 && j < size2)
{
if (one[i] <= two[j])
{
order[k] = one[i];
i++;
}
else
{
order[k] = two[j];
j++;
}
k++;
}
while (i < size1)
{
order[k] = one[i];
i++;
k++;
}
while (j < size2)
{
order[k] = two[j];
j++;
k++;
}
}
public void mergesort(int l, int r){ // l is the start index and r is the last index
int m;
m = l+(r-1)/2;
mergesort(l,m);
mergesort(m+1,r);
merge(l,m,r);
}
public void display(){
for(int i = 0; i<size; i++){
System.out.println("Order token:" + orderlist[i].token + " Time:" + orderlist[i].time);
}
}
}
public class Demo {
public static void main(String args[]){
Queue orderlist = new Queue();
Random rand = new Random();
for (int i = 0; i<10; i++){
orderlist.add("Customer"+Integer.toString(i),"Food"+Integer.toString(i),i,rand.nextInt(20)+1); // random value for the completion time
}
display();
orderlist.mergesort(0,orderlist.getSize()-1);
display();
}
}