0

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
  • Q: So what exactly is your question? What exactly do you expect your code to do, that it isn't? – paulsm4 Oct 30 '20 at 23:17
  • It's not processing the cars again i.e. if a car leaves, one from the queue should enter the stack –  Oct 30 '20 at 23:21

2 Answers2

0

When you say "if a car is removed from the garage, another car from the queue is supposed to be inserted" - where is this logic in your departure() method? There isn't any code in that method that removes a car from the queue?

The behavior you're expecting is missing from your code.

Kevin Hooke
  • 2,583
  • 2
  • 19
  • 33
0

Your departure() does not call the arrival() method anywhere in its body, which would add the next car to the garage. When the departure() is successful (the return value is valid (not -1)) you know that a car has left the garage. Then you can simply use the next car from the queue and add it via the arrival() method. And it will fit in the garage because there is now room for the next car.

Progman
  • 16,827
  • 6
  • 33
  • 48
  • Not sure why that didnt compute for me. So something like this in the departure method: while true{ arrival(license)}? –  Oct 30 '20 at 23:32
  • @Dogwithastick You don't need a `while` loop. Only one car (at most) left the garage. – Progman Oct 30 '20 at 23:33
  • @Dogwithastick Try it. – Progman Oct 30 '20 at 23:40
  • it's close, but returning "W321RE, Y23456, X12345, null, B12GFT, null, Z23YTU". Tried remove instead of poll - get no such element exception –  Oct 30 '20 at 23:41
  • @Dogwithastick You should only add one car to the garage when there is actually a car in the queue. If there is none, then obviously you don't have to "arrive" anything. – Progman Oct 30 '20 at 23:45
  • Does not compute. Logically, it makes sense that if something departs, I want to push onto the stack a queue.peek(), which retrieves, but does not remove, the head of this queue. and then remove that element from the queue –  Oct 30 '20 at 23:47
  • @Dogwithastick You can use `pop()` for that, that does remove the element from the queue and returns the value which was removed. – Progman Oct 30 '20 at 23:49
  • stack.push(queue.pop());? that returns a no such element exception –  Oct 30 '20 at 23:50
  • @Dogwithastick Again, only do that when you have cars in the queue. That might not be always the case. – Progman Oct 30 '20 at 23:52
  • if(!queue.isEmpty()){ stack.push(queue.pop()); } –  Oct 30 '20 at 23:53
  • @Dogwithastick Close, you have to call the `arrival()` method, because it has additional logic (like increasing the counter). On that matter, you are currently not adjusting `numAdded` when a car departure. – Progman Oct 30 '20 at 23:54
  • 2
    @Dogwithastick You might want to use a debugger and use single steps to see how the program is executing your methods, see https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – Progman Oct 30 '20 at 23:59
  • I don't know what arrival needs to process exactly if it has already "arrived" –  Oct 31 '20 at 00:00
  • if(!queue.isEmpty()){ arrival(stack.push(queue.pop())); } Doesnt seem to work –  Oct 31 '20 at 00:05
  • @Dogwithastick Please see: [What Do You Mean “It Doesn't Work”?](https://meta.stackexchange.com/q/147616). Use a debugger as described in https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems to find the bug in your application. – Progman Oct 31 '20 at 15:55