From ad756955e19e8317440a8255a58223ee0fdcfaa7 Mon Sep 17 00:00:00 2001 From: Omar Bautista Date: Fri, 22 Sep 2017 11:13:53 -0700 Subject: [PATCH] Interface implementation for business logic for Parking Lot Changes on this commit: - Interface and implementation for business logic - Car now have a new constructor tu define the plates - Car now call their internal constructors and a last call to super class - App moved outside to clarify that is the main class - ParkingLot logic moved to service implementation and only initilization remains on class --- .../nearsoft/javaschool/{domain => }/App.java | 26 ++++--- src/com/nearsoft/javaschool/domain/Car.java | 11 ++- .../javaschool/domain/Motorcycle.java | 2 +- .../javaschool/domain/ParkingLot.java | 51 -------------- .../nearsoft/javaschool/domain/Vehicle.java | 5 +- .../javaschool/service/ParkingLotService.java | 16 +++++ .../service/impl/ParkingLotServiceImpl.java | 70 +++++++++++++++++++ 7 files changed, 116 insertions(+), 65 deletions(-) rename src/com/nearsoft/javaschool/{domain => }/App.java (62%) create mode 100644 src/com/nearsoft/javaschool/service/ParkingLotService.java create mode 100644 src/com/nearsoft/javaschool/service/impl/ParkingLotServiceImpl.java diff --git a/src/com/nearsoft/javaschool/domain/App.java b/src/com/nearsoft/javaschool/App.java similarity index 62% rename from src/com/nearsoft/javaschool/domain/App.java rename to src/com/nearsoft/javaschool/App.java index ece90a2..99e5369 100644 --- a/src/com/nearsoft/javaschool/domain/App.java +++ b/src/com/nearsoft/javaschool/App.java @@ -1,6 +1,12 @@ -package com.nearsoft.javaschool.domain; +package com.nearsoft.javaschool; +import com.nearsoft.javaschool.domain.Car; +import com.nearsoft.javaschool.domain.Motorcycle; +import com.nearsoft.javaschool.domain.ParkingLot; +import com.nearsoft.javaschool.domain.Vehicle; import com.nearsoft.javaschool.enums.CarType; +import com.nearsoft.javaschool.service.ParkingLotService; +import com.nearsoft.javaschool.service.impl.ParkingLotServiceImpl; import java.util.ArrayList; import java.util.List; @@ -12,21 +18,25 @@ public class App { public static void main(String[] args) throws InterruptedException { + // Create a new parking lot ParkingLot parkingLot = new ParkingLot("Java School", 3); - System.out.println("Total of available spots: " + parkingLot.getFreeSpots()); + // Initialize the service to perform operations + ParkingLotService parkingLotService = new ParkingLotServiceImpl(parkingLot); + + System.out.println("Total of available spots: " + parkingLotService.getFreeSpots()); //creates vehicles List cars = new ArrayList<>(); - cars.add(new Car()); cars.add(new Car(CarType.SUV)); + cars.add(new Car(CarType.SEDAN, "UK-2348")); cars.add(new Motorcycle()); cars.add(new Car()); for (Vehicle car : cars) { - System.out.println("Total of available spots: " + parkingLot.getFreeSpots()); - if (parkingLot.park(car)) { + System.out.println("Total of available spots: " + parkingLotService.getFreeSpots()); + if (parkingLotService.park(car)) { System.out.println("The car: " + car.getPlates() + " was parked successfully."); } else { System.out.println("There is no empty space to park."); @@ -35,8 +45,8 @@ public static void main(String[] args) throws InterruptedException { for (Vehicle car : cars) { - System.out.println("Total of available spots: " + parkingLot.getFreeSpots()); - double fare = parkingLot.clearSpot(car); + System.out.println("Total of available spots: " + parkingLotService.getFreeSpots()); + double fare = parkingLotService.clearSpot(car); if (fare > 0) { System.out.println("Total fare: " + fare); System.out.println("The car: " + car.getPlates() + " was unparked successfully."); @@ -45,7 +55,7 @@ public static void main(String[] args) throws InterruptedException { } } - System.out.println("Total of available spots: " + parkingLot.getFreeSpots()); + System.out.println("Total of available spots: " + parkingLotService.getFreeSpots()); } diff --git a/src/com/nearsoft/javaschool/domain/Car.java b/src/com/nearsoft/javaschool/domain/Car.java index 9c9a511..258cd1e 100644 --- a/src/com/nearsoft/javaschool/domain/Car.java +++ b/src/com/nearsoft/javaschool/domain/Car.java @@ -3,18 +3,23 @@ import com.nearsoft.javaschool.enums.CarType; /** - * Created by pruiz on 9/21/17. + * This example shows the interaction with the Constructors + * on the same class and the super class. */ public class Car extends Vehicle { private CarType type = CarType.SEDAN; public Car() { - super(4); + this(CarType.SEDAN); } public Car(CarType type) { - super(4); + this(type, "UNDEFINED"); + } + + public Car(CarType type, String plates){ + super(4,plates); this.type = type; } diff --git a/src/com/nearsoft/javaschool/domain/Motorcycle.java b/src/com/nearsoft/javaschool/domain/Motorcycle.java index aef44aa..df85939 100644 --- a/src/com/nearsoft/javaschool/domain/Motorcycle.java +++ b/src/com/nearsoft/javaschool/domain/Motorcycle.java @@ -3,7 +3,7 @@ public class Motorcycle extends Vehicle { public Motorcycle() { - super(2); + super(2, "UNDEFINED"); } @Override diff --git a/src/com/nearsoft/javaschool/domain/ParkingLot.java b/src/com/nearsoft/javaschool/domain/ParkingLot.java index 5ae23f3..36795a3 100644 --- a/src/com/nearsoft/javaschool/domain/ParkingLot.java +++ b/src/com/nearsoft/javaschool/domain/ParkingLot.java @@ -1,9 +1,5 @@ package com.nearsoft.javaschool.domain; - -import java.time.Duration; -import java.time.LocalDateTime; - public class ParkingLot { private Spot[] spots; @@ -54,51 +50,4 @@ public int hashCode() { return name != null ? name.hashCode() : 0; } - public int getFreeSpots() { - int freeSpots = 0; - for (Spot spot : spots) { - if (spot.isAvailable()) { - freeSpots++; - } - } - return freeSpots; - } - - public boolean park(Vehicle car) { - - if (getFreeSpots() > 0) { - for (int i = 0; i < spots.length; i++) { - if (spots[i].isAvailable()) { - spots[i].setCar(car); - break; - } - } - return true; - } else { - return false; - } - - } - - public double clearSpot(Vehicle vehicle) { - - double fare = 0.0; - for (int i = 0; i < spots.length; i++) { - if (spots[i].getCar() == null) - continue; - - if (spots[i].getCar().equals(vehicle)) { - - double diff = Math.ceil(Duration.between(spots[i].getArrivalTime(), LocalDateTime.now()).toMinutes() / 60); - - fare = spots[i].getCar().getHourPrice() * diff; - - spots[i].setCar(null); - - return fare; - } - } - return fare; - - } } diff --git a/src/com/nearsoft/javaschool/domain/Vehicle.java b/src/com/nearsoft/javaschool/domain/Vehicle.java index d8b67f5..856b0a7 100644 --- a/src/com/nearsoft/javaschool/domain/Vehicle.java +++ b/src/com/nearsoft/javaschool/domain/Vehicle.java @@ -6,8 +6,9 @@ public abstract class Vehicle { private int numberOfWheels; - public Vehicle(int numberOfWheels) { + public Vehicle(int numberOfWheels, String plates) { this.numberOfWheels = numberOfWheels; + this.plates = plates; } public String getPlates() { @@ -24,4 +25,4 @@ public int getNumberOfWheels() { public abstract float getHourPrice(); -} \ No newline at end of file +} diff --git a/src/com/nearsoft/javaschool/service/ParkingLotService.java b/src/com/nearsoft/javaschool/service/ParkingLotService.java new file mode 100644 index 0000000..416304d --- /dev/null +++ b/src/com/nearsoft/javaschool/service/ParkingLotService.java @@ -0,0 +1,16 @@ +package com.nearsoft.javaschool.service; + +import com.nearsoft.javaschool.domain.Vehicle; + +/** + * Created by obautista on 9/22/17. + */ +public interface ParkingLotService { + + int getFreeSpots(); + + boolean park(Vehicle car); + + double clearSpot(Vehicle vehicle); + +} diff --git a/src/com/nearsoft/javaschool/service/impl/ParkingLotServiceImpl.java b/src/com/nearsoft/javaschool/service/impl/ParkingLotServiceImpl.java new file mode 100644 index 0000000..85ba3b4 --- /dev/null +++ b/src/com/nearsoft/javaschool/service/impl/ParkingLotServiceImpl.java @@ -0,0 +1,70 @@ +package com.nearsoft.javaschool.service.impl; + +import com.nearsoft.javaschool.domain.ParkingLot; +import com.nearsoft.javaschool.domain.Spot; +import com.nearsoft.javaschool.domain.Vehicle; +import com.nearsoft.javaschool.service.ParkingLotService; + +import java.time.Duration; +import java.time.LocalDateTime; + +/** + * Created by obautista on 9/22/17. + */ +public class ParkingLotServiceImpl implements ParkingLotService { + + private static ParkingLot parkingLot; + + public ParkingLotServiceImpl(ParkingLot parkingLot){ + this.parkingLot = parkingLot; + } + + public int getFreeSpots() { + int freeSpots = 0; + for (Spot spot : parkingLot.getSpots()) { + if (spot.isAvailable()) { + freeSpots++; + } + } + return freeSpots; + } + + public boolean park(Vehicle car) { + Spot[] spots = parkingLot.getSpots(); + if (getFreeSpots() > 0) { + for (int i = 0; i < spots.length; i++) { + if (spots[i].isAvailable()) { + spots[i].setCar(car); + break; + } + } + return true; + } else { + return false; + } + + } + + public double clearSpot(Vehicle vehicle) { + Spot[] spots = parkingLot.getSpots(); + double fare = 0.0; + for (int i = 0; i < spots.length; i++) { + if (spots[i].getCar() == null) + continue; + + if (spots[i].getCar().equals(vehicle)) { + + double diff = Math.ceil( + Duration.between(spots[i].getArrivalTime(), LocalDateTime.now()).toMinutes() / 60); + + fare = spots[i].getCar().getHourPrice() * diff; + + spots[i].setCar(null); + + return fare; + } + } + return fare; + + } +}