Question

In: Computer Science

CS 400 Assignment 2: applying ArrayList In this assignment, you are going to build a program...

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! 


Solutions

Expert Solution

//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


Related Solutions

For this assignment, you are going to build upon several skills that you've learned: Create an...
For this assignment, you are going to build upon several skills that you've learned: Create an object that contains objects. Using querySelectorAll to read a nodeList from the DOM. Looping through the nodeList then updating the HTML page. Set up Create the assignment in a "week6" folder with the typical files: index.html css/styles.css js/scripts.js This is the standard structure that we'll use in all assignments. Here is the HTML to use for this assignment. Change the meta author tag to...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going to implement a simple C version of the UNIX cat program called lolcat. The cat program allows you to display the contents of one or more text files. The lolcat program will only display one file. The correct usage of your program should be to execute it on the command line with a single command line argument consisting of the name you want to...
MBA 5010 Week 4 Integrative Assignment This week, you’re going to build on the knowledge you...
MBA 5010 Week 4 Integrative Assignment This week, you’re going to build on the knowledge you gained. Your assignment is to deconstruct the economics underlying Airbnb. Specifically, I want you to answer the following questions: • Does Airbnb capture a sufficient amount of any value generated to remain a viable business? The purpose of this question is to provide you an opportunity to demonstrate your understanding of the economics of value creation. So, emphasize this aspect of your answer. Try...
CS 1102 Unit 5 – Programming AssignmentIn this assignment, you will again modify your Quiz program...
CS 1102 Unit 5 – Programming AssignmentIn this assignment, you will again modify your Quiz program from the previous assignment. You will create an abstract class called "Question", modify "MultipleChoiceQuestion" to inherit from it, and add a new subclass of "Question" called "TrueFalseQuestion". This assignment will again involve cutting and pasting from existing classes. Because you are learning new features each week, you are retroactively applying those new features. In a typical programming project, you would start with the full...
problem 3-1 You are going to build a C++ program which runs a single game of...
problem 3-1 You are going to build a C++ program which runs a single game of Rock, Paper, Scissors. Two players (a human player and a computer player) will compete and individually choose Rock, Paper, or Scissors. They will then simultaneously declare their choices and the winner is determined by comparing the players’ choices. Rock beats Scissors. Scissors beats Paper. Paper beats Rock. The learning objectives of this task is to help develop your understanding of abstract classes, inheritance, and...
Project Assignment The purpose of this assignment is for you to gain practice in applying the...
Project Assignment The purpose of this assignment is for you to gain practice in applying the concepts and techniques we learned in class. In order to complete the assignment, please do the following: 1. find or create a data set containing values of at least one interval or ratio variable for at least one-hundred cases (n >= 100); 1 2. provide basic descriptive statistics to summarize the central tendency and variability of the data; 3. provide at least one table...
In this assignment, you are going to write a Python program to demonstrate the IPO (Input-Process-Output)...
In this assignment, you are going to write a Python program to demonstrate the IPO (Input-Process-Output) cycle that is the heart of many imperative programs used for processing large amount of data. Daisy is recently hired by a warehouse. One of her job is to keep track the items ordered by all the branches of the company. The company wants to automate the task using a computer program. Being a friend of Daisy, she knows you are a Computer Science...
CS 400 Assignment 4 Stack application: postfix expression evaluation. Description: - The stack data structure can...
CS 400 Assignment 4 Stack application: postfix expression evaluation. Description: - The stack data structure can be used to evaluate postfix expressions. Please refer to the first 14 pages of this tutorial for postfix expression evaluation: http://www.cs.nthu.edu.tw/~wkhon/ds/ds10/tutorial/tutorial2.pdf Requirement: - deal with single digit positive integers only. Operands and operators are fully separated by space. - learn and use STL stack: http://www.cplusplus.com/reference/stack/stack/ - learn and use isdigit(): http://www.cplusplus.com/reference/cctype/isdigit/ - take in a postfix arithmetic expression from cin, and evaluate its value....
For this 1-2 page assignment, you are going to watch the video of a speech and...
For this 1-2 page assignment, you are going to watch the video of a speech and write a peer review as if the speaker would get the feedback from you in written form. In the review please address the following aspects of the speech, giving clear examples from the speech and then critique the student’s performance. At least three aspects of delivery At least two aspects of content Was the speech effective? Student Speeches for Analysis
The assignment is to build a program in Python that can take a string as input...
The assignment is to build a program in Python that can take a string as input and produce a “frequency list” of all of the wordsin the string (see the definition of a word below.)  For the purposes of this assignment, the input strings can be assumed not to contain escape characters (\n, \t, …) and to be readable with a single input() statement. When your program ends, it prints the list of words.  In the output, each line contains of a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT