示例#1
0
  public void sellTickets() {
    StringTokenizer st = new StringTokenizer(tickets.getViewState(), ",");
    ArrayList<Map.Entry<String, Integer>> disCodes = db.getDiscountsAndPrices();
    int discountCodeCount = disCodes.size();
    if (st.countTokens() == discountCodeCount) {
      ArrayList<Map.Entry<String, Integer>> order = new ArrayList<Map.Entry<String, Integer>>();
      int orderSize = 0;
      int price = 0;
      for (Map.Entry<String, Integer> codeAndPrice : disCodes) {
        int ticCount = Integer.parseInt(st.nextToken());
        orderSize = orderSize + ticCount;
        order.add(new AbstractMap.SimpleEntry<String, Integer>(codeAndPrice.getKey(), ticCount));
        price = price + (codeAndPrice.getValue() * ticCount);
      }
      try {
        Movie m = db.getMovie(movieList.getViewState(), timesList.getViewState());
        int attendance = m.getAttendance();
        int capacity = m.getTheater().getCapacity();
        if (capacity >= attendance + orderSize) {

          feedback.setViewState(
              "Please tender $"
                  + price
                  + " to the cashier and proceed to theater "
                  + m.getTheater().getID());
          for (Map.Entry<String, Integer> suborder : order) {
            m.addOrder(suborder.getKey(), suborder.getValue());
          }
        } else {
          feedback.setViewState(
              "I'm sorry, but the showing of "
                  + m.toString()
                  + " is sold out. Please select another film.");
        }
      } catch (Exception e) {
        System.out.println(e);
      }

    } else {
      String err =
          "Malformed ticket order: Input must be comma "
              + "separated, without spaces,"
              + " with the value in each postion reflecting "
              + "the number of each type of ticket you would like."
              + "e.g. If the discount codes are Adult,Child,Senior "
              + "2,1,0 would represent an order of two adults and"
              + " one child";
      System.out.println(err);
      feedback.setViewState(err);
    }
  }