Need to create a C++ program with following criteria: price: positive whole numb
ID: 3753669 • Letter: N
Question
Need to create a C++ program with following criteria:
price: positive whole number, between 0-9999. Randomly generated.
color: a string, out of red, blue, white, black, gray, green, yellow. Randomly selected.
VIN: positive whole number, 5 chars, should have two A-to-Z chars. Randomly generated in your program.
year: between, 1950 and 2017. Randomly generated.
owner: One word string, having only a-z and A-Z. The first letter is capitalized, the rest are lowercase. Randomly generated.
Program should have a class contain following functions:
-Create a bag
- Add a new car (all data pieces will be randomly created)
- Search a car with VIN
- List VINs of the cars what are specified by a color. (for instance, VINs of all white cars)
- List VINs of the cars what are specified by owner name. (search by owner name)
- Remove a car with VIN
- Remove the last car (the one just added)
- Remove all cars (do not forget to free structs from memory)
- Get the number of cars in the bag
The program also automatically add 1000 cars to the bag once the program is run.
Explanation / Answer
The template provided has been modified to implement a linked list version with all the features and functions required. The code for that is :
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
struct CarNode {
Car carObject;
struct CarNode * nextCar;
};
class Car {
private:
string colorOptions[] = {
"red",
"blue",
"white",
"black",
"gray",
"green",
"yellow"
};
string upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string lowerCase = "abcdefghijklmnopqrstuvwxyz";
string numbers = "0123456789";
int price = 0;
string color;
string vin;
string owner;
int year;
void setColor() {
int option = rand() * 7;
this.color = colorOptions[option];
}
void setPrice() {
this.price = rand() * 10000;
}
void setOwner() {
string nameVal = upperCase[(rand() * 26)];
int nameSize = (rand() * 50) + 10;
for (int i = 0; i < nameSize; i++) {
int index = rand() * 26;
nameVal = nameVal + lowerCase[index];
}
this.name = nameVal;
}
void setVin() {
string vinVal = number[(rand() * 10)];
for (int i = 0; i < 5; i++) {
int index = rand() * 26;
vinVal = vinVal + upperCase[index]
}
}
void setYear() {
this.year = (rand() * 68) + 1950;
}
public:
Car() {
this.setYear();
this.setPrice();
this.setVin();
this.setOwner();
this.setColor();
}
string getOwner() {
return this.owner;
}
string getColor() {
return this.color;
}
string getVin() {
return this.vin;
}
int getYear() {
return this.year;
}
int getPrice() {
return this.price;
}
};
class BagDyn
{
public:
BagDyn(); // construction
bool Add(int n); // Add, true if successfully added
bool Remove(int n); // Removes an item from the bag
unsigned int getCapacity(); // return the capacity, the max number of items to be held
unsigned int getSize(); // current number of items in the bag
bool Search(int s); // search for an item
bool isEmtpy(); // Checks if the bag is empty
void ListAll(); // displays all items of the bag
private:
CarNode * head = NULL;
unsigned int size;
unsigned int capacity;
};
BagDyn::BagDyn() // construction
{
cout << "Enter the capacity of the bag:";
cin >> capacity;
size = 0;
// data = new Car[capacity];
// if (data == NULL)
// {
// cout << "An error occured, bag cannot be allocated" << endl;
// }
// else
// {
// cout << data;
// cout << "A bag to keep " << capacity << " items is created and ready to use" << endl;
}
}
// returns the capacity, the max number of items to be held
unsigned int BagDyn::getCapacity()
{
return capacity;
}
// number of items in the bag
unsigned int BagDyn::getSize()
{
return size;
}
// search for an item
bool BagDyn::searchByVin(string vin) {
CarNode * temp = this.head;
while (temp != NULL) {
if (temp.carObject.getVin() == vin) {
return true;
}
}
return false
}
void BagDyn::listVinByColor(string color) {
CarNode * temp = this.head;
while (temp != NULL) {
if (temp.carObject.getColor() == color) {
cout << temp.carObject.getVin() << endl;
}
}
}
void BagDyn::listVinByOwner(string owner) {
CarNode * temp = this.head;
while (temp != NULL) {
if (temp.carObject.getOwner() == owner) {
cout << temp.carObject.getVin() << endl;
}
}
}
// Checks if the bag is empty
bool BagDyn::isEmtpy()
{
if (size == 0) return true;
return false;
}
// Adds an items, true if successfully added
bool BagDyn::Add(CarNode node)
{
if (size == capacity) return false;
CarNode * temp = this.head;
while (temp != NULL) {
if (temp.carObject.getVin() == node.carObject.getVin()) {
return false;
}
temp = temp - > nextCar;
}
CarNode * newNode = new CarNode();
newNode.carObject = new Car();
newNode.nextCar = NULL;
size++;
}
// data[size] = n; size++;
// displays all items of the bag
void BagDyn::ListAll()
{
CarNode * temp = this.head;
if (size == 0) {
cout << "Bag is empty" << endl;
return;
}
while (temp != NULL) // i < size or i <= (size-1)
{
cout << "Owner Name: " << temp.carObject.getOwner() << endl;
cout << "Car Color: " << temp.carObject.getColor() << endl;
cout << "Vin: " << temp.carObject.getVin() << endl;
cout << "Price: " << temp.carObject.getPrice() << endl;
cout << "Year: " << temp.carObject.getYear() << endl;
cout << endl << endl;
temp = temp - > nextCar;
}
cout << endl;
}
// Remove car by by Vin
bool BagDyn::removeCarByVin(string vinVal) {
CarNode * temp = this.head;
while (temp != NULL) {
CarnNode * prev = temp;
if (temp.carObject.getVin() == vinVal) {
prev - > nextCar = temp - > nextCar;
temp.nextCar = NULL;
CarNode * junkNode = temp;
free(junkNode);
size--;
return true;
}
}
return false;
}
bool BagDyn::removeLastCar(string vinVal) {
CarNode * temp = this.head;
CarNode * prev = NULL;
while (temp) {
prev = temp;
temp = temp - > nextCar;
}
if (prev != NULL) {
prev - > nextCar = NULL;
free(temp);
size--;
return true;
}
return false;
}
void BagDyn::removeAllCars() {
CarNode * temp = this.head;
CarNode * next;
while (temp != NULL) {
next = current - > next;
current - > next = NULL;
free(current);
current = next;
}
head = null;
}
// Demonstrate the usage of the bag
int main()
{
BagDyn aBag = new BagDyn();
// Create your bag here.
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.