public synchronized void editOption() {
   synchronized (Automobile.class) {
     System.out.println("Edit Option by thread: " + super.getName());
     auto.printOptionSet(oldSetName);
     auto.updateOption(oldSetName, oldOptionName, newOptionName, newOptionPrice);
     auto.printOptionSet(oldSetName);
   }
 }
 // Methods
 public synchronized void editOptionSetChoice() {
   try {
     synchronized (Automobile.class) {
       System.out.println("Edit OptionSet Choice by thread: " + super.getName());
       System.out.println("Choice changes from " + auto.getOptionChoice(oldSetName) + " to : ");
       auto.setOptionChoice(oldSetName, newChoice);
       System.out.println(auto.getOptionChoice(oldSetName));
     }
   } catch (NotFoundException e) {
     e.printStackTrace();
   }
 }
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String modelName = request.getParameter("model");

    response.setContentType("text/html");
    PrintWriter writer = response.getWriter();
    HttpSession session = request.getSession();

    DefaultSocketClient client = new DefaultSocketClient("localhost", 8088);
    if (client.openConnection()) {
      Automobile a = client.getModelfromServer(modelName);

      writer.println("<!DOCTYPE HTML>");
      writer.println("<html>");
      writer.println("<head>");
      writer.println("<title>ConfigModel</title>");
      writer.println("</head>");
      writer.println("<body><div>");
      writer.println("<h1>Basic Car Choice</h1>");
      writer.println("<form action='Summary.jsp'>");
      writer.println("<table action='ConfigModel' border='1px'>");
      writer.println(
          "<tr><td align='middle'><b>Make/Model: </b></td><td>"
              + a.getMake()
              + " "
              + a.getModel()
              + "</td></tr>");
      ArrayList<String> opsetNames = a.getOpsetNames();
      for (int i = 0; i < opsetNames.size(); i++) {
        writer.println("<tr><td align='middle'><b>" + opsetNames.get(i) + ": " + "</b></td>");
        writer.println("<td><select name='" + opsetNames.get(i) + "'>");
        ArrayList<String> optionNames = a.getOptionNames(opsetNames.get(i));
        for (int j = 0; j < optionNames.size(); j++) {
          String optionName = optionNames.get(j);
          writer.println("<option value='" + optionName + "'>" + optionName + "</option>");
        }
        writer.println("</select></td>");
        writer.println("</tr>");
      }
      writer.println(
          "<tr><td colspan=2 align='right'><input type='submit' value='Done'/></td></tr>");
      writer.println("</table>");
      writer.println("</form>");
      writer.println("</div></body>");
      writer.println("</html>");

      session.setAttribute("auto", a);
    }
  }
  public void run() {
    try {
      in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      while (true) {
        String message = in.readLine();
        if (message == null) continue;

        out = new PrintWriter(socket.getOutputStream(), true);
        switch (message) {
          case "quit":
            out.println("Bye!");
            System.out.println("Quit");
            socket.close();
            return;
          case "send":
            ObjectInputStream objectIS = new ObjectInputStream(socket.getInputStream());
            Properties properties = (Properties) objectIS.readObject();
            BuildCarModelOptions bulidCarModelOptions = new BuildCarModelOptions();
            Automobile auto = bulidCarModelOptions.buildAutoOptions(properties);
            System.out.println("Receive Automobile " + auto.getModel());
            out.println("Automobile created successfully.");
            break;
          case "list":
            AutoServer autoServer = new BuildAuto();
            out.println(autoServer.listAutomobiles());
            out.println("");
            System.out.println("Return list of Automobiles:\n" + autoServer.listAutomobiles());
            break;
          case "config":
            String modelName = in.readLine();
            BuildAuto buildAuto = new BuildAuto();
            Automobile automobile = buildAuto.getInstace(modelName);
            ObjectOutputStream objectOS = new ObjectOutputStream(socket.getOutputStream());
            objectOS.writeObject(automobile);
            System.out.println("Return Automobile " + automobile.getModel());
            break;
        }
      }

    } catch (IOException e) {
      System.err.println("Could not listen on port: " + socket.getPort());
      System.exit(1);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
      System.exit(1);
    }
  }
  /**
   * buildAutoObject() reads the data from the file to Automotive object
   *
   * @param filename
   * @return
   * @throws FileNotFoundException
   * @throws FixProblems
   * @throws IOException
   * @throws NumberFormatException
   */
  public Automobile buildAutoObject(String filename) throws FixProblems {
    String currentStep = "";
    try {
      BufferedReader bufferedReader = null;
      currentStep = "FILE_READ";
      bufferedReader = new BufferedReader(new FileReader(filename));
      String line = null;
      a = new Automobile("Ford's Wagon ZTW", 5, 18445);

      int count = 0;
      int cost = 0;
      int optionCount = 0, optionSetCount = 0;
      while ((line = bufferedReader.readLine()) != null) {
        if (!line.contains(":")) {
          continue;
        }
        String optionSetName = null;
        StringTokenizer st = new StringTokenizer(line, ":");
        String s = st.nextToken();
        if ((s).equals("keys")) {
          currentStep = "OPTION SET";
          optionSetName = st.nextToken();
          count = Integer.parseInt(st.nextToken().trim());
          a.setOptionSet(optionSetName, count, optionSetCount);
          optionSetCount++;
          optionCount = 0;
        } else if (!(s).equals("keys")) {
          if (optionCount < count) {
            currentStep = "OPTION PRICE";
            cost = Integer.parseInt(st.nextToken().trim());
            a.setOption(optionSetName, s, cost, optionCount, optionSetCount - 1, count);
            optionCount++;
          } else {
            System.out.println("Array out of Bounds");
          }
        }
      }

      a.print();
    } catch (Exception ex) {
      throw new FixProblems(ex, currentStep);
      // throw new FixProblems(101);
    }
    return a;
  }
  /*
   * addAuto method. parse the Automobile object and add them to three tables.
   */
  public void addAuto(Automobile auto) {
    String name = null;
    float price = 0;
    int autoid = 0;
    String sql = null;
    try {
      String autoName = auto.getMake() + " " + auto.getModel();
      price = auto.getBasePrice();
      sql =
          "insert into automobile (name, base_price) values('"
              + autoName
              + "',"
              + price
              + ")"; // MySQL command
      statement.executeUpdate(sql); // add to automobile table

      sql = "select id from automobile where name ='" + autoName + "';";
      ResultSet rs = statement.executeQuery(sql); // use ResultSet object to receive
      while (rs.next()) {
        autoid = rs.getInt("id"); // get auto_id
      }
      for (int i = 0; i < auto.getOptionSetNum(); i++) {
        name = auto.getOptionSetName(i);
        sql = "insert into optionset (name, auto_id) values('" + name + "'," + autoid + ")";
        statement.executeUpdate(sql); // add to optionset table
        int opsid = 0;
        sql = "select id from optionset where name ='" + name + "';";
        ResultSet rrs = statement.executeQuery(sql);
        while (rrs.next()) {
          opsid = rrs.getInt("id"); // get optionset id
        }
        for (int j = 0; j < auto.getOptionNum(i); j++) {
          name = auto.getOptionName(i, j);
          price = auto.getOptionPrice(i, j);
          sql =
              "insert into options (name, price, option_id) values('"
                  + name
                  + "',"
                  + price
                  + ","
                  + opsid
                  + ")";
          statement.executeUpdate(sql); // add to options table
        }
      }

    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
Beispiel #7
0
  public static Automobile parseProperties(Properties properObj) {
    Automobile autoObj = new Automobile();

    autoObj.setModelName(properObj.getProperty("CarModel"));
    properObj.remove("CarModel");
    autoObj.setMaker(properObj.getProperty("CarMake"));
    properObj.remove("CarMake");
    autoObj.setBasePrice(Float.parseFloat(properObj.getProperty("CarPrice")));
    properObj.remove("CarPrice");

    int i = 1;
    String key = "Option";
    while (!properObj.isEmpty()) {
      String optionsetName = properObj.getProperty(key + i);
      autoObj.setOptionset(optionsetName);
      properObj.remove(key + i);

      int j = 1;
      while (properObj.containsKey(key + i + "Value" + j)) {
        float optionPrice = 0;
        String optionName = properObj.getProperty(key + i + "Value" + j);
        properObj.remove(key + i + "Value" + j);
        if (properObj.containsKey(key + i + "Value" + j + "price")) {
          optionPrice = Float.parseFloat(properObj.getProperty(key + i + "Value" + j + "price"));
          properObj.remove(key + i + "Value" + j + "price");
        }
        autoObj.setOption(optionsetName, optionName, optionPrice);
        j++;
      }
      i++;
    }
    return autoObj;
  }
Beispiel #8
0
  public static Automobile buildAutoObject(String fileName) throws AutoException {
    Automobile model = new Automobile();
    try {
      FileReader file = new FileReader(fileName);
      BufferedReader buff = new BufferedReader(file);
      boolean eof = false;

      String line;
      for (int i = 0; i < 3; i++) {
        line = buff.readLine();
        if (line == null) {
          eof = true;
        } else {
          String[] array = line.split(": ");
          try {
            if (array.length == 2) {
              if (i == 0) {
                model.setModelName(array[1]);
              } else if (i == 1) {
                try {
                  float basePrice =
                      NumberFormat.getNumberInstance(java.util.Locale.US)
                          .parse(array[1])
                          .floatValue();
                  model.setBasePrice(basePrice);
                } catch (ParseException e) {
                  e.printStackTrace();
                }
              } else if (i == 2) {
                model.setMaker(array[1]);
              }
            } else {
              if (i == 0) {
                throw new AutoException(ModelError.MISSING_MODEL_NAME);
              } else if (i == 1) {
                throw new AutoException(ModelError.MISSING_MODEL_PRICE);
              }
            }
          } catch (AutoException e) {
            e.fix(e.getErrorCode().getNumber(), model);
          }
        }
      }

      int i = 0; // number of line
      while (!eof) {
        line = buff.readLine();
        if (line == null) {
          eof = true;
        } else {
          String[] optionSet = line.split(": "); // setName + options
          try {
            if (optionSet.length != 2 || optionSet[0].isEmpty() || optionSet[1].isEmpty()) {
              throw new AutoException(ModelError.MISSING_OPTIONSET_DATA);
            } else {
              String[] options = optionSet[1].split("; "); // (optionName + price)*n
              model.setOptionset(optionSet[0]);
              for (int j = 0; j < options.length; j++) {
                String[] option = options[j].split(", "); // optionName + price
                if (option.length < 2) {
                  throw new AutoException(ModelError.MISSING_OPTION_DATA);
                } else {
                  try {
                    float price =
                        NumberFormat.getNumberInstance(java.util.Locale.US)
                            .parse(option[1])
                            .floatValue();
                    model.setOption(i, option[0], price);
                  } catch (ParseException e) {
                    e.printStackTrace();
                  }
                }
              }
            }
          } catch (AutoException e) {
            e.fix(e.getErrorCode().getNumber(), model);
          }
        }
        i++;
      }
      buff.close();
    } catch (IOException e) {
      System.out.println("Error -- " + e.toString());
    }
    return model;
  }