First Round - Machine Coding
Design a car parking application
There are two categories of cars: SUV and hatchback.
Maintain a count of how many SUV and hatchback cars enter the premise
Calculate the payment each car has to make based upon the rates as hatchback parking as 10 rupees per hour and SUV being 20 rupees an hour.
In case if hatchback occupancy is full then hatchback cars can occupy SUV spaces but with hatchback rates.
During exit there needs to be the system to inform the user how much they have to pay.
Admin can see all the cars which are parked in the system
my code
public class Main {
public static void main(String[] args) {
User parkingManager = new User(1,"notAdmin");
User admin = new User(2,"Admin");
Premise premise = new Premise(2, 2);
Car suv1 = new Car("123", "SUV");
Car suv2 = new Car("345", "SUV");
Car suv3 = new Car("12123", "SUV");
Car hatchback1 = new Car("121", "hatchBack");
Car hatchback2 = new Car("090", "hatchBack");
Car hatchback3 = new Car("8880", "hatchBack");
premise.addCar(hatchback1, 1);
premise.addCar(hatchback2, 2);
premise.addCar(hatchback3,5);
System.out.println(premise.addCar(suv1, 3));
// System.out.println(premise.addCar(suv2,3));
// System.out.println(premise.addCar(hatchback2, 2));
//
System.out.println(premise.removeCar(hatchback1, 4));
premise.showAllParkedCars(admin);
// System.out.println(premise.addCar(hatchback3,4));
}
}
class Car{
public String carNo;
public String carType;
public Car(String carNo, String carType){
this.carNo = carNo;
this.carType = carType;
}
}
class User{
public int userId;
public String usertype;
public User(int userId, String usertype){
this.userId = userId;
this.usertype = usertype;
}
}
class Pair{
int slotNo;
int time;
public Pair(int slotNo, int time){
this.slotNo = slotNo;
this.time = time;
}
public String toString(){
return "slot no: " + slotNo;
}
}
class Premise{
int totalSlots;
int totalEarnings = 0;
int countOfSUVSlots;
int countOfHatchBackSlots;
Map<String, Pair>suvSpace;
Map<String, Pair> hatchBackSpace;
List<Integer> freeSUVSlots;
List<Integer> freeHatchBackSlots;
public Premise(int countOfSUVSlots, int countOfHatchBackSlots){
this.countOfSUVSlots = countOfSUVSlots;
this.countOfHatchBackSlots = countOfHatchBackSlots;
suvSpace = new HashMap<>();
hatchBackSpace = new HashMap<>();
totalSlots = countOfSUVSlots + countOfHatchBackSlots;
freeSUVSlots = new ArrayList<>(countOfSUVSlots);
for(int i = 0; i < countOfSUVSlots; i++) freeSUVSlots.add(0);
freeHatchBackSlots = new ArrayList<>(countOfHatchBackSlots);
for(int i = 0; i < countOfSUVSlots; i++) freeHatchBackSlots.add(0);
}
public boolean addCar(Car car, int time) {
if (car.carType.equals("hatchBack")) {
if (countOfHatchBackSlots != 0) {
countOfHatchBackSlots--;
for (int slot = 0; slot < freeHatchBackSlots.size(); slot++) {
if (freeHatchBackSlots.get(slot) == 0) {
hatchBackSpace.put(car.carNo, new Pair(slot, time));
freeHatchBackSlots.set(slot, -1);
return true;
}
}
} else if (countOfSUVSlots != 0) {
countOfSUVSlots--;
for (int slot = 0; slot < freeSUVSlots.size(); slot++) {
if (freeSUVSlots.get(slot) == 0) {
suvSpace.put(car.carNo, new Pair(slot, time));
freeSUVSlots.set(slot, -1);
return true;
}
}
}
} else {
if (countOfSUVSlots != 0) {
countOfSUVSlots--;
for (int slot = 0; slot < freeSUVSlots.size(); slot++) {
if (freeSUVSlots.get(slot) == 0) {
suvSpace.put(car.carNo, new Pair(slot, time));
freeSUVSlots.set(slot, -1);
return true;
}
}
}
}
return false;
}
public int removeCar(Car car, int currentTime){
if(car.carType.equals("hatchBack")){
if(hatchBackSpace.containsKey(car.carNo)){
countOfHatchBackSlots++;
Pair pair = hatchBackSpace.get(car.carNo);
int slotNo = pair.slotNo;
int time = pair.time;
freeHatchBackSlots.set(slotNo,0);
hatchBackSpace.remove(car.carNo);
return (currentTime - time) * 10;
} else{
countOfSUVSlots++;
Pair pair = suvSpace.get(car.carNo);
int slotNo = pair.slotNo;
int time = pair.time;
freeSUVSlots.set(slotNo,0);
suv.remove(car.carNo);
return (currentTime - time) * 10;
}
} else{
countOfSUVSlots++;
Pair pair = suvSpace.get(car.carNo);
int slotNo = pair.slotNo;
int time = pair.time;
freeSUVSlots.set(slotNo,0);
suvSpace.remove(car.carNo);
return (currentTime - time) * 20;
}
}
public void showAllParkedCars(User user){
if(user.usertype.equals("Admin")){
System.out.println("SUV Space");
System.out.println(suvSpace);
System.out.println("___________________");
System.out.println("HatchBack space");
System.out.println(hatchBackSpace);
} else {
System.out.println("Don't have permission to see all the cars");
}
}
}
Could you please suggest what I need to improve so that I can take care of it in the future?
Comments (3)


