C++ Parking ticket 2
Parking Ticket Code/ParkedCar.cpp
Parking Ticket Code/ParkedCar.cpp
// Implementation file for the ParkedCar class
#include
<
iostream
>
#include
"ParkedCar.h"
using
namespace
std
;
// Default Constructor
ParkedCar
::
ParkedCar
()
{
make
=
""
;
model
=
""
;
color
=
""
;
licenseNumber
=
""
;
minutesParked
=
0
;
}
// Constructor
// Parameters:
// mk The car's make.
// mod The car's model.
// col The car's color.
// lic The car's license number.
// min The number of minutes parked.
ParkedCar
::
ParkedCar
(
string mk
,
string mod
,
string col
,
string lic
,
int
min
)
{
make
=
mk
;
model
=
mod
;
color
=
col
;
licenseNumber
=
lic
;
minutesParked
=
min
;
}
// Copy constructor
ParkedCar
::
ParkedCar
(
const
ParkedCar
&
car2
)
{
make
=
car2
.
make
;
model
=
car2
.
model
;
color
=
car2
.
color
;
licenseNumber
=
car2
.
licenseNumber
;
minutesParked
=
car2
.
minutesParked
;
}
// print function
void
ParkedCar
::
print
()
{
cout
<<
"Car Information:\n"
;
cout
<<
"\tMake: "
<<
make
<<
endl
;
cout
<<
"\tmodel: "
<<
model
<<
endl
;
cout
<<
"\tColor: "
<<
color
<<
endl
;
cout
<<
"\tLicense Number: "
<<
licenseNumber
<<
endl
;
cout
<<
"\tMinutes Parked: "
<<
minutesParked
<<
endl
;
}
Parking Ticket Code/ParkedCar.h
// Specification file for the ParkedCar class #ifndef PARKED_CAR_H #define PARKED_CAR_H #include<string> using namespace std; // ParkedCar class class ParkedCar { private: string make; // The car's make string model; // The car's model string color; // The car's color string licenseNumber; // The car's license number int minutesParked; // Minutes parked public: // Default constructor ParkedCar(); // Constructor ParkedCar(string, string, string, string, int); // Copy constructor ParkedCar(const ParkedCar &); // Mutators void setMake(string m) { make = m; } void setModel(string m) { model = m; } void setColor(string c) { color = c; } void setLicenseNumber(string lic) { licenseNumber = lic; } void setMinutesParked(int m) { minutesParked = m; } // Accessors string getMake() const { return make; } string getModel() const { return model;} string getColor() const { return color; } string getLicenseNumber() const { return licenseNumber; } int getMinutesParked() const { return minutesParked; } // print function void print(); }; #endif
Parking Ticket Code/ParkingMeter.h
// Specification file for the ParkingMeter class #ifndef PARKING_METER_H #define PARKING_METER_H #include <iostream> using namespace std; // ParkingMeter class class ParkingMeter { private: int minutesPurchased; // Minutes purchased public: // Default constructor ParkingMeter() { minutesPurchased = 0; } // Constructor. The parameter m is // the number of minutes purchased. ParkingMeter(int m) { minutesPurchased = m; } // Mutator void setMinutesPurchased(int m) { minutesPurchased = m; } // Accessor int getMinutesPurchased() const { return minutesPurchased; } // print function void print() { cout << "Meter Information:\n"; cout << "\tMinutes Purchases: " << minutesPurchased << endl; } }; #endif
Parking Ticket Code/ParkingTicket.cpp
Parking Ticket Code/ParkingTicket.cpp
// Implementation file for the ParkingTicket class
#include
"ParkingTicket.h"
#include
<
iostream
>
#include
<
iomanip
>
using
namespace
std
;
// Default constructor
ParkingTicket
::
ParkingTicket
()
{
fine
=
0.0
;
minutes
=
0
;
}
// Constructor
// Parameters:
// aCar - A ParkedCar object.
// min - Minutes illegally parked.
ParkingTicket
::
ParkingTicket
(
ParkedCar
aCar
,
int
min
)
{
car
=
aCar
;
minutes
=
min
;
// Calculate the fine.
calculateFine
();
}
// Copy constructor
ParkingTicket
::
ParkingTicket
(
const
ParkingTicket
&
ticket2
)
{
car
=
ticket2
.
car
;
fine
=
ticket2
.
fine
;
}
// calculateFine method
// This method calculates the amount of the parking fine.
void
ParkingTicket
::
calculateFine
()
{
// Get the time parked in hours.
double
hours
=
minutes
/
60.0
;
// Get the hours as an int.
int
hoursAsInt
=
static_cast
<
int
>
(
hours
);
// If there was a portion of an hour, round up.
if
((
hours
-
hoursAsInt
)
>
0
)
hoursAsInt
++
;
// Assign the base fine.
fine
=
BASE_FINE
;
// Add the additional hourly fines.
fine
+=
(
hoursAsInt
*
HOURLY_FINE
);
}
// print function
void
ParkingTicket
::
print
()
{
// Print car information.
car
.
print
();
// Print ticket information.
cout
<<
"Ticket Information:\n"
;
cout
<<
"\tMinutes in violation: "
<<
minutes
<<
endl
;
cout
<<
"\tFine: $ "
<<
setprecision
(
2
)
<<
fixed
<<
showpoint
<<
fine
<<
endl
;
}
Parking Ticket Code/ParkingTicket.h
// Specification file for the ParkingTicket class #ifndef PARKING_TICKET_H #define PARKING_TICKET_H #include "ParkedCar.h" // Constant for the base fine. const double BASE_FINE = 25.0; // Constant for the additional hourly fine. const double HOURLY_FINE = 10.0; // ParkingTicket class class ParkingTicket { private: ParkedCar car; // The parked car double fine; // The parking fine int minutes; // Minutes illegally parked // calculateFine method // This method calculates the amount of the parking fine. void calculateFine(); public: // Default Constructor ParkingTicket(); // Constructor ParkingTicket(ParkedCar, int); // Copy constructor ParkingTicket(const ParkingTicket &); // Mutators void setCar(ParkedCar c) { car = c; } void setMinutes(int m) { minutes = m; } // Accessors ParkedCar getCar() const { return car; } double getFine() const { return fine; } // print function void print(); }; #endif
Parking Ticket Code/PoliceOfficer.cpp
Parking Ticket Code/PoliceOfficer.cpp
// Implementation file for the PoliceOfficer class
#include
"ParkedCar.h"
#include
"ParkingMeter.h"
#include
"ParkingTicket.h"
#include
"PoliceOfficer.h"
#include
<
iostream
>
using
namespace
std
;
// The patrol function looks at the number of
// minutes a car has been parked and the number
// of minutes purchased. If the minutes parked is
// greater than the minutes purchased, a pointer
// to a ParkingTicket object is returned. Otherwise
// the function returns a nullptr.
ParkingTicket
*
PoliceOfficer
::
patrol
(
ParkedCar
car
,
ParkingMeter
meter
)
{
// Get the minutes parked over the amount purchased.
int
illegalMinutes
=
car
.
getMinutesParked
()
-
meter
.
getMinutesPurchased
();
// Determine whether the car is illegally parked.
if
(
illegalMinutes
>
0
)
{
// Yes, it is illegally parked.
// Create a ParkingTicket object.
ticket
=
new
ParkingTicket
(
car
,
illegalMinutes
);
}
// Return the ticket, if any.
return
ticket
;
}
// print function
void
PoliceOfficer
::
print
()
{
cout
<<
"Police Officer Information:\n"
;
cout
<<
"\tName: "
<<
name
<<
endl
;
cout
<<
"\tBadge Number: "
<<
badgeNumber
<<
endl
;
}
Parking Ticket Code/PoliceOfficer.h
// Specification file for the PoliceOfficer class #ifndef POLICE_OFFICER_H #define POLICE_OFFICER_H #include "ParkedCar.h" #include "ParkingMeter.h" #include "ParkingTicket.h" // PoliceOfficer class class PoliceOfficer { private: string name; // Officer's name string badgeNumber; // Badge number ParkingTicket *ticket; // Pointer to a ticket public: // Default constructor PoliceOfficer() { name = ""; badgeNumber = ""; ticket = nullptr; } // Constructor // The parameter n is the officer's name. // The parameter bn is the officer's badge number. PoliceOfficer(string n, string bn) { name = n; badgeNumber = bn; ticket = nullptr; } // Copy constructor PoliceOfficer(const PoliceOfficer &officer2) { name = officer2.name; badgeNumber = officer2.badgeNumber; ticket = new ParkingTicket(*officer2.ticket); } // Mutators void setName(string n) { name = n; } void setBadgeNumber(string b) { badgeNumber = b; } // Accessors string getName() { return name; } string getBadgeNumber() { return badgeNumber; } // patrol function ParkingTicket *patrol(ParkedCar, ParkingMeter); // print function void print(); }; #endif
Parking Ticket Code/spc14-14.cpp
Parking Ticket Code/spc14-14.cpp
// Chapter 14, Programming Challenge 14: Parking Ticket Simulator
#include
<
iostream
>
#include
"ParkedCar.h"
#include
"ParkingMeter.h"
#include
"ParkingTicket.h"
#include
"PoliceOfficer.h"
using
namespace
std
;
int
main
()
{
// Create a ParkingTicket pointer. If a parking ticket
// is issued, this will point to it.
ParkingTicket
*
ticket
=
nullptr
;
// Create a ParkedCar object.
// The car was parked for 125 minutes.
ParkedCar
car
(
"Volkswagen"
,
"1972"
,
"Red"
,
"147RHZM"
,
125
);
// Create a ParkingMeter object.
// 60 minutes were purchased.
ParkingMeter
meter
(
60
);
// Create a PoliceOfficer object.
PoliceOfficer
officer
(
"Joe Friday"
,
"4788"
);
// Let the officer patrol.
ticket
=
officer
.
patrol
(
car
,
meter
);
if
(
ticket
!=
nullptr
)
{
// Display the officer information.
officer
.
print
();
// Display the ticket information.
ticket
->
print
();
// We're done with the ticket, so delete it.
delete
ticket
;
ticket
=
nullptr
;
}
else
cout
<<
"No crimes were committed.\n"
;
return
0
;
}