diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ParkingLot/Client.java b/src/ParkingLot/Client.java index 319b4e7..aaf9245 100644 --- a/src/ParkingLot/Client.java +++ b/src/ParkingLot/Client.java @@ -2,20 +2,25 @@ import ParkingLot.controllers.TicketController; import ParkingLot.dtos.IssueTicketRequestDTO; +import ParkingLot.dtos.IssueTicketResponseDTO; +import ParkingLot.models.Bill; import ParkingLot.models.Gate; +import ParkingLot.models.GateType; import ParkingLot.models.VehicleType; -import ParkingLot.repositories.GateRepository; -import ParkingLot.repositories.ParkingLotRepository; -import ParkingLot.repositories.TicketRepository; -import ParkingLot.repositories.VehicleRepository; +import ParkingLot.repositories.*; +import ParkingLot.service.BillService; import ParkingLot.service.TicketService; +import java.util.Scanner; + public class Client { public static void main(String[] args) { + Scanner sc = new Scanner(System.in); VehicleRepository vehicleRepository = new VehicleRepository(); TicketRepository ticketRepository = new TicketRepository(); ParkingLotRepository parkingLotRepository = new ParkingLotRepository(); GateRepository gateRepository = new GateRepository(); + BillRepository billRepository = new BillRepository(); TicketService ticketService = new TicketService( gateRepository, @@ -29,11 +34,22 @@ public static void main(String[] args) { IssueTicketRequestDTO request = new IssueTicketRequestDTO(); request.setGateId(1); request.setOwnerName("Mohit"); - request.setVehicleType(VehicleType.AUTO); + request.setVehicleType(VehicleType.CAR); request.setVehicleNumber("DL 1VC 0001"); request.setParkingLotId(1); - ticketController.issueTicket(request); + IssueTicketResponseDTO responseDTO = ticketController.issueTicket(request); + System.out.println("Ticket Issued : "+responseDTO); + + + BillService billService = new BillService(ticketRepository,billRepository,gateRepository); + int durationInHours = sc.nextInt(); + int exitGateId = 2; + Gate gate = new Gate(2,"Ex-G1", GateType.EXIT); + gateRepository.save(gate); + Gate gte = gateRepository.findGateById(exitGateId).get(); + Bill bill = billService.generateBill(responseDTO,durationInHours,exitGateId); + System.out.println(bill); } } diff --git a/src/ParkingLot/dtos/IssueTicketResponseDTO.java b/src/ParkingLot/dtos/IssueTicketResponseDTO.java index 61cbd16..b06db18 100644 --- a/src/ParkingLot/dtos/IssueTicketResponseDTO.java +++ b/src/ParkingLot/dtos/IssueTicketResponseDTO.java @@ -29,4 +29,12 @@ public void setTicketId(int ticketId) { this.ticketId = ticketId; } + @Override + public String toString() { + return "IssueTicketResponseDTO{" + + "ticketId=" + ticketId + + ", parkingSlotNumber='" + parkingSlotNumber + '\'' + + ", responseStatus=" + responseStatus + + '}'; + } } diff --git a/src/ParkingLot/models/Bill.java b/src/ParkingLot/models/Bill.java index 95b3a89..15bdec0 100644 --- a/src/ParkingLot/models/Bill.java +++ b/src/ParkingLot/models/Bill.java @@ -13,6 +13,12 @@ public class Bill extends BaseModel{ // there can be partial payments // Cash : 20 , UPI : 30 // AmazonPay : 20 , CC : 50 + public Bill(Ticket ticket, int amount, Gate gate, Operator operator) { + this.ticket = ticket; + this.amount = amount; + this.gate = gate; + this.operator = operator; + } public Date getExitTime() { @@ -62,6 +68,18 @@ public List getPayment() { public void setPayment(List payment) { this.payment = payment; } + + @Override + public String toString() { + return "Bill{" + + "exitTime=" + exitTime + + ", ticket=" + ticket + + ", amount=" + amount + + ", gate=" + gate + + ", operator=" + operator + + ", payment=" + payment + + '}'; + } } // double amount diff --git a/src/ParkingLot/models/Gate.java b/src/ParkingLot/models/Gate.java index 7b05bd0..3fa53a9 100644 --- a/src/ParkingLot/models/Gate.java +++ b/src/ParkingLot/models/Gate.java @@ -6,6 +6,16 @@ public class Gate extends BaseModel{ private Operator currentOperator; private GateStatus gateStatus; + public Gate() { + } + public Gate(int id,String gateNumber, GateType gateType) { + this.setId(id); + this.gateNumber = gateNumber; + this.gateType = gateType; + this.currentOperator = new Operator("Operator-1"); + this.gateStatus = GateStatus.OPEN; + } + public String getGateNumber() { return gateNumber; } @@ -37,4 +47,14 @@ public GateStatus getGateStatus() { public void setGateStatus(GateStatus gateStatus) { this.gateStatus = gateStatus; } + + @Override + public String toString() { + return "Gate{" + + "gateNumber='" + gateNumber + '\'' + + ", gateType=" + gateType + + ", currentOperator=" + currentOperator + + ", gateStatus=" + gateStatus + + '}'; + } } diff --git a/src/ParkingLot/models/Operator.java b/src/ParkingLot/models/Operator.java index 382f67d..4f0363b 100644 --- a/src/ParkingLot/models/Operator.java +++ b/src/ParkingLot/models/Operator.java @@ -3,4 +3,16 @@ public class Operator extends BaseModel{ private String Name; private Gate gate; + + public Operator(String name) { + super(); + this.Name = name; + } + + @Override + public String toString() { + return "Operator{" + + "Name='" + Name + '\'' + + '}'; + } } diff --git a/src/ParkingLot/models/ParkingFloor.java b/src/ParkingLot/models/ParkingFloor.java index 697ccb9..db76e48 100644 --- a/src/ParkingLot/models/ParkingFloor.java +++ b/src/ParkingLot/models/ParkingFloor.java @@ -1,5 +1,6 @@ package ParkingLot.models; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -9,6 +10,11 @@ public class ParkingFloor extends BaseModel { private ParkingFloorStatus parkingFloorStatus; private List vehicleCapacities; + public ParkingFloor() { + this.parkingSlots = new ArrayList(); + parkingSlots.add(new ParkingSlot()); + } + public String getFloorName() { return floorName; } diff --git a/src/ParkingLot/models/ParkingSlot.java b/src/ParkingLot/models/ParkingSlot.java index 423e6b6..439942a 100644 --- a/src/ParkingLot/models/ParkingSlot.java +++ b/src/ParkingLot/models/ParkingSlot.java @@ -6,6 +6,12 @@ public class ParkingSlot extends BaseModel { private VehicleType vehicleType; private ParkingFloor parkingFloor; + public ParkingSlot() { + this.parkingSlotStatus = ParkingSlotStatus.EMPTY; + this.vehicleType = VehicleType.CAR; + this.slotNumber = "SLOT-0"; + } + public ParkingFloor getParkingFloor() { return parkingFloor; } diff --git a/src/ParkingLot/repositories/BillRepository.java b/src/ParkingLot/repositories/BillRepository.java new file mode 100644 index 0000000..7d892ca --- /dev/null +++ b/src/ParkingLot/repositories/BillRepository.java @@ -0,0 +1,24 @@ +package ParkingLot.repositories; + +import ParkingLot.models.Bill; +import ParkingLot.models.Ticket; + +import java.util.Map; +import java.util.Optional; +import java.util.TreeMap; + +public class BillRepository { + private static Map bills = new TreeMap<>(); + private static int id =1; + public BillRepository() { +// bills.put(id, new Bill(id++)); + } + + public void saveBill(Bill bill){ + bills.put(id++, bill); + } + + public Optional getBill(int id){ + return Optional.ofNullable(bills.get(id)); + } +} diff --git a/src/ParkingLot/repositories/GateRepository.java b/src/ParkingLot/repositories/GateRepository.java index 5a401cd..af38235 100644 --- a/src/ParkingLot/repositories/GateRepository.java +++ b/src/ParkingLot/repositories/GateRepository.java @@ -2,11 +2,28 @@ import ParkingLot.models.Gate; +import java.util.Map; import java.util.Optional; +import java.util.TreeMap; public class GateRepository { + private static final Map gateMap = new TreeMap<>(); + private static int idCounter =1; + public GateRepository() { + Gate gate = new Gate(); + gate.setId(idCounter); + gateMap.put(idCounter, gate); + idCounter++; + } + public Optional findGateById(int gateId){ - return Optional.empty(); + if(!gateMap.containsKey(gateId)) + return Optional.empty(); + return Optional.of(gateMap.get(gateId)); + } + + public void save(Gate gate) { + gateMap.put(gate.getId(), gate); } } // ORM diff --git a/src/ParkingLot/repositories/ParkingLotRepository.java b/src/ParkingLot/repositories/ParkingLotRepository.java index 8c3f378..73d1a00 100644 --- a/src/ParkingLot/repositories/ParkingLotRepository.java +++ b/src/ParkingLot/repositories/ParkingLotRepository.java @@ -9,15 +9,15 @@ import java.util.TreeMap; public class ParkingLotRepository { - private Map parkingLots = new TreeMap<>(); - + private static final Map parkingLots = new TreeMap<>(); + private static int idCounter = 1; public ParkingLotRepository() { ParkingLot parkingLot = new ParkingLot(); - parkingLot.setId(1); + parkingLot.setId(idCounter++); parkingLots.put(parkingLot.getId(), parkingLot); } - ParkingLot findParkingLotByGate(Gate gate){ + public ParkingLot findParkingLotByGate(Gate gate){ for(ParkingLot parkingLot : parkingLots.values()){ for(Gate gate1 : parkingLot.getEntryGates()){ if(gate1.getId() == gate.getId()){ diff --git a/src/ParkingLot/repositories/TicketRepository.java b/src/ParkingLot/repositories/TicketRepository.java index f4fdbf1..4cdefa3 100644 --- a/src/ParkingLot/repositories/TicketRepository.java +++ b/src/ParkingLot/repositories/TicketRepository.java @@ -3,6 +3,7 @@ import ParkingLot.models.Ticket; import java.util.Map; +import java.util.Optional; import java.util.TreeMap; public class TicketRepository { @@ -16,5 +17,9 @@ public Ticket save(Ticket ticket){ return ticket; } + public Optional findById(int id){ + return Optional.ofNullable(tickets.get(id)); + } + } diff --git a/src/ParkingLot/repositories/VehicleRepository.java b/src/ParkingLot/repositories/VehicleRepository.java index 66090bc..500e987 100644 --- a/src/ParkingLot/repositories/VehicleRepository.java +++ b/src/ParkingLot/repositories/VehicleRepository.java @@ -2,16 +2,29 @@ import ParkingLot.models.Vehicle; +import java.util.HashMap; +import java.util.Map; import java.util.Optional; +import java.util.TreeMap; public class VehicleRepository { + + static Map vehicleDb = new TreeMap<>(); + static int idCounter = 0; + public Optional findVehicleByVehicleNumber(String vehicleNumber){ - return Optional.empty(); + if(!vehicleDb.containsKey(vehicleNumber)) + return Optional.empty(); + return Optional.of(vehicleDb.get(vehicleNumber)); } public Vehicle save(Vehicle vehicle){ // save this to db - vehicle.setId(1); + vehicle.setId(idCounter++); + vehicle.setOwnerName(vehicle.getOwnerName()); + vehicle.setLicensePlate(vehicle.getLicensePlate()); + vehicle.setVehicleType(vehicle.getVehicleType()); + vehicleDb.put(vehicle.getLicensePlate(),vehicle); return vehicle; } } diff --git a/src/ParkingLot/service/BillService.java b/src/ParkingLot/service/BillService.java new file mode 100644 index 0000000..750726f --- /dev/null +++ b/src/ParkingLot/service/BillService.java @@ -0,0 +1,45 @@ +package ParkingLot.service; + +import ParkingLot.dtos.IssueTicketResponseDTO; +import ParkingLot.dtos.ResponseStatus; +import ParkingLot.models.Bill; +import ParkingLot.models.Gate; +import ParkingLot.models.Ticket; +import ParkingLot.models.Vehicle; +import ParkingLot.repositories.BillRepository; +import ParkingLot.repositories.GateRepository; +import ParkingLot.repositories.TicketRepository; +import ParkingLot.strategies.AmountCalculationStrategy; + +import java.util.Optional; + +public class BillService { + private TicketRepository ticketRepository; + private BillRepository billRepository; + private GateRepository gateRepository; + private AmountCalculationStrategy amountCalculationStrategy; + + public BillService(TicketRepository ticketRepository, BillRepository billRepository, GateRepository gateRepository) { + this.ticketRepository = ticketRepository; + this.billRepository = billRepository; + this.gateRepository = gateRepository; + this.amountCalculationStrategy = new AmountCalculationStrategy(); + } + public Bill generateBill(IssueTicketResponseDTO responseDTO, int durationInHours, int exitGateId) { + if(responseDTO.getResponseStatus()== ResponseStatus.FAILURE){ + return null; + } + Optional ticketOptional = ticketRepository.findById(responseDTO.getTicketId()); + Ticket ticket; + ticket = ticketOptional.orElseGet(Ticket::new); + int amount = getAmount(ticket.getVehicle(), durationInHours); + Optional gateOptional = gateRepository.findGateById(exitGateId); + Gate gate = gateOptional.orElseGet(Gate::new); + return new Bill(ticket,amount,gate,gate.getCurrentOperator()); + } + + private int getAmount(Vehicle vehicle, int durationInHours) { + if(vehicle==null) return 0; + return amountCalculationStrategy.getAmount(vehicle.getVehicleType(),durationInHours); + } +} diff --git a/src/ParkingLot/strategies/AmountCalculationStrategy.java b/src/ParkingLot/strategies/AmountCalculationStrategy.java new file mode 100644 index 0000000..23005f0 --- /dev/null +++ b/src/ParkingLot/strategies/AmountCalculationStrategy.java @@ -0,0 +1,22 @@ +package ParkingLot.strategies; + +import ParkingLot.models.VehicleType; + +public class AmountCalculationStrategy { + //implemented Simple factory. can be extended to factory method. + public int getAmount(VehicleType v,int durationInHours){ + if(durationInHours < 0) return 0; + if(v == VehicleType.BUS) { + return durationInHours * 80; + }; + if(v == VehicleType.TRUCK) { + return durationInHours * 60; + } + if(v == VehicleType.CAR) { + return durationInHours * 40; + } + else { + return durationInHours * 30; + } + } +}