Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.2 kB
6
Indexable
Never
interface Car {
  public String getDescription();
  public double cost();
}

class CModel implements Car {
  @Override
  public String getDescription() {
    return "C Model";
  }

  @Override
  public double cost() {
    return 38000;
  }
}

class EModel implements Car {
  @Override
  public String getDescription() {
    return "E Model";
  }

  @Override
  public double cost() {
    return 41000;
  }
}

class CSModel implements Car {
  @Override
  public String getDescription() {
    return "CS Model";
  }

  @Override
  public double cost() {
    return 54000;
  }
}

abstract class CarDecorator implements Car {
    protected Car car;

    public CarDecorator(Car car) {
        this.car = car;
    }

    public String getDescription() {
        return car.getDescription();
    }

    public double cost() {
        return car.cost();
    }
}

class V8Engine extends CarDecorator {

  public V8Engine(Car car) {
      super(car);
  }

  public String getDescription() {
      return car.getDescription() + ", V-8 Engine";
  }

  public double cost() {
      return car.cost() + 4000;
  }
}

class V12Engine extends CarDecorator {
    Car car;

    public V12Engine(Car car) {
        super(car);
    }

    public String getDescription() {
        return car.getDescription() + ", V-12 Engine";
    }

    public double cost() {
        return car.cost() + 6000;
    }
}

public class CarOrder {

    public static boolean isValid(String model, String feature) {
        if (feature.equalsIgnoreCase("V-8")) {
            if (!(model.equalsIgnoreCase("C"))) {
                System.out.println("V8Engine can only be added to CModel");
                return false;
            }
        } else if (feature.equalsIgnoreCase("V-12")) {
            if (!(model.equalsIgnoreCase("E") || model.equalsIgnoreCase("CS"))) {
                System.out.println("V12Engine can only be added to EModel and CSModel");
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
      do {
          System.out.print("Enter car model: ");
          String model = System.console().readLine();
          if (model.equalsIgnoreCase("quit")) {
              break;
          }
          Car car = null;
          if (model.equalsIgnoreCase("C")) {
              car = new CModel();
          } else if (model.equalsIgnoreCase("E")) {
              car = new EModel();
          } else if (model.equalsIgnoreCase("CS")) {
              car = new CSModel();
          } else {
              System.out.println("Invalid car model");
              continue;
          }
          boolean hasV8 = false;
          boolean hasV12 = false;
          boolean hasSunRoof = false;
          boolean hasTowingPackage = false;
          boolean hasOversizedGasTank = false;
          boolean hasPumaSkinSeats = false;
          boolean hasSealSkinSeats = false;
          boolean hasKomodoDragonSkinSeats = false;
          boolean has32ozCupHolders = false;
          boolean hasBlaupunktNewYork800Radio = false;
          boolean hasSpareTire = false;
          boolean hasFreeFirstOilChange = false;
          
          do {
              System.out.print("Enter feature: ");
              String feature = System.console().readLine();
              feature = feature.toUpperCase();
              if (feature.equalsIgnoreCase("done")) {
                  break;
              }
              if (feature.equalsIgnoreCase("V-8") && isValid(model, feature)) {
                  if (hasV8) {
                      System.out.println("V8Engine already added");
                      continue;
                  }
                  car = new V8Engine(car);
                  hasV8 = true;
              } else if (feature.equalsIgnoreCase("V-12") && isValid(model, feature)) {
                  if (hasV12) {
                      System.out.println("V12Engine already added");
                      continue;
                  }
                  car = new V12Engine(car);
                  hasV12 = true;
              }
          } while (true);
          System.out.println("Car: " + car.getDescription());
          System.out.println("Cost: " + car.cost());
      } while (true);
    }
}