示例#1
0
 // Driver
 public static void main(String[] args) {
   Scanner input = new Scanner(System.in);
   System.out.println("What file do you want to use for input/output?");
   String filename = input.nextLine();
   Airline a = new Airline(filename);
   String city1, city2;
   loop:
   while (true) {
     System.out.println("\n\tWhat would you like to do?");
     System.out.println("\t1: See All Direct Routes");
     System.out.println("\t2: Find Minimum Spanning Tree");
     System.out.println("\t3: Find Shortest Path by Distance");
     System.out.println("\t4: Find Shortest Path by Cost");
     System.out.println("\t5: Find Shortest Path by Hops");
     System.out.println("\t6: Find Trips Under Cost");
     System.out.println("\t7: Add a Route");
     System.out.println("\t8: Remove a Route");
     System.out.println("\t9: Add a City");
     System.out.println("\t10: Remove a City");
     System.out.println("\t11: Quit");
     System.out.println("\t12: Quit Without Saving");
     System.out.print("Enter numeric choice: ");
     int choice = input.nextInt();
     input.nextLine(); // throw out leftover newline
     switch (choice) {
       case 1:
         a.showAllRoutes();
         break;
       case 2:
         a.mst();
         break;
       case 3:
         System.out.print("Enter the first city: ");
         city1 = input.nextLine();
         System.out.print("Enter the second city: ");
         city2 = input.nextLine();
         a.shortestByDistance(city1, city2);
         break;
       case 4:
         System.out.print("Enter the first city: ");
         city1 = input.nextLine();
         System.out.print("Enter the second city: ");
         city2 = input.nextLine();
         a.shortestByCost(city1, city2);
         break;
       case 5:
         System.out.print("Enter the first city: ");
         city1 = input.nextLine();
         System.out.print("Enter the second city: ");
         city2 = input.nextLine();
         a.shortestByHops(city1, city2);
         break;
       case 6:
         System.out.print("Enter max cost: ");
         double cost = input.nextInt();
         a.pathsUnderCost(cost);
         break;
       case 7:
         System.out.print("Enter the first city: ");
         city1 = input.nextLine();
         System.out.print("Enter the second city: ");
         city2 = input.nextLine();
         System.out.print("Enter the distance: ");
         int distance = input.nextInt();
         System.out.print("Enter the price: ");
         double price = input.nextDouble();
         a.addRoute(city1, city2, distance, price);
         break;
       case 8:
         System.out.print("Enter the first city: ");
         city1 = input.nextLine();
         System.out.print("Enter the second city: ");
         city2 = input.nextLine();
         a.removeRoute(city1, city2);
         break;
       case 9:
         System.out.print("Enter the city name: ");
         city1 = input.nextLine();
         a.addCity(city1);
         break;
       case 10:
         System.out.print("Enter the city name: ");
         city1 = input.nextLine();
         a.removeCity(city1);
         break;
       case 11:
         a.saveRoutes(filename);
         break loop;
       case 12:
         break loop;
     }
   }
 }
示例#2
0
  /** Example. */
  @Test
  public void example() {
    double dblReserve = TankSize.RESERVE.size() * 2.0; // RESERVE size is set to 1600
    FuelTank largeReserve = new FuelTank(dblReserve);
    FuelTank tripleReserve = new FuelTank(TankSize.RESERVE.multi(3));
    FuelTank normalReserve = new FuelTank(TankSize.RESERVE);
    FuelTank miniTank = new FuelTank(2); // 2 Gallon tank
    FuelTank fiveGalTank = new FuelTank(5);
    FuelTank largeFuelTank = new FuelTank(TankSize.LARGE);
    Airplane smallPlane =
        new Airplane(
            new Random().nextInt(),
            Airline.AAL,
            largeFuelTank,
            AirplaneState.WAITINHANGAR,
            Airline.AAL.getHangar());
    Airplane p1 = new Airplane();
    p1.setId(1);
    p1.setOwner(Airline.getRandom());
    p1.setTank(new FuelTank(new Random().nextGaussian() * 100));
    p1.setHolder(p1.getOwner().getHangar());

    /**
     * //How a taxi would be created Taxi t1 = new Taxi(new Random().nextInt(), null, new
     * FuelTank(TankSize.SMALL.multi((new Random().nextGaussian()*.5))), VehicleState.WAITING,
     * Taxiway.A); Taxi t2 = new Taxi(); t2.setId(2); t2.setOwner(Airport.AUS); t2.setTank(new
     * FuelTank(new Random().nextGaussian()*15)); t2.setHolder(t2.getOwner().chooseTaxiway());
     */
    /*
     * All Tanks are empty, you must either fill() or add(Fuel f)
     * Vehicles can refuel() although this fills to FULL not MAX
     */
    try {
      tripleReserve.fill(); // Magically fill tank to capacity
      largeReserve.fill(tripleReserve); // Fill tank from source
      normalReserve.fill(tripleReserve);
    } catch (FuelTankException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      assert (tripleReserve.isEmpty());
      assert (largeReserve.isFull());
      assert (normalReserve.isFull());
    }

    try {
      largeFuelTank.fill(normalReserve);
      fiveGalTank.fill(largeFuelTank);
      miniTank.fill(largeFuelTank);
      smallPlane.getTank().fill(largeFuelTank);

    } catch (FuelTankException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      assert (!largeFuelTank.isEmpty());
      assert (!normalReserve.isEmpty());
      assert (miniTank.isFull());
      assert (smallPlane.getTank().isFull());
    }
  }
示例#3
0
  /**
   * Method used for posting on the servlet
   *
   * @param request comes from the client
   * @param response gives to the client
   * @throws ServletException
   * @throws IOException
   */
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/plain");

    String name = getParameter("name", request);
    if (name == null) {
      missingRequiredParameter(response, "name");
      return;
    }

    String flightNumber = getParameter("flightNumber", request);
    if (flightNumber == null) {
      missingRequiredParameter(response, "flight number");
      return;
    }

    String src = getParameter("src", request);
    if (src == null) {
      missingRequiredParameter(response, "source airport");
      return;
    }

    String departTime = getParameter("departTime", request);

    String[] departArray = departTime.split(" ");
    if (departTime == null) {
      missingRequiredParameter(response, "departure time");
      return;
    }

    String dest = getParameter("dest", request);
    if (dest == null) {
      missingRequiredParameter(response, "destination airport");
      return;
    }

    String arrivalTime = getParameter("arrivalTime", request);
    String[] arrivalArray = arrivalTime.split(" ");
    if (arrivalTime == null) {
      missingRequiredParameter(response, "arrival time");
      return;
    }

    String[] args = {
      name,
      flightNumber,
      src,
      departArray[0],
      departArray[1],
      departArray[2],
      dest,
      arrivalArray[0],
      arrivalArray[1],
      arrivalArray[2]
    };
    Flight flight = Flight.getFlightFromArgs(args);

    if (this.data.containsKey(name)) {
      Airline airline1 = this.data.get(name);

      for (Object flight1 : this.data.get(name).getFlights()) {
        Flight flights2 = (Flight) flight1;
        if (flights2.getFlightNumber().equals(flight.getFlightNumber())) {
          System.out.println("This Flight number exists");
          return;
        }
      }
      airline1.addFlight(flight);
      this.data.replace(name, airline1);
      System.out.println("added flight to existing airline");
    }

    if (!this.data.containsKey(name)) {
      Airline airline = new Airline(name);
      airline.addFlight(flight);
      this.data.put(name, airline);
      System.out.println("Created a new flight");
    }

    PrintWriter pw = response.getWriter();
    pw.println("added");
    pw.flush();

    response.setStatus(HttpServletResponse.SC_OK);
  }