In: Computer Science
CS 400 Assignment 2: applying ArrayList In this assignment, you are going to build a program that can help rental car companies to manage their rental fleet. Requirement: - build ArrayList class as container. - build Car class/structure to represent car objects. - An ArrayList object is to hold Car objects. Car class should have following fields: - id (int) - make (string) - model (string) - color (string) Instructions: - make up 15 cars and save them into a file: cars.data - load these cars' info from the file, initialize 15 Car objects, place them into the Arraylist - if new cars are added by user, they should be appended to the cars.data file when exiting the program - design a menu based user interface that allows following operations: -- search by id -- search by make -- search by model -- search by color -- add a new car (new car id cannot be same as existing ones) -- delete an existing car by id -- list all cars -- exit the program Grading: - compilable and meaningful attemps: 30 points. - functionality: 60 points (ArrayList 10p, Car 10p, file io 10p, menu functions 30p) - comments and indentation: 10 points. Submission: - submit related .h files, .cpp files and cars.data via blackboard. Do not forget to submit cars.data!
//Please edit the car.txt data
//================ ArrayList.h ====================
#pragma once //ArrayList is only created one time in
memory
#include<iostream>
using namespace std;
//create template
template<class T> //to tell c++ we are creating templated
class
class ArrayList
{
//attributes
private:
const static int DEFAULT_SIZE = 5; //constant for the
initial size
T* list; //pointer to the array
int count; //Number of items in list
int capacity; //current size in memory
public:
//constructor
ArrayList();
//second constructor
ArrayList(int initialCapacity);
//destructor
~ArrayList(void);
//determine if the ArrayList is empty
bool isEmpty();
// Get the item at the given position
T get(int position);
/**
Add an item to the array list
*/
void add(T data);
//Remove item a the given position
void removeAt(int position);
//Get the count of items in the Arraylist
int getCount(void);
//Get the ArrayList current capacity
int getCapacity();
};
//====================== ArrayList.cpp
#include"ArrayList.h"
//constructor
template<class T>
ArrayList<T>::ArrayList()
{
this->list = new T[DEFAULT_SIZE];
this->capacity = DEFAULT_SIZE;
this->count = 0; //since there is no item in
list
}
//second constructor
template<class T>
ArrayList<T>::ArrayList(int initialCapacity)
{
this->list = new T(initialCapacity);
this->capacity = initialCapacity;
this->count = 0;
}
//destructor
template<class T>
ArrayList<T>::~ArrayList(void)
{
if (this->list != nullptr)
{
delete[] this->list;
this->list = nullptr;
}
}
//determine if the ArrayList is empty
template<class T>
bool ArrayList<T>::isEmpty()
{
return count == 0; //array is empty if it has zero
items
}
// Get the item at the given position
template<class T>
T ArrayList<T>::get(int position)
{
if (position < count)
{
return list[position];
}
else
{
throw("Not found");
}
}
/**
Add an item to the array list
*/
template<class T>
void ArrayList<T>::add(T data)
{
//if the array is full, double the size
if (count == capacity)
{
capacity = 2 * capacity;
T* temp = new T[capacity];
//copy items from current array
to bigger array
for (int i = 0; i < count;
i++)
{
temp[i] =
list[i];
}
//delete the current array
delete[] list;
// rename the bigger array to the
current array name
list = temp;
}
//add the data item to the array
list[count] = data;
//incement the count
count++;
}
//Remove item a the given position
template<class T>
void ArrayList<T>::removeAt(int position)
{
//replace every item from that position on with
//the next item
for (int i = position; i < count - 1; i++)
{
list[i] = list[i + 1];
}
//decrease the item count
count--;
}
//Get the count of items in the Arraylist
template<class T>
int ArrayList<T>::getCount(void)
{
return count;
}
//Get the ArrayList current capacity
template<class T>
int ArrayList<T>::getCapacity()
{
return capacity;
}
//====================Car.h ==============
#pragma once
#include<iostream>
#include<string>
using namespace std;
/**
Car class should have following fields:
- id (int)
- make (string)
- model (string)
- color (string)
*/
class Car
{
private:
int id;
string make;
string model;
string color;
public:
//default Constructor
Car();
//Overload Constructor
Car(int id, string make, string model, string
color);
//getters
int getID();
string getModel();
string getMake();
string getColor();
//setters
void setID(int id);
void setModel(string model);
void setMake(string make);
void setColor(string color);
//overload operator for input and outpur
friend ostream& operator << (ostream&
out, const Car& c);
friend istream& operator >> (istream&
in, Car& c);
};
//================= Car.cpp
#include"Car.h"
//default Constructor
Car::Car()
{
id = 0;
make = "";
model = "";
color = "";
}
//Overload Constructor
Car::Car(int id, string make, string model, string color)
{
this->id = id;
this->make = make;
this->model = model;
this->color = color;
}
//getters
int Car::getID()
{
return id;
}
string Car::getModel()
{
return model;
}
string Car::getMake()
{
return make;
}
string Car::getColor()
{
return color;
}
//setters
void Car::setID(int id)
{
this->id = id;
}
void Car::setModel(string model)
{
this->model = model;
}
void Car::setMake(string make)
{
this->make = make;
}
void Car::setColor(string color)
{
this->color = color;
}
//overload operator for input and outpur
ostream& operator << (ostream& out, const Car&
c)
{
out << c.id << " " << c.make
<< " " << c.model << " " << c.color
<< endl;
return out;
}
istream& operator >> (istream& in, Car& c)
{
cout << "Enter Id: ";
in >> c.id;
cout << "Enter Make: ";
in >> c.make;
cout << "Enter Model: ";
in >> c.model;
cout << "Enter color: ";
in >> c.color;
return in;
}
//========================== main.cpp=======================
#include"ArrayList.h"
#include"Car.h"
#include"ArrayList.cpp"
#include<fstream>
//===================function prototype
void menu();
int main()
{
ifstream infile("cars.txt");
if (!infile)// || !outfile)
{
cout << "ERROR!!! .... File
not found....Existing" << endl;
//pause
system("pause");
return -1;
}
//Create Array List of Car
ArrayList<Car>* cars = new
ArrayList<Car>();
//add upto 15 cars
int id;
string make, model, color;
while (infile >> id >> make >> model
>> color)
{
Car car(id, make, model,
color);
cars->add(car);
}
int menuChoice;
do
{
menu();
cin >> menuChoice;
bool found = false;
switch (menuChoice)
{
case 1:
cout <<
"Enter Id: ";
cin >>
id;
for (int i = 0;
i < cars->getCount(); i++)
{
if (id == cars->get(i).getID())
{
cout << cars->get(i)
<< endl;
found = true;
}
}
if
(!found)
{
cout << "Not found..." <<
endl;
}
break;
case 2:
cout <<
"Enter make: ";
cin >>
make;
for (int i =
0; i < cars->getCount(); i++)
{
if (make == cars->get(i).getMake())
{
cout << cars->get(i)
<< endl;
found = true;
}
}
if
(!found)
{
cout << "Not found..." <<
endl;
}
break;
case 3:
cout <<
"Enter model: ";
cin >>
model;
for (int i =
0; i < cars->getCount(); i++)
{
if (model == cars->get(i).getModel())
{
cout << cars->get(i)
<< endl;
found = true;
}
}
if
(!found)
{
cout << "Not found..." <<
endl;
}
break;
case 4:
cout <<
"Enter color: ";
cin >>
color;
for (int i =
0; i < cars->getCount(); i++)
{
if (color == cars->get(i).getColor())
{
cout << cars->get(i)
<< endl;
found = true;
}
}
if
(!found)
{
cout << "Not found..." <<
endl;
}
break;
case 5:
cout <<
"Enter Id: ";
cin >>
id;
for (int i = 0;
i < cars->getCount(); i++)
{
if (id == cars->get(i).getID())
found = true;
}
if (found)
{
cout << "Duplicate Car ID Found ... Try
again.." << endl;
break;
}
else
{
ofstream outfile("cars.txt", ios::app);
cout << "Enter car model: ";
cin >> model;
cout << "Enter car make: ";
cin >> make;
cout << "Enter car color: ";
cin >> color;
Car car(id, make, model, color);
//add to arraylist
cars->add(car);
//add to file
outfile << car;
outfile.close();
cout << "Car saved successfully...."
<< endl;
}
break;
case 6:
cout <<
"Enter Id: ";
cin >>
id;
for (int i = 0;
i < cars->getCount(); i++)
{
if (id == cars->get(i).getID())
{
cars->removeAt(i);
cout << "Delete
successfully..." << endl;
found = true;
}
}
if
(!found)
{
cout << "Car not found ...." <<
endl;
}
break;
case 7:
cout <<
"Cars Record: " << endl;
for (int i = 0;
i < cars->getCount(); i++)
{
cout << cars->get(i) <<
endl;
}
break;
case 8:
cout <<
"Thanks for using App...\n" << endl;
system("pause");
break;
default:
cout <<
"Wrong choice...Try again..";
break;
}
}while (menuChoice != 8);
}
//============== Function definitions
void menu()
{
cout << "(1) Search by id\n(2) Search by
make\n(3) Search by model\n(4) Search by color\n(5) Add new
Car\n(6) Delete an existing car\n(7) list all cars\n(8) Exit the
program.\nEnter your choice: ";
}
//============= output ======================
//If you need any help regarding this solution ......... please leave a comment ........ thanks