HELLO, PLEASE ANSWER THE QUESTION CORRECTLY AND SHOW THAT THE CODE RUNS ON THE S
ID: 3726330 • Letter: H
Question
HELLO, PLEASE ANSWER THE QUESTION CORRECTLY AND SHOW THAT THE CODE RUNS ON THE SCREEN. THANKS
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.
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; }
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
main.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.