import java.io.*;
public class RentACar
{
  public static void main(String [] args)
  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    try{
      System.out.println("Hello! Are you looking to rent a car? Yes or No");
      String decision = br.readLine();
      boolean multiple = false;
      
      if(decision.equalsIgnoreCase("no"))
        System.exit(0);
      
      System.out.println("Do you want to choose your car? Yes or No");
      decision = br.readLine();
      
      boolean custom = false;
      
      if(decision.equalsIgnoreCase("yes")){
        custom = true;
      }
      
      Car myCar = rent(custom);
      Car nextCar = null;
      
      System.out.println("Congratulations!");
      System.out.println("Car 1: "+myCar);
      if( multiple ){
        System.out.println("Car 2: "+nextCar);
      }
      
      decision = "none";
      
      while(true){
        System.out.println("Please choose from the following choices:");
        System.out.println("Done -- if you wish to return the car");
        System.out.println("Drive -- to let us know how many miles you have driven");
        System.out.println("Driven -- to find out how many miles you have driven");
        System.out.println("Fill -- to put gas in your tank");
        System.out.println("Gas -- to find out how many gallons of gas are left in your tank");
        System.out.println("MPG -- to find out how many miles per gallon your car drives");
        System.out.println("Doors -- to exchange your car for one with a different number of doors");
        System.out.println("Color -- to exchange your car for one with a different color");
        System.out.println("Specs -- if you wish to see the specifications of your car");
        System.out.println("Music -- to change your preset stations");
        System.out.println("Rent -- to rent another car");
        
        decision = br.readLine();
        
        if( decision.equalsIgnoreCase("drive") ){
          System.out.println("How many miles have you driven car 1 since the last time you told us?");
          int miles = (new Integer(br.readLine())).intValue();
          boolean outOfGas = myCar.addMiles(miles);
          if( outOfGas ){
            break;
          }
          
          if( multiple ){
            System.out.println("How many miles have you driven car 2 since the last time your told us?");
            miles = (new Integer(br.readLine())).intValue();
            outOfGas = nextCar.addMiles(miles);
            if( outOfGas ){
              break;
            }            
          }
        }
        
        else if( decision.equals("done") ){
            System.out.println("Do you wish to rent another car? Yes or No");
            String done = br.readLine();
            
            if( done.equalsIgnoreCase("yes") )
              myCar = rent(custom);
            else{
              if( myCar != null ){
                System.out.println("Do you wish to keep your other car? Yes or No");
                done = br.readLine();
                if (done.equalsIgnoreCase("yes") ){
                  myCar = nextCar;
                  nextCar = null;
                  multiple = false;

                }
                else 
                  break;
              }  
            }
        }
        
        else if( decision.equalsIgnoreCase("driven") ){
          System.out.println("Car 1 has4 driven "+myCar.getMiles()+" miles so far.\n");
          if( multiple ){
            System.out.println("Car 2 has driven "+nextCar.getMiles()+" miles so far.fill\n");
          }
        }
        
        else if( decision.equalsIgnoreCase("fill") ){
          System.out.println("How many gallons do you wish to put in car 1?");
          int gallons = (new Integer(br.readLine())).intValue();
          myCar.addGas(gallons);
          
          if( multiple ){
            System.out.println("How many gallons do you wish to put in car 2?");
            gallons = (new Integer(br.readLine())).intValue();
            nextCar.addGas(gallons);      
          }
        }
        
        else if( decision.equalsIgnoreCase("gas") ){
          System.out.println("Car 1 has "+myCar.getGas()+" gallons left.\n");
          if( multiple ){
            System.out.println("Car 2 has "+nextCar.getGas()+" gallons left.\n");
          }
        }
        
        else if( decision.equalsIgnoreCase("mpg") ){
          System.out.println("Car 1 does "+myCar.getMPG()+" miles per gallon.\n");
          if( multiple ){
            System.out.println("Car 2 does "+nextCar.getMPG()+" miles per gallon.\n");
          }
        } 
        
        else if( decision.equalsIgnoreCase("doors") ){
          System.out.println("Car 1 has "+myCar.getDoors()+" doors.\n");
          if( multiple ){
            System.out.println("Car 2 has "+nextCar.getDoors()+" doors.\n");
          }
        }
        
        else if( decision.equalsIgnoreCase("color") ){
          System.out.println("Car 1 is "+myCar.getColor()+".\n");
          if( multiple ){
            System.out.println("Car 2 is "+nextCar.getColor()+".\n");
          }
        }
        
        else if( decision.equalsIgnoreCase("specs") ){
          System.out.println("Car 1:\n"+myCar);
          if( multiple ){
            System.out.println("Car 2:\n"+nextCar);
          }
        }
        
        else if( decision.equalsIgnoreCase("music") ){
          System.out.println("Your current stations are: "+myCar.getPlaylist());
          int station = 0;
          while( station < 1 || station > 5 ){
            System.out.println("Which station do you wish to change?");
            station = (new Integer(br.readLine())).intValue();
          }
          
          System.out.println("What do you wish to change it to?");
          System.out.println("0 -- Rock");
          System.out.println("1 -- Hip Hop");
          System.out.println("2 -- Jazz");
          System.out.println("3 -- Rap");
          System.out.println("4 -- R&B");
          System.out.println("5 -- Alternative");
          System.out.println("6 -- Dance");
          System.out.println("7 -- Instrumental");
          System.out.println("8 -- Techno");
          System.out.println("9 -- News Radio");
          
          int choice = (new Integer(br.readLine())).intValue();
          
          while( choice < 0 || choice > 9 ){
            System.out.println("Please choose a valid channel");
            choice = (new Integer(br.readLine())).intValue();
          }
          
          myCar.setPlaylist(station, choice);
        }
        
        else if( decision.equalsIgnoreCase("rent") ){
          nextCar = rent(custom);
          multiple = true;
        }
        
        else
          break;
      }

      System.out.println("Thanks, have a nice day!");
    }
    
    catch(IOException e){
      System.out.println("Sorry, you are not trustworthy enough to rent a car.");
      System.exit(0);
    }
  }
  
  static public Car rent(boolean custom){
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
      Car car;
      if( custom ){
        System.out.println("Are you looking for a 2 door or 4 door car?");
        int doors = (new Integer(br.readLine())).intValue();
        
        System.out.println("What color do you want your car to be? Blue, Black, Silver or Red?");
        String color = br.readLine();
        
        System.out.println("Please choose from the following list of stations:");
        System.out.println("(You must choose 5 stations)");
        System.out.println("0 -- Rock");
        System.out.println("1 -- Hip Hop");
        System.out.println("2 -- Jazz");
        System.out.println("3 -- Rap");
        System.out.println("4 -- R&B");
        System.out.println("5 -- Alternative");
        System.out.println("6 -- Dance");
        System.out.println("7 -- Instrumental");
        System.out.println("8 -- Techno");
        System.out.println("9 -- News Radio");
        int counter = 0;
        int[] playlist = new int[5];
        while(counter < 5){
          System.out.println("Please choose a station");
          playlist[counter] = (new Integer(br.readLine())).intValue();
          counter++;
        }
        
        car = new Car(color, doors, playlist);
      }
      else{
        car = new Car();
      }
      return car;
    }
    catch( IOException e ){
      System.out.println("Sorry, you are not trustworthy enough to rent a car.");
      System.exit(0);
    }
    return null;
  }
}
