Table of Contents
Problem Statement
Create a program which takes input from user about the brand and type of vehicle they want to have and what they want to do with it. make sure to follow all the oops principles and use appropriate design pattern. take care of following points:
- There are more than 3 brands (Mercedes, BMW, Ford).
- There are 3 types of vehicles Car, Bike, SuperBike.
- Mercedes has Car, Bike.
- BMW has Car, SuperBike. when asked for a Bike give a SuperBike instead.
- Ford has Car. No bike.
Class Diagram
Will be creating 4 Classes for representing 3 important aspects of the game.
-
GarageManager: This will we resposible for prompting the user for its input and will take care of object creation and performing operation on that.
-
Vehicle(Interface): This Interface will define how a Concrete vehicle should behave and what all characteristics define a vehicle.
-
Car: This implements Vehicle interface and resposible for defininig characteristics of a car.
-
Bike: This implements Vehicle interface and resposible for defininig characteristics of a Bike.
-
Superbike: This implements Vehicle interface and resposible for defininig characteristics of a Superbike.
-
VehicleFactory(Interface): This Interface will define how a Concrete vehicleFactory should behave and what all characteristics define a vehicleFactory. It shows what type of methods will result creation of different vehicles.
-
MercedesFactory: This implements VehicleFactory interface and resposible for creating Vehicles for Mercedes. It can create a car and a bike.
-
BMWFactory: This implements VehicleFactory interface and resposible for creating Vehicles for BMW. It can create a car and a Superbike.
-
FordFactory: This implements VehicleFactory interface and resposible for creating Vehicles for Ford. It can create only car.
Class diagram below explains this:
classDiagram
class Vehicle {
+String horn()
+String drive()
}
class VehicleFactory {
+Vehicle getBikeVehicle()
+Vehicle getCarVehicle()
+String getBrand()
}
class Bike {
-String brand
+Bike(String)
+String horn()
+String drive()
}
class Car {
-String brand
+Car(String)
+String horn()
+String drive()
}
class SuperBike {
-String brand
+SuperBike(String)
+String horn()
+String drive()
}
class MercedesFactory {
+Vehicle getBikeVehicle()
+Vehicle getCarVehicle()
+String getBrand()
}
class FordFactory {
+Vehicle getBikeVehicle()
+Vehicle getCarVehicle()
+String getBrand()
}
class BMWFactory {
+Vehicle getBikeVehicle()
+Vehicle getCarVehicle()
+String getBrand()
}
class GarageManager {
-Scanner scanner
+void attendCustomer()
-boolean checkBrandInput(String)
-boolean checkVehicleInput(String)
-boolean checkOperationInput(String)
-void hornCaller(Vehicle, VehicleFactory)
-void driveCaller(Vehicle, VehicleFactory)
-VehicleFactory getFactoryBasedOnBrand(String)
-Vehicle getVehicleBasedOnType(String, VehicleFactory)
-void doOperation(String, VehicleFactory, Vehicle)
}
Vehicle <|.. Bike
Vehicle <|.. Car
Vehicle <|.. SuperBike
VehicleFactory <|.. MercedesFactory
VehicleFactory <|.. FordFactory
VehicleFactory <|.. BMWFactory
GarageManager --> VehicleFactory
GarageManager --> Vehicle
MercedesFactory --> Bike
MercedesFactory --> Car
FordFactory --> Car
BMWFactory --> SuperBike
BMWFactory --> Car
Relationships
- GarageManager has a has a, one-to-one relationship with Vehicle, as there can be only one Vehicle in a game at a certain period of time.
- GarageManager has a has a, one-to-one relationship with VehicleFactory, as there can be only one VehicleFactory in a game at a certain period of time.
Design Patterns
I have used Factory Pattern in this, as it aligned with our usecase, because if in future we get a new company (say Ferrari), then we don’t have to edit any existing class but extend the logic for new companies.
Same goes for vehicles, if we want to introduce a new vehicle (say supercar) tomorrow then we can extend the current logic rather than changing the existing one.
Code Files
Main.java
public class Main {
public static void main(String[] args) {
GarageManager garageManager = new GarageManager();
garageManager.attendCustomer();
}
}
garage/GarageManager.java
import factory.BMWFactory;
import factory.FordFactory;
import factory.MercedesFactory;
import factory.VehicleFactory;
import vehicle.Vehicle;
import java.util.Scanner;
public class GarageManager {
private Scanner scanner = new Scanner(System.in);
public void attendCustomer(){
System.out.println("Hey!! welcome to the garage.
We have many vehicles.");
VehicleFactory vehicleFactory;
Vehicle vehicle;
while(true) {
System.out.println("what type of brand do you prefer?");
System.out.println("Mercedes (type m)");
System.out.println("BMW (type b)");
System.out.println("ford (type f)");
String brand = scanner.nextLine();
if(!checkBrandInput(brand))
{
System.out.println("That is wrong input. try again");
continue;
}
System.out.println("what type of vehicle do you prefer?");
System.out.println("Bike (type b)");
System.out.println("Car (type c)");
String type = scanner.nextLine();
if(!checkVehicleInput(type)) {
System.out.println("That is wrong input. try again");
continue;
}
vehicleFactory = getFactoryBasedOnBrand(brand);
vehicle = getVehicleBasedOnType(type, vehicleFactory);
while(true) {
System.out.println("What do you want to do with
your vehicle?");
System.out.println("Horn (type h)");
System.out.println("Drive (type d)");
System.out.println("nothing (type anything else)");
String operation = scanner.nextLine();
if(!checkOperationInput(operation)) {
System.out.println("cool, bye!!");
break;
}
doOperation(operation,vehicleFactory,vehicle);
}
}
}
private boolean checkBrandInput(String brand) {
return "m".equals(brand) || "b".equals(brand) || "f".equals(brand);
}
private boolean checkVehicleInput(String vehicleType) {
return "c".equals(vehicleType) || "b".equals(vehicleType);
}
private boolean checkOperationInput(String operation) {
return "h".equals(operation) || "d".equals(operation);
}
private void hornCaller(Vehicle vehicle, VehicleFactory factory) {
if(vehicle != null)
System.out.println(vehicle.horn());
else
System.out.println(factory.getBrand() + " does not have
this vehicle");
}
private void driveCaller(Vehicle vehicle, VehicleFactory factory) {
if(vehicle != null)
System.out.println(vehicle.drive());
else
System.out.println(factory.getBrand() + " does not have
this vehicle");
}
private VehicleFactory getFactoryBasedOnBrand(String brand) {
if("m".equals(brand)) {
return new MercedesFactory();
}
else if("b".equals(brand)) {
return new BMWFactory();
}
return new FordFactory();
}
private Vehicle getVehicleBasedOnType(String type,
VehicleFactory factory) {
if("b".equals(type)) {
return factory.getBikeVehicle();
}
return factory.getCarVehicle();
}
private void doOperation(String operation, VehicleFactory factory,
Vehicle vehicle) {
if("h".equals(operation)) {
hornCaller(vehicle,factory);
}
else {
driveCaller(vehicle, factory);
}
}
}
garage/vehicle/Vehicle.java
package vehicle;
public interface Vehicle {
public String horn();
public String drive();
}
garage/vehicle/Car.java
package vehicle;
public class Car implements Vehicle{
private String brand;
public Car(String brand) {
this.brand = brand;
}
@Override
public String drive() {
return "This is a driving Car of " + brand;
}
@Override
public String horn() {
return "This is honking Car of " + brand;
}
}
garage/vehicle/Bike.java
package vehicle;
public class Bike implements Vehicle{
private String brand;
public Bike(String brand) {
this.brand = brand;
}
@Override
public String drive() {
return "This is a driving Bike of " + brand;
}
@Override
public String horn() {
return "This is honking Bike of " + brand;
}
}
garage/vehicle/SuperBike.java
package vehicle;
public class SuperBike implements Vehicle{
private String brand;
public SuperBike(String brand) {
this.brand = brand;
}
@Override
public String drive() {
return "This is a driving SuperBike of " + brand;
}
@Override
public String horn() {
return "This is honking SuperBike of " + brand;
}
}
garage/factory/VehicleFactory.java
package factory;
import vehicle.Vehicle;
public interface VehicleFactory {
public Vehicle getBikeVehicle();
public Vehicle getCarVehicle();
public String getBrand();
}
garage/factory/MercedesFactory.java
package factory;
import vehicle.Bike;
import vehicle.Car;
import vehicle.Vehicle;
public class MercedesFactory implements VehicleFactory{
private static final String BRAND="Mercedes";
@Override
public Vehicle getBikeVehicle() {
return new Bike(BRAND);
}
@Override
public Vehicle getCarVehicle() {
return new Car(BRAND);
}
@Override
public String getBrand() {
return BRAND;
}
}
garage/factory/BMWFactory.java
package factory;
import vehicle.Bike;
import vehicle.Car;
import vehicle.SuperBike;
import vehicle.Vehicle;
public class BMWFactory implements VehicleFactory{
private static final String BRAND="BMW";
@Override
public Vehicle getBikeVehicle() {
return new SuperBike(BRAND);
}
@Override
public Vehicle getCarVehicle() {
return new Car(BRAND);
}
@Override
public String getBrand() {
return BRAND;
}
}
garage/factory/FordFactory.java
package factory;
import vehicle.Car;
import vehicle.Vehicle;
public class FordFactory implements VehicleFactory{
public static final String BRAND = "Ford";
@Override
public Vehicle getBikeVehicle() {
return null;
}
@Override
public Vehicle getCarVehicle() {
return new Car(BRAND);
}
@Override
public String getBrand() {
return BRAND;
}
}
Summary
While there are many approaches to this problem, the one presented here is the simplest to devise and easiest to apply. My main motive was not to come up with the fully functional end to end code but to apply my learnings and understanding of factory pattern.
One thing to note is that while this code fulfills the basic requirements, it is also extensible enough to let the user to extend the logic to multiple companies and multiple products, that is something factory patterns helps us to achieve.
|