Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interface implementation for business logic for Parking Lot #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Vehicle> 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.");
Expand All @@ -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.");
Expand All @@ -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());

}

Expand Down
11 changes: 8 additions & 3 deletions src/com/nearsoft/javaschool/domain/Car.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/com/nearsoft/javaschool/domain/Motorcycle.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class Motorcycle extends Vehicle {

public Motorcycle() {
super(2);
super(2, "UNDEFINED");
}

@Override
Expand Down
51 changes: 0 additions & 51 deletions src/com/nearsoft/javaschool/domain/ParkingLot.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.nearsoft.javaschool.domain;


import java.time.Duration;
import java.time.LocalDateTime;

public class ParkingLot {

private Spot[] spots;
Expand Down Expand Up @@ -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;

}
}
5 changes: 3 additions & 2 deletions src/com/nearsoft/javaschool/domain/Vehicle.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -24,4 +25,4 @@ public int getNumberOfWheels() {

public abstract float getHourPrice();

}
}
16 changes: 16 additions & 0 deletions src/com/nearsoft/javaschool/service/ParkingLotService.java
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
@@ -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;

}
}