My question is: why doesnt my program repeat itself when processing lines from a text file.
I am writing a class that manages a garage - cars in a garage are put into a stack of no more than 7, and if that is full, they go into a queue of no more than 5. There is method for adding cars manually and one for reading the input of a text file. I have also been given a stackInt class that I am asked to use while creating the stack object. It has only 4 methods, which makes things cumbersome :
public interface StackInt<E> {
E push(E obj);
E peek();
E pop();
boolean empty();
}
So I've had to write my code with ways to circumvent not having other useful stack methods. The majority of my methods work as intended: it reads the text file and process it, but it doesnt repeat the cycle of process if, for example, a car is removed from the garage, another car from the queue is supposed to be inserted. Here is my code:
import java.util.*;
import java.util.LinkedList;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
public class Garage{
public String line;
public String stackToString;
public int position;
public int numAdded;
LinkedList<String> queue = new LinkedList<>();
StackInt<String> stack = new LinkedStack<>();
public Garage(){
}
public Garage(String fileName){
try{
File file = new File(fileName);
Scanner scan = new Scanner(file);
while(scan.hasNextLine()) {
line = scan.nextLine();
String[] data = line.split(" ");
if (data[0].equals("a"))
arrival(data[1]);
else if (data[0].equals("d"))
departure(data[1]);
}
}
catch(FileNotFoundException e) {
System.out.println("File not found");
}
}
public boolean arrival(String license){
boolean added = false;
if(numAdded < 7){
stack.push(license);
added = true;
} else if (queue.size() < 5) {
added = queue.add(license);
numAdded--;
}
if(added){
numAdded++;
}
return true;
}
public int departure(String license){
Stack<String> temp = new Stack();
while(!stack.empty()){
temp.push(stack.pop());
}
position = temp.indexOf(license);
temp.remove(license);
while(!temp.isEmpty()){
stack.push(temp.pop());
}
return position;
}
public int numberParked(){
return numAdded;
}
public int numberWaiting(){
return queue.size();
}
public String toString(){
Stack<String> tempStack = new Stack();
while (!stack.empty()){
tempStack.push(stack.pop());
stackToString = tempStack.toString().replace("[", "").replace("]", "");
}
while (!tempStack.empty()){
stack.push(tempStack.pop());
}
return "Cars in Garage: " + stackToString + "\n" + "Cars waiting: " + (queue.toString().replace("[", "").replace("]", ""));
}
}
What I'm testing with is this:
public class GarageTest
{
public static void main (String [] args)
{
Garage g1 = new Garage("parking.txt");
System.out.println("Number parked: " + g1.numberParked());
System.out.println("Number waiting: " + g1.numberWaiting());
System.out.println("Parking WEB445 ... " + g1.arrival("WEB445"));
System.out.println("Parking BEA345 ... " + g1.arrival("BEA345"));
System.out.println(g1);
System.out.println("Z23YTU departs after " + g1.departure("B12GFT") + " car(s) moved");
System.out.println(g1);
}
}
Output is:
Number parked: 7
Number waiting: 5
Parking WEB445 ... true
Parking BEA345 ... true
Cars in Garage: Y23456, X12345, B12GFT, Z23YTU
Cars waiting: W321RE, CVBNMK, DFGHJK, ERTYUI, FGHJKL
Z23YTU departs after 2 car(s) moved
Cars in Garage: Y23456, X12345, Z23YTU
Cars waiting: W321RE, CVBNMK, DFGHJK, ERTYUI, FGHJKL
Whats expected on the "cars in garage" line is:
DFGHJK CVBNMK R23EWQ W321RE Y23456 X12345 B12GFT Z23YTU
with the queue on the next line. It's removing a few that have "d", but not filling the garage
afterward
The text file that is being processed (a for arrival, d for departure):
a A123TR
a Z23YTU
a Z23YTU
a ERW345
d ERW345
a B12GFT
d a23TR
a X12345
a Y23456
a W321RE
d R23EWQ
a CVBNMK
a DFGHJK
a ERTYUI
a FGHJKL
a GHJKL9
a HJKL98