HELLO, PLEASE TAKE TIME TO ANSWER THIS QUESTION. PLESE ENSURE ITS CORRECT AND SH
ID: 3726335 • Letter: H
Question
HELLO, PLEASE TAKE TIME TO ANSWER THIS QUESTION. PLESE ENSURE ITS CORRECT AND SHOW THAT THE PROGRAM RUNS.
Task 2: Introduce Linked Lists
In this part you will replace the CustomerArray and VehicleArray classes with new linked list classes.
You will create new linked list classes called CustomerList and VehicleList which will hold the collection
of customers and the collection of vehicles as a linked list, respectively. You will store the customer
objects in alphabetical order based on their last name and you will store the vehicle object based on the
year of the vehicle in descending order (ie. newest first) in their respective linked lists.
The CustomerList and VehicleList classes should:
hold a pointer to the head of the list, but no pointer to the tail of the list
provide an add function that takes a Customer or Vehicle pointer and adds the object in its
correct place in the CustomerList or VehicleList class, respectively
provide a getSize function that returns the size of the list. This value should not be stored, it
should be calculated each time this function is called
provide a get function in the CustomerList that takes an integer parameter (id) and returns a
pointer to the Customer object in the list with that id. If no such object exists, return null
manage its memory to avoid memory leaks
Notes:
DO NOT use dummy nodes! Every node in each list must correspond to an object.
You will modify your program to use a CustomerList and VehicleList objects instead of a
CustomerArray and VehicleArray object to hold the objects.
All classes will continue to interact with the CustomerList and VehicleList classes the same way
they did with the CustomerArray and VehicleArray classes.
Both lists will no longer have a maximum size. This means that the add functions in both list
classes will no longer need to return an int as they should return void. You will have to change
your add functions in the Customer and Vehicle classes to reflect this change.
The changes to the rest of the classes should be minimal.
The order in which your datafill is added to the list must test all cases: adding to the front, back,
middle, etc.
Your list must be stored in proper order of at all times
You will have to update your Makefile to compile your new program.
Task 3: Modify the “Print Customer Database”
feature
Printing out the linked lists poses a new challenge in maintaining our existing design, specifically the
separation of the UI and collection classes. We are not permitted to print out data from inside the
collection class, since a collection class should not know how to interact with the outside world. We also
cannot allow the UI class to traverse the product collection in order to print it to the screen, since that
would require knowledge by the UI class of the internal configuration of the list, including the Node class,
which would also violate encapsulation rules.
Our solution here will be to implement a formatting function in the CustomerList and VehicleList classes.
These functions will have the prototype:
void toString(string& outStr)
where the outStr parameter is the result of the function concatenating all the object’s data formatted into
one long string. This long string will contain all data found by traversing the linked list. The UI class will
invoke the formatting function on the List objects, and then output the resulting formatted string to the
screen. Take a look at the current printing code in the View as a hint on how to implement these
functions. Your output does not have to be in the exact same format as before, just ensure that all of the
same information is there in a readable way.
Note: DO NOT have your UI class traverse the linked list! DO NOT print to the screen from the list class!
Task 4: Add some options to the menu
We will now add two new options to our user interface menu.
1. Option 2 on the list should now be “Add Customer ”. When the user chooses this option,
the program will prompt the use for all of the relevant information, create a customer
object and add it to the Customer list. Write the appropriate functions in the view,
controller, and shop classes (maintaining the overall proper design of the program) to do
so.
2. Option 3 on the list will now be “Add Vehicle” . When the user chooses this option the
program will prompt the user for a customer id. If it is invalid, tell the user (hint: use the
getCustomer function in the shop class mentioned at the very top of the assignment). If
not, prompt the user for all of the relevant information, create a vehicle object and add it
to the customer’s vehicle list.
In both of these cases you need to think about which classes should do the interaction with the
user, which should be creating objects, etc. You need to maintain the overall design of the
program including the separation of the view, controller and entity classes. Take a look at how
the print customer database option works and trace the code through the various classes to
understand how these functions should be implemented.
Constraints
your program must not have any memory leaks (don’t worry if valgrind reports that some memory
on the heap is “still reachable”)
do not use any global variables
your program must reuse functions everywhere possible
your program must be thoroughly commented
View.h
#ifndef VIEW_H
#define VIEW_H
#include "CustomerArray.h"
class View {
public:
void mainMenu(int&);
void printCustomers(CustomerArray&);
void pause();
private:
int readInt();
};
#endif
View.cc
#include <sstream>
#include <iomanip>
#include <iostream>
using namespace std;
#include "View.h"
#include "CustomerArray.h"
#include "Customer.h"
#include "VehicleArray.h"
#include "Vehicle.h"
void View::mainMenu(int& choice) {
string str;
choice = -1;
cout<< " **** Toby's Auto Mechanic Information Management System **** ";
cout<< " MAIN MENU ";
cout<< " 1. Print Customer Database ";
cout<< " 0. Exit ";
while (choice < 0 || choice > 1) {
cout << "Enter your selection: ";
choice = readInt();
}
if (choice == 0) { cout << endl; }
}
void View::printCustomers(CustomerArray& arr) {
cout << endl << "CUSTOMERS: " << endl << endl;
for (int i = 0; i < arr.getSize(); i++) {
Customer* cust = arr.get(i);
ostringstream name;
name << cust->getFname() << " " << cust->getLname();
cout << "Customer ID " << cust->getId() << endl << endl
<< " Name: " << setw(40) << name.str() << endl
<< " Address: " << setw(37) << cust->getAddress() << endl
<< " Phone Number: " << setw(32) << cust->getPhoneNumber() << endl;
if (cust->getNumVehicles() > 0) {
cout << endl << " " << cust->getNumVehicles()
<< " vehicle(s): " << endl << endl;
}
VehicleArray& varr = cust->getVehicles();
for (int j = 0; j < varr.getSize(); j++) {
Vehicle* v = varr.get(j);
ostringstream make_model;
make_model << v->getMake() << " " << v->getModel();
cout << " " << j+1 << ") " << setw(7) << v->getColour() << " "
<< v->getYear() << " " << setw(17) << make_model.str() << " ("
<< v->getMilage() << "km)" << endl;
}
cout << endl << endl;
}
}
void View::pause() {
string str;
cout << "Press enter to continue...";
getline(cin, str);
}
int View::readInt() {
string str;
int num;
getline(cin, str);
stringstream ss(str);
ss >> num;
return num;
}
VehichleArray.h
#ifndef VEHICLEARRAY_H
#define VEHICLEARRAY_H
#include "defs.h"
#include "Vehicle.h"
class VehicleArray
{
public:
VehicleArray();
~VehicleArray();
int add(Vehicle*);
Vehicle* get(int);
int getSize();
private:
Vehicle* elements[MAX_VEHICLES];
int size;
};
#endif
vehichleArray.cc
#include "VehicleArray.h"
#include "Vehicle.h"
#include "defs.h"
VehicleArray::VehicleArray() { size = 0; }
VehicleArray::~VehicleArray() {
for(int i = 0; i < size; i++) {
delete elements[i];
}
}
int VehicleArray::getSize() { return size; }
int VehicleArray::add(Vehicle* v) {
if (size == MAX_VEHICLES) {
return C_NOK;
}
elements[size] = v;
size++;
return C_OK;
}
Vehicle* VehicleArray::get(int i) {
if ((i >= size) || (i < 0)) {
return 0;
}
return elements[i];
}
Vehichle.h
#ifndef VEHICLE_H
#define VEHICLE_H
#include <string>
using namespace std;
class Vehicle {
public:
Vehicle(string, string, string, int, int);
string getMake();
string getModel();
string getColour();
int getYear();
int getMilage();
private:
string make;
string model;
string colour;
int year;
int mileage;
};
#endif
vehicle.cc
#include "Vehicle.h"
Vehicle::Vehicle(string ma, string mo, string col, int y, int m) {
make = ma;
model = mo;
colour = col;
year = y;
mileage = m;
}
string Vehicle::getMake() { return make; }
string Vehicle::getModel() { return model; }
string Vehicle::getColour() { return colour; }
int Vehicle::getYear() { return year; }
int Vehicle::getMilage() { return mileage; }
shopController.h
#ifndef SHOPCONTROLLER_H
#define SHOPCONTROLLER_H
#include "View.h"
#include "Shop.h"
class ShopController {
public:
ShopController();
void launch();
private:
Shop mechanicShop;
View view;
void initCustomers();
};
#endif
shopController.cc
#include "ShopController.h"
ShopController::ShopController() {
initCustomers();
}
void ShopController::launch() {
int choice;
while (1) {
choice = -1;
view.mainMenu(choice);
if (choice == 1) {
view.printCustomers(mechanicShop.getCustomers());
view.pause();
} /*else if (choice == 2) {
} else if (choice == 3) {
} else if (choice == 4) {
} ... */
else {
break;
}
}
}
void ShopController::initCustomers() {
Customer* newCustomer;
Vehicle* newVehicle;
newCustomer = new Customer("Maurice", "Mooney", "2600 Colonel By Dr.",
"(613)728-9568");
newVehicle = new Vehicle("Ford", "Fiesta", "Red", 2007, 100000);
newCustomer->addVehicle(newVehicle);
mechanicShop.addCustomer(newCustomer);
newCustomer = new Customer("Abigail", "Atwood", "43 Carling Dr.",
"(613)345-6743");
newVehicle = new Vehicle("Subaru", "Forester", "Green", 2016, 40000);
newCustomer->addVehicle(newVehicle);
mechanicShop.addCustomer(newCustomer);
newCustomer = new Customer("Brook", "Banding", "1 Bayshore Dr.",
"(613)123-7456");
newVehicle = new Vehicle("Honda", "Accord", "White", 2018, 5000);
newCustomer->addVehicle(newVehicle);
newVehicle = new Vehicle("Volkswagon", "Beetle", "White", 1972, 5000);
newCustomer->addVehicle(newVehicle);
mechanicShop.addCustomer(newCustomer);
newCustomer = new Customer("Ethan", "Esser", "245 Rideau St.",
"(613)234-9677");
newVehicle = new Vehicle("Toyota", "Camery", "Black", 2010, 50000);
newCustomer->addVehicle(newVehicle);
mechanicShop.addCustomer(newCustomer);
newCustomer = new Customer("Eve", "Engram", "75 Bronson Ave.",
"(613)456-2345");
newVehicle = new Vehicle("Toyota", "Corolla", "Green", 2013, 80000);
newCustomer->addVehicle(newVehicle);
newVehicle = new Vehicle("Toyota", "Rav4", "Gold", 2015, 20000);
newCustomer->addVehicle(newVehicle);
newVehicle = new Vehicle("Toyota", "Prius", "Blue", 2017, 10000);
newCustomer->addVehicle(newVehicle);
mechanicShop.addCustomer(newCustomer);
newCustomer = new Customer("Victor", "Vanvalkenburg", "425 O'Connor St.",
"(613)432-7622");
newVehicle = new Vehicle("GM", "Envoy", "Purple", 2012, 60000);
newCustomer->addVehicle(newVehicle);
newVehicle = new Vehicle("GM", "Escalade", "Black", 2016, 40000);
newCustomer->addVehicle(newVehicle);
newVehicle = new Vehicle("GM", "Malibu", "Red", 2015, 20000);
newCustomer->addVehicle(newVehicle);
newVehicle = new Vehicle("GM", "Trailblazer", "Orange", 2012, 90000);
newCustomer->addVehicle(newVehicle);
//newVehicle = new Vehicle("GM", "Vue", "Blue", 2015, 20000);
//newCustomer->addVehicle(newVehicle);
mechanicShop.addCustomer(newCustomer);
}
shop.h
#ifndef SHOP_H
#define SHOP_H
#include "Customer.h"
#include "CustomerArray.h"
class Shop{
public:
int addCustomer(Customer*);
Customer* getCustomer(int);
CustomerArray& getCustomers();
private:
CustomerArray customers;
};
#endif
shop.cc
#include "Shop.h"
#include "defs.h"
int Shop::addCustomer(Customer* c) { return customers.add(c); }
Customer* Shop::getCustomer(int i) { return (customers.get(i)); }
CustomerArray& Shop::getCustomers() { return customers; }
main.cc
#include "ShopController.h"
int main(int argc, char* argv[])
{
ShopController control;
control.launch();
return 0;
}
defs.h
#ifndef DEFS_H
#define DEFS_H
#define MAX_VEHICLES 4
#define MAX_CUSTOMERS 6
#define C_OK 0
#define C_NOK -1
#endif
costumerArray.h
#ifndef CUSTOMERARRAY_H
#define CUSTOMERARRAY_H
#include "Customer.h"
class CustomerArray
{
public:
CustomerArray();
~CustomerArray();
int add(Customer*);
Customer* get(int);
int getSize();
private:
Customer* elements[MAX_CUSTOMERS];
int size;
};
#endif
customerArray.cc
#include "CustomerArray.h"
#include "Customer.h"
#include "defs.h"
CustomerArray::CustomerArray() { size = 0; }
CustomerArray::~CustomerArray() {
for(int i = 0; i < size; i++) {
delete elements[i];
}
}
int CustomerArray::getSize() { return size; }
int CustomerArray::add(Customer* c) {
if (size == MAX_CUSTOMERS) {
return C_NOK;
}
elements[size] = c;
size++;
return C_OK;
}
Customer* CustomerArray::get(int i) {
if ((i >= size) || (i < 0)) {
return 0;
}
return elements[i];
}
customer.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <string>
#include "Vehicle.h"
#include "VehicleArray.h"
using namespace std;
class Customer {
public:
Customer(string="", string="", string="", string="");
int getId();
string getFname();
string getLname();
string getAddress();
string getPhoneNumber();
int getNumVehicles();
VehicleArray& getVehicles();
int addVehicle(Vehicle*);
private:
static int nextId;
int id;
string firstName;
string lastName;
string address;
string phoneNumber;
VehicleArray vehicles;
};
#endif
customer.cc
#include <iostream>
using namespace std;
#include "Customer.h"
int Customer::nextId = 1000;
Customer::Customer(string fname, string lname, string add, string pnum) {
id = nextId++;
firstName = fname;
lastName = lname;
address = add;
phoneNumber = pnum;
}
int Customer::getId() { return id; }
string Customer::getFname() { return firstName; }
string Customer::getLname() { return lastName; }
string Customer::getAddress() { return address; }
string Customer::getPhoneNumber() { return phoneNumber; }
int Customer::getNumVehicles() { return vehicles.getSize(); }
VehicleArray& Customer::getVehicles() { return vehicles; }
int Customer::addVehicle(Vehicle* v) { return vehicles.add(v); }
MAKE FILE
OBJ = main.o ShopController.o View.o Shop.o CustomerArray.o VehicleArray.o Customer.o Vehicle.o
mechanicshop: $(OBJ)
g++ -o mechanicshop $(OBJ)
main.o: main.cc
g++ -c main.cc
ShopController.o: ShopController.cc ShopController.h Shop.h View.h
g++ -c ShopController.cc
View.o: View.cc View.h
g++ -c View.cc
Shop.o: Shop.cc Shop.h CustomerArray.h
g++ -c Shop.cc
CustomerArray.o: CustomerArray.cc CustomerArray.h Customer.h defs.h
g++ -c CustomerArray.cc
VehicleArray.o: VehicleArray.cc VehicleArray.h Vehicle.h defs.h
g++ -c VehicleArray.cc
Customer.o: Customer.cc Customer.h
g++ -c Customer.cc
Vehicle.o: Vehicle.cc Vehicle.h
g++ -c Vehicle.cc
clean:
rm -f $(OBJ) mechanicshop
Explanation / Answer
Customer.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <string>
#include "Vehicle.h"
#include "VehicleArray.h"
using namespace std;
class Customer
{
public:
Customer(string = "", string = "", string = "", string = "");
int getId();
string getFname();
string getLname();
string getAddress();
string getPhoneNumber();
int getNumVehicles();
VehicleArray &getVehicles();
int addVehicle(Vehicle *);
private:
static int nextId;
int id;
string firstName;
string lastName;
string address;
string phoneNumber;
VehicleArray vehicles;
};
#endif
CustomerArray.h
#ifndef CUSTOMERARRAY_H
#define CUSTOMERARRAY_H
#include "./Customer.h"
class CustomerArray
{
public:
CustomerArray();
~CustomerArray();
int add(Customer *);
Customer *get(int);
int getSize();
private:
Customer *elements[MAX_CUSTOMERS];
int size;
};
#endif
defs.h
#ifndef DEFS_H
#define DEFS_H
#define MAX_VEHICLES 4
#define MAX_CUSTOMERS 6
#define C_OK 0
#define C_NOK -1
#endif
Shop.h
#ifndef SHOP_H
#define SHOP_H
#include "Customer.h"
#include "CustomerArray.h"
class Shop
{
public:
int addCustomer(Customer *);
Customer &getCustomer(int);
CustomerArray &getCustomers();
private:
CustomerArray customers;
};
#endif
ShopController.h
#ifndef SHOPCONTROLLER_H
#define SHOPCONTROLLER_H
#include "View.h"
#include "Shop.h"
class ShopController
{
public:
ShopController();
void launch();
private:
Shop mechanicShop;
View view;
void initCustomers();
};
#endif
Vehicle.h
#ifndef VEHICLE_H
#define VEHICLE_H
#include <string>
using namespace std;
class Vehicle
{
public:
Vehicle(string, string, string, int, int);
string getMake();
string getModel();
string getColour();
int getYear();
int getMilage();
private:
string make;
string model;
string colour;
int year;
int mileage;
};
#endif
VehicleArray.h
#ifndef VEHICLEARRAY_H
#define VEHICLEARRAY_H
#include "defs.h"
#include "Vehicle.h"
class VehicleArray
{
public:
VehicleArray();
~VehicleArray();
int add(Vehicle *);
Vehicle *get(int);
int getSize();
private:
Vehicle *elements[MAX_VEHICLES];
int size;
};
#endif
View.h
#ifndef VIEW_H
#define VIEW_H
#include "CustomerArray.h"
class View
{
public:
void mainMenu(int &);
void printCustomers(CustomerArray &);
void pause();
private:
int readInt();
};
#endif
demo.cpp
// VIEW.CC
#include <sstream>
#include <iomanip>
#include <iostream>
using namespace std;
#include "./View.h"
#include "./CustomerArray.h"
#include "./Customer.h"
#include "VehicleArray.h"
#include "./Vehicle.h"
void View::mainMenu(int &choice)
{
string str;
choice = -1;
cout << " **** Toby's Auto Mechanic Information Management System **** ";
cout << " MAIN MENU ";
cout << " 1. Print Customer Database ";
cout << " 0. Exit ";
while (choice < 0 || choice > 1)
{
cout << "Enter your selection: ";
choice = readInt();
}
if (choice == 0)
{
cout << endl;
}
}
void View::printCustomers(CustomerArray &arr)
{
cout << endl
<< "CUSTOMERS: " << endl
<< endl;
for (int i = 0; i < arr.getSize(); i++)
{
Customer *cust = arr.get(i);
ostringstream name;
name << cust->getFname() << " " << cust->getLname();
cout << "Customer ID " << cust->getId() << endl
<< endl
<< " Name: " << setw(40) << name.str() << endl
<< " Address: " << setw(37) << cust->getAddress() << endl
<< " Phone Number: " << setw(32) << cust->getPhoneNumber() << endl;
if (cust->getNumVehicles() > 0)
{
cout << endl
<< " " << cust->getNumVehicles()
<< " vehicle(s): " << endl
<< endl;
}
VehicleArray &varr = cust->getVehicles();
for (int j = 0; j < varr.getSize(); j++)
{
Vehicle *v = varr.get(j);
ostringstream make_model;
make_model << v->getMake() << " " << v->getModel();
cout << " " << j + 1 << ") " << setw(7) << v->getColour() << " "
<< v->getYear() << " " << setw(17) << make_model.str() << " ("
<< v->getMilage() << "km)" << endl;
}
cout << endl
<< endl;
}
}
void View::pause()
{
string str;
cout << "Press enter to continue...";
getline(cin, str);
}
int View::readInt()
{
string str;
int num;
getline(cin, str);
stringstream ss(str);
ss >> num;
return num;
}
// VEHICLEARRAY.CC
#include "VehicleArray.h"
#include "Vehicle.h"
#include "defs.h"
int VehicleArray::getSize(){
return size;
}
Vehicle *VehicleArray::get(int i){
return elements[i];
}
// VEHICLE.CC
#include "Vehicle.h"
string Vehicle::getModel(){
return model;
}
string Vehicle::getMake(){
return make;
}
string Vehicle::getColour(){
return colour;
}
int Vehicle::getMilage(){
return mileage;
}
int Vehicle::getYear(){
return year;
}
VehicleArray::VehicleArray(){
size=0;
}
VehicleArray::~VehicleArray(){
int i;
for(i=0; i<size; i++){
elements[i] = NULL;
}
size = 0;
}
// SHOPCONTROLLER.CC
#include "ShopController.h"
ShopController::ShopController()
{
initCustomers();
}
void ShopController::launch()
{
int choice;
while (1)
{
choice = -1;
view.mainMenu(choice);
if (choice == 1)
{
view.printCustomers(mechanicShop.getCustomers());
view.pause();
} /*else if (choice == 2) {
} else if (choice == 3) {
} else if (choice == 4) {
} ... */
else
{
break;
}
}
}
void ShopController::initCustomers()
{
//add data fill here
}
// SHOP.CC
#include "Shop.h"
#include "defs.h"
int Shop::addCustomer(Customer *c)
{
return customers.add(c);
}
Customer &Shop::getCustomer(int i) { return *(customers.get(i)); }
CustomerArray &Shop::getCustomers() { return customers; }
#include "ShopController.h"
int main(int argc, char *argv[])
{
ShopController control;
control.launch();
return 0;
}
// CUSTOMERARRAY.CC
#include "CustomerArray.h"
#include "Customer.h"
#include "defs.h"
int CustomerArray::getSize(){
return size;
}
Customer *CustomerArray::get(int ind){
if(ind>size)
return 0;
return elements[ind];
}
int CustomerArray::add(Customer *n){
if(size==MAX_CUSTOMERS){
cout<<"Customer max limit exceeded"<<endl;
return C_NOK;
}
elements[size++] = n;
return C_OK;
}
CustomerArray::CustomerArray(){
size=0;
}
CustomerArray::~CustomerArray(){
int i;
for(i=0; i<size; i++){
elements[i] = NULL;
}
size = 0;
}
// CUSTOMER.CC
#include <iostream>
using namespace std;
#include "Customer.h"
int Customer::nextId;
Customer::Customer(string firstName, string lastName, string address, string phoneNumber){
Customer::nextId = 1000;
firstName = firstName;
lastName = lastName;
address = address;
phoneNumber = phoneNumber;
}
string Customer::getLname(){
return lastName;
}
string Customer::getFname(){
return firstName;
}
string Customer::getPhoneNumber(){
return phoneNumber;
}
string Customer::getAddress(){
return address;
}
int Customer::getId(){
return id;
}
int Customer::getNumVehicles(){
CustomerArray *custArr;
return custArr->getSize();
}
VehicleArray &Customer::getVehicles(){
return vehicles;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.