Ejemplo n.º 1
0
  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);
    }
  }
Ejemplo n.º 2
0
  /*
   * 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();
    }
  }
Ejemplo n.º 3
0
  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);
    }
  }