#include <iostream>
#include <iomanip> //for setprecision on cout
#include <ctime>
#include <unistd.h> //for sleep
using namespace std;
class ParkingSpace
{
public:
ParkingSpace();
~ParkingSpace();
void becomesTaken(void);
void becomesFree(void);
time_t start, end, elapsed_secs;
};
ParkingSpace::ParkingSpace(){}
ParkingSpace::~ParkingSpace(){}
void ParkingSpace::becomesTaken(void){
cout << "somebody just parked" << endl;
start=time(NULL);
}
void ParkingSpace::becomesFree(void){
cout << "a space was freed" << endl;
end=time(NULL);
elapsed_secs=difftime(end, start);
}
class ParkingLot
{
ParkingSpace parkingspacearray[3];
double pricepersec;
long double revenue;
unsigned int revenue_cents;
public:
ParkingLot();
~ParkingLot();
void test(void);
void calcrevenue(time_t);
void askformoney(ParkingSpace);
};
ParkingLot::ParkingLot(void){pricepersec=132.25;}
ParkingLot::~ParkingLot(void){}
void ParkingLot::test(void){
parkingspacearray[0].becomesTaken();
sleep(2);
parkingspacearray[0].becomesFree();
calcrevenue(parkingspacearray[0].elapsed_secs);
askformoney(parkingspacearray[0]);
}
void ParkingLot::calcrevenue(time_t elapsed_secs){
revenue=elapsed_secs*pricepersec;
revenue_cents=(int)(revenue*100);
}
void ParkingLot::askformoney(ParkingSpace spaceinquestion){
char *start_str, *end_str;
start_str = ctime(&spaceinquestion.start);
end_str = ctime(&spaceinquestion.end);
cout << "You used our services from/to " << endl << spaceinquestion.start << " " << start_str << spaceinquestion.end << " " << end_str << endl;
cout << fixed << setprecision(2);
cout << "So you owe us $" << (double) revenue_cents/100 << endl;
}
int main(void){
ParkingLot building;
building.test();
return 0;
}
So this is what happens when I run it:
somebody just parked
a space was freed
You used our services from/to
1423719995 Thu Feb 12 13:46:37 2015
1423719997 Thu Feb 12 13:46:37 2015
So you owe us $264.50
Why are the start and end times the same? They should be 2 secs apart!