예제 #1
0
  public static void main(String[] args) {

    Car car1 = new Car();
    Car car2 = new Car();
    Car car3 = new Car();
    Car car4 = new Car();

    // create car 1
    for (int increment = 0; increment < 3; increment++) {
      // prompt for user input on car
      String color = JOptionPane.showInputDialog(null, "Please enter the car's color.");
      String horsePower = JOptionPane.showInputDialog(null, "Please enter the car's horse power.");
      int horsePwr = Integer.parseInt(horsePower);
      String engineSize = JOptionPane.showInputDialog(null, "Please enter the car's engine size.");
      int engine = Integer.parseInt(engineSize);
      String make = JOptionPane.showInputDialog(null, "Please enter the car's make");

      // create car1
      if (increment == 0) {
        car1.setColor(color);
        car1.setHorsePwr(horsePwr);
        car1.setEngine(engine);
        car1.setMake(make);
      }
      // create car2
      if (increment == 1) {
        car2.setColor(color);
        car2.setHorsePwr(horsePwr);
        car2.setEngine(engine);
        car2.setMake(make);
      }
      // create car3
      if (increment == 2) {
        car3.setColor(color);
        car3.setHorsePwr(horsePwr);
        car3.setEngine(engine);
        car3.setMake(make);

        // create car 4
        car4.setColor(color);
        car4.setHorsePwr(horsePwr);
        car4.setEngine(engine);
        car4.setMake(make);
      }
    }

    // Output cars and count
    JOptionPane.showMessageDialog(null, "Car one: " + car1);
    JOptionPane.showMessageDialog(null, "Car two: " + car2);
    JOptionPane.showMessageDialog(null, "Car three: " + car3);
    JOptionPane.showMessageDialog(null, "Car four: " + car4);
    JOptionPane.showMessageDialog(null, "Car count is " + Car.getCount());

    // compare car 3 and car 4
    if (car3.equals(car4)) {
      JOptionPane.showMessageDialog(null, "Car three and car four are equal.");
    }
  }
예제 #2
0
 public void modifyCar(int id, int price, String name, int model, String color) {
   Car temp = (Car) cars.get(id);
   temp.setPrice(price);
   temp.setName(name);
   temp.setModel(model);
   temp.setColor(color);
   cars.set(id, temp);
 }
예제 #3
0
  private List<Car> createCars(int carsToCreate) {
    Car car;
    List<Car> carList = new ArrayList();

    for (int i = 0; i < carsToCreate; i++) {
      car = new Car();
      car.setBodyStyle(car.getBodyStyle());
      car.setColor(car.getColor());
      car.setMake(car.getMake());
      car.setIsFast(car.getIsFast());

      carList.add(car);
    }

    return carList;
  }