I am trying to make a ferry simulator with one ferry that works like a stack and another like a queue. I have a working class for stacks and queues with pop/push; I am trying to figure out how to simply this code for the ferry class.
import java.util.Scanner;
public class Ferry {
static Stack ferry1 = new Stack();
static Queue ferry2 = new Queue();
Stack ferryStack = new Stack();
static int car1, car2, car3, car4, car5, car6, car7, car8, car9, car10, car11, car12, car13, car14, car15, car16, car17, car18, car19, car20;
static int getTag(){
Scanner tag = new Scanner(System.in);
int carID;
System.out.print("Enter in a tag number.");
carID = tag.nextInt();
return carID;
}
public static Queue ferry2Loading(){
car1 = getTag();
car2 = getTag();
car3 = getTag();
car4 = getTag();
car5 = getTag();
car6 = getTag();
car7 = getTag();
car8 = getTag();
car9 = getTag();
car10 = getTag();
car11 = getTag();
car12 = getTag();
car13 = getTag();
car14 = getTag();
car15 = getTag();
car16 = getTag();
car17 = getTag();
car18 = getTag();
car19 = getTag();
car20 = getTag();
ferry2.queuePush("car1");
ferry2.queuePush("car2");
ferry2.queuePush("car3");
ferry2.queuePush("car4");
ferry2.queuePush("car5");
ferry2.queuePush("car6");
ferry2.queuePush("car7");
ferry2.queuePush("car8");
ferry2.queuePush("car9");
ferry2.queuePush("car10");
ferry2.queuePush("car11");
ferry2.queuePush("car12");
ferry2.queuePush("car13");
ferry2.queuePush("car14");
ferry2.queuePush("car15");
ferry2.queuePush("car16");
ferry2.queuePush("car17");
ferry2.queuePush("car18");
ferry2.queuePush("car19");
ferry2.queuePush("car20");
return ferry2;
}
public static Stack ferry1Loading(){
car1 = getTag();
car2 = getTag();
car3 = getTag();
car4 = getTag();
car5 = getTag();
car6 = getTag();
car7 = getTag();
car8 = getTag();
car9 = getTag();
car10 = getTag();
car11 = getTag();
car12 = getTag();
car13 = getTag();
car14 = getTag();
car15 = getTag();
car16 = getTag();
car17 = getTag();
car18 = getTag();
car19 = getTag();
car20 = getTag();
ferry1.stackPush(car1);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car2);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car3);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car4);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car5);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car6);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car7);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car8);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car9);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car10);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car11);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car12);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car13);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car14);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car15);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car16);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car17);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car18);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car19);
System.out.println(ferry1.stackPeek());
ferry1.stackPush(car20);
System.out.println(ferry1.stackPeek());
return ferry1;
}
public static void main(String args[]){
ferry1Loading();
ferry2Loading();
//System.out.println("CARS ON THE FERRY "+ferry1+"");
//ferry1.push("s5gsg");
//
// Stack ferryStack = new Stack(20);
// System.out.println(ferryStack.peek());
// ferryStack.push("2b");
// System.out.println(ferryStack.peek());
// ferryStack.push("665sg");
// System.out.println(ferryStack.peek());
// ferryStack.push("12sgs");
// System.out.println(ferryStack.peek());
// ferryStack.push("3sgsg2");
// System.out.println(ferryStack.peek());
// ferryStack.pop();
// ferryStack.push("2sg0");
// System.out.println(ferryStack.peek());
}
}
Stack Code:
public class Stack {
int maxSize;
int top;
String arr[];
public Stack(){
int n = 0;
maxSize = n;
arr = new String[maxSize];
top = 0;
}
public void stackPush(String str){
if(top < maxSize){
arr[top] = str;
top++;
}else{
System.out.println("Error Stack Overflow!!");
}
}
public String stackPop(){
if(!this.empty()){
String popped = this.peek();
arr[top-1] = null;
top--;
return popped;
}else{
return null;
}
}
public String peek(){
if(top > 0){
return arr[top-1];
}else{
return null;
}
}
public boolean empty(){
if (top == 0){
return true;
}else{
return false;
}
}
}
Queue Code:
public class Queue {
int maxSize;
int top;
int rear;
String arr[];
public Queue(){
int n = 0;
maxSize = n ;
arr = new String[maxSize];
top = -1;
rear = 0;
}
public void queuePush(String cars){
if(top < maxSize - 1){
top++;
arr[top] = cars;
//display();
// }else{
// System.out.println("Error Queue Overflow!!");
}
}
public void queuePop(){
if(top >= rear){
rear++;
//display();
}else{
System.out.println("Error");
}
//}
//public void display() {
// if (top >= rear) {
// System.out.println("Elements in Queue : ");
// for (int i = rear; i <= top; i++) {
// System.out.println(arr[i]);
// }
// }
// }
//
// public static void main(String[] args) {
// Queue queueDemo = new Queue(20);
// queueDemo.pop();
// queueDemo.push("23");
// queueDemo.push("2");
// queueDemo.push("73");
// queueDemo.push("21");
// queueDemo.pop();
// queueDemo.pop();
// queueDemo.pop();
// queueDemo.pop();
// }
}
}
Stack
storeString
whileQueue
storeint
? \$\endgroup\$