1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iomanip>
#include "Car.h"
#include "Car.cpp"
using namespace std;
void display_inventory(vector<Car> all_cars)
{
cout << "Inventory:" << endl;
for (int i = 0; i < all_cars.size(); i++)
{
cout << all_cars[i].toString();
}
}
int find_car(vector<Car> all_cars, string your_choice)
{
for (int index = 0; index < all_cars.size(); index++)
{
if (your_choice == all_cars[index].getName())
{
return index;
}
}
return -1;
}
void display_balance(vector<Car> all_cars, double &balance)
{
/*for (int i = 0; i < all_cars.size(); i++)
{
balance -= all_cars[i].getPrice();
}*/
cout << "Balance: " << fixed << setprecision(2) << balance << endl;
}
void buy_car(vector<Car> &all_cars, double &balance)
{
string name;
string color;
double price;
cout << "Please enter in the following information of new car: Name, Color, Price: " << endl;
cin >> name;
cin >> color;
cin >> price;
Car new_car(name, color, price);
string your_choice = name;
int found = find_car(all_cars, your_choice);
if (balance - price < 0 || found != -1)
{
cout << "Car NOT added to the vector" << endl;
}
/*if (found != -1)
{
cout << "Sorry, the car was already in the inventory" << endl;
}*/
else
{
all_cars.push_back(new_car);
balance -= new_car.getPrice();
}
}
void sell_car(vector<Car> &all_cars, double& balance)
{
string sell_car_name;
cout << "Please enter the car to be sold: " << endl;
cin >> sell_car_name;
string your_choice = sell_car_name;
int found = find_car(all_cars, your_choice);
if (found != -1)
{
Car sell_car = all_cars[found];
balance += sell_car.getPrice();
all_cars.erase(all_cars.begin() + found);
}
if (found == -1)
{
cout << "Sorry, that car is not in our inventory" << endl;
}
}
void paint_car(vector<Car> &all_cars, double &balance)
{
string paint_car_name;
cout << "Which car do you want to paint? " << endl;
cin >> paint_car_name;
string your_choice = paint_car_name;
int found = find_car(all_cars, your_choice);
Car paint_car = all_cars[found];
string new_color;
cout << "What color?" << endl;
cin >> new_color;
paint_car.paint(new_color);
all_cars[found] = paint_car;
}
void load_file(vector<Car> &all_cars, double &balance)
{
//load info
string file_name;
double file_balance;
string name;
string color;
double price;
ifstream input_file;
cout << "file name? " << endl;
cin >> file_name;
input_file.open(file_name.c_str());
input_file >> file_balance;
while (input_file >> name)
{
input_file >> color;
input_file >> price;
Car new_car(name, color, price);
all_cars.push_back(new_car);
}
balance += file_balance;
}
void save_file(vector<Car> all_cars, double balance)
{
string name;
string color;
double price;
string file_name;
ofstream save_file;
cout << "File name?" << endl;
cin >> file_name;
save_file.open(file_name);
save_file << fixed << setprecision(2) << balance << "\n";
for (int i = 0; i < all_cars.size(); i++)
{
name = all_cars[i].getName();
color = all_cars[i].getColor();
price = all_cars[i].getPrice();
save_file << name << " ";
save_file << color << " ";
save_file << price << "\n";
}
save_file.close();
}
int main()
{
double balance = 10000;
vector<Car> all_cars;
/*Car my_car("Jalopy", "Blue", 3402.99);
Car my_car2("Rustbucket", "Brown", 44.99);
Car my_car3("Lemon", "Yellow", 4226.99);
all_cars.push_back(my_car);
all_cars.push_back(my_car2);
all_cars.push_back(my_car3);*/
int menu = 0;
while (menu == 0)
{
cout << "\n*************MENU***********" << endl;
cout << "1. Show Current Inventory\n2. Show Current Balance\n3. Buy a Car\n4. Sell a Car\n5. Paint a Car\n6. Load a File\n7. Save File\n8. Quit Program" << endl;
cout << "****************************" << endl << endl;
//my_car.fight(all_cars[0], all_cars[1]);
int input;
cin >> input;
if (input = 1)
{
display_inventory(all_cars);
}
else if (input = 2)
{
display_balance(all_cars, balance);
}
else if (input = 3)
{
buy_car(all_cars, balance);
}
else if (input = 4)
{
sell_car(all_cars, balance);
}
else if (input = 5)
{
paint_car(all_cars, balance);
}
else if (input = 6)
{
load_file(all_cars, balance);
}
else if (input = 7)
{
save_file(all_cars, balance);
}
else if (input = 8)
{
menu = 0;
return 0;
}
}
system("pause");
return 0;
}
| |