Accenture Interview Question
Java DevelopersCountry: India
Interview Type: Written Test
Hi friends, I have tried to code this problem in C++ and tried to show usage of two design patterns ( singleton, observer ). Overall the code is written assuming bare minimum requirements and there is always scope of improvements. The ParkingLot class is a singleton and observer pattern is used to update the balance reports. Please provide your valuable comments.
#include "stdafx.h"
#include <iostream>
#include <list>
#include <vector>
using namespace std;
class Vehicle
{
protected:
double m_vehicleSize;
double m_timeIN;
public:
double GetVehicleSize()
{
return m_vehicleSize;
}
void SetVehicleSize( double size )
{
m_vehicleSize = size;
}
double GetTimeIN()
{
return m_timeIN;
}
void SetTimeIN( double time_in )
{
m_timeIN = time_in;
}
virtual void GetVehicleNumber()
{
}
};
class Car : public Vehicle
{
};
class Truck : public Vehicle
{
};
class BalanceReport
{
double m_balanceAmount;
public:
BalanceReport()
{
m_balanceAmount = 0;
}
void update( double fee )
{
SetBalanceAmount( fee );
}
void SetBalanceAmount( double fee )
{
m_balanceAmount += fee;
}
double GetBalanceAmount()
{
return m_balanceAmount;
}
};
class ParkingLot
{
list<Vehicle*> m_vehiclesList;
vector<BalanceReport*> observers;
double m_parkingLotSize;
double m_remaingParkingSpace;
int m_totalVehiclesEntered;
int m_totalVehicleExited;
static ParkingLot* instance;
ParkingLot()
{
m_parkingLotSize = 100 ;
m_parkingLotSize = 100;
m_totalVehiclesEntered = 0;
m_totalVehicleExited = 0;
}
ParkingLot( double parkingLotSize, double balanceAmount )
{
m_parkingLotSize = parkingLotSize;
m_remaingParkingSpace = parkingLotSize;
m_totalVehiclesEntered = 0;
m_totalVehicleExited = 0;
}
public:
static ParkingLot* GetInstance()
{
if( instance == NULL )
{
instance = new ParkingLot( 500, 0 );
}
return instance;
}
//observer pattern
void AddObserver( BalanceReport* o )
{
observers.push_back( o );
}
void RemoveObserver( BalanceReport* o )
{
observers.pop_back();
}
void EnterVehicleIntoParking( Vehicle* vehicle, double time_in )
{
vehicle->SetTimeIN( time_in );
m_vehiclesList.push_back( vehicle );
if( dynamic_cast< Car* >( vehicle ) )
{
m_remaingParkingSpace -= 1;
}
else
{
m_remaingParkingSpace -= 2;
}
m_totalVehiclesEntered += 1;
}
void NotifyObserver( double parkingFees )
{
for( vector<BalanceReport*>::iterator iter = observers.begin();
iter != observers.end(); ++iter )
{
(*iter)->update( parkingFees );
}
}
void ExitVehicleFromParking( Vehicle* vehicle, double time_out )
{
double parkingFees = 0.0;
double totalTime = time_out - vehicle->GetTimeIN();
if( dynamic_cast< Car* >( vehicle ) )
{
parkingFees = 2 * totalTime;
}
else
{
parkingFees = 3 * totalTime;
}
NotifyObserver( parkingFees );
if( dynamic_cast< Car* >( vehicle ) )
{
m_remaingParkingSpace += 1;
}
else
{
m_remaingParkingSpace += 2;
}
m_totalVehicleExited += 1;
m_vehiclesList.remove( vehicle );
}
void PrintBalanceReport()
{
cout << "Balance Amount: " << observers[0]->GetBalanceAmount() << endl // Assumption: As of now only one observer
<< "Total vehicles entered: " << m_totalVehiclesEntered << endl
<< "Total vehicles exited: " << m_totalVehicleExited << endl
<< "Remaining parking space: "<< m_remaingParkingSpace << endl;
}
};
ParkingLot* ParkingLot::instance = NULL;
int _tmain(int argc, _TCHAR* argv[])
{
ParkingLot* parking = ParkingLot::GetInstance(); //singleton parking lot class
BalanceReport* report = new BalanceReport;
parking->AddObserver( report );
Vehicle* car1 = new Car;
Vehicle* truck1 = new Truck;
parking->EnterVehicleIntoParking( car1, 2 );
parking->EnterVehicleIntoParking( truck1, 3 );
parking->ExitVehicleFromParking( car1, 10 );
parking->ExitVehicleFromParking( truck1, 12 );
parking->PrintBalanceReport();
delete report;
return 0;
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
public class Parking{
private static final String entercar="EnterCar";
private static final String entertruck="EnterTruck";
private static final String exitcar="ExitCar";
private static final String exittruck="ExitTruck";
private static final String Quit="Quit";
private static final String Report="Report";
public static void main(String [] args) throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of spaces: ");
String s=br.readLine();
int N;
try{
N=Integer.parseInt(s);
}
catch (Exception e){ System.out.println("WTF are you inputting? get the fuck out, exiting"); return;
}
ParkingLot lot= new ParkingLot(N);
boolean running =true;
while(running){
s=br.readLine();
switch (s){
case Quit: running = false; break;
case entercar: lot.EnterCar(); break;
case entertruck: lot.EnterTruck(); break;
case Report: PrintReport(lot.getReport());
default:
if (s.startsWith(exitcar)){
int time= Integer.parseInt(s.substring(exitcar.length()));
lot.ExitCar(time);
}
else if(s.startsWith(exittruck)){
int time=Integer.parseInt(s.substring(exittruck.length()));
lot.ExitTruck(time);
}
else{
System.out.println("Wrong input man");
}
}
}
}
private static void PrintReport (int[] arr){
System.out.println("Cars entered: "+ arr[0]);
System.out.println("Trucks entered: "+ arr[1]);
System.out.println("Cars exited: "+ arr[2]);
System.out.println("Trucks exited: "+ arr[3]);
System.out.println("Cars parking: "+ arr[4]);
System.out.println("Trucks parking: "+ arr[5]);
System.out.println("Spaces available: "+ arr[6]);
System.out.println("Fees paid: "+ arr[7]);
}
}
class ParkingLot {
private final int totalSpaces;
private int carsParked=0;
private int trucksParked=0;
private int carsEntered=0;
private int carsExited=0;
private int FeeCollection=0;
private int trucksEntered=0;
private int trucksExited=0;
public ParkingLot(int Spaces){
totalSpaces=Spaces;
}
public void EnterCar(){
carsParked++;
carsEntered++;
}
public void ExitCar(int time)
{
carsParked--;
carsExited++;
FeeCollection+=car.getCostPerHour()*time;
}
public void EnterTruck(){
trucksParked++;
trucksEntered++;
}
public void ExitTruck(int time){
trucksParked--;
trucksExited++;
FeeCollection+=car.getCostPerHour()*time;
}
/* populates values in an array with following order
Cars Entered: 1
Trucks Entered: 1
Cars Exited: 1
Trucks Exited: 0
Parking Cars: 0
Parking Trucks: 1
Spaces available: 13
Fees paid: $4
*/
public int[] getReport(){
int [] ans=new int[8];
ans[0]=carsEntered;
ans[1]=trucksEntered;
ans[2]=carsExited;
ans[3]=trucksExited;
ans[4]=carsParked;
ans[5]=trucksParked;
ans[6]= totalSpaces -(carsParked*car.getSpacesOccupied() + trucksParked*truck.getSpacesOccupied());
ans[7]=FeeCollection;
return ans;
}
}
class car{
private static final int _costHr=2;
private static final int _spacesOccupied=1;
public static final int getCostPerHour(){
return _costHr;
}
public static final int getSpacesOccupied(){
return _spacesOccupied;
}
}
class truck {
private static final int _costHr=3;
private static final int _spacesOccupied=2;
public static final int getCostPerHour(){
return _costHr;
}
public static final int getSpacesOccupied(){
return _spacesOccupied;
}
}
I will go for a Builder Design Pattern into it.
- Jasbir Singh April 27, 2014I have a interface Vehicle defined as:
interface Vehicle{
public String name();
public Action actionPerformed();
public float price();
}
Another interface Action defined as :
interface Action implements TimeRecord{
public Time timeWhileEntering();
public Time timeWhileExiting();
}
public class Car implements Vehicle{
public void name(){
return "car";
}
@Override
public float price(){
return 2.0;
}
}
public class Truck implements Vehicle{
public void name(){
return "truck";
}
@Override
public float price(){
return 3.0;
}
}
public Class Parking{
public list<Vehicle> vehicles = new ArrayList<Vehicle>();
public int SpaceAvailable = 0;
}
My parking class will be storing data for all the vehicles which enter or exit the parking. And will have control over the space available for parking and any time a user can get the complete data of cost as it has an ArrayList vehicle in it which will store all the details of the vehicle who entered the parking lot.
I would go ahead with something like this.