Esempio n. 1
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;
  }
Esempio n. 2
0
  /**
   * 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;
  }
Esempio n. 3
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;
  }