i:\SADP\Mid Term Exam\Memeto.java

mail@pastecode.io avatar
unknown
java
a year ago
1.1 kB
2
Indexable
Never
// Abstract Product
abstract class Car {
    abstract void start();
}

// Concrete Products
class Sedan extends Car {
    @Override
    void start() {
        System.out.println("Sedan car started.");
    }
}

class SUV extends Car {
    @Override
    void start() {
        System.out.println("SUV car started.");
    }
}

class SportsCar extends Car {
    @Override
    void start() {
        System.out.println("Sports car started.");
    }
}

// Factory
class CarFactory {
    public Car createCar(String carType) {
        if (carType.equalsIgnoreCase("Sedan")) {
            return new Sedan();
        } else if (carType.equalsIgnoreCase("SUV")) {
            return new SUV();
        } else if (carType.equalsIgnoreCase("SportsCar")) {
            return new SportsCar();
        }
        return null;
    }
}

// // Usage
// CarFactory carFactory = new CarFactory();

// Car sedan = carFactory.createCar("Sedan");
// sedan.start();

// Car suv = carFactory.createCar("SUV");
// suv.start();

// Car sportsCar = carFactory.createCar("SportsCar");
// sportsCar.start();