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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
// TUAN"S FISH STORE
// This programn will keep track of all your wonderful fishies :))
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// fish strcture for each fish
struct Fish
{
string name;
int tank;
int quantity;
double price;
};
// Prototypes for all functions
void displayMenu();
void displayInventory( Fish, int, int );
void readInventory( Fish, int, int );
void writeInventory( Fish, int, int );
void addInventory( Fish, int, int );
void removeInventory( Fish, int, int );
void searchInventory( Fish, int, int );
void updateInventory( Fish, int, int );
void updateTank( Fish, int, int );
int main( )
{
int choice; //users choice for the menu
const int MAX_SIZE = 100; // Size of array
Fish myInventory; //Fish tag initiated to myInventory
int currentSize = 0;
displayMenu();
cout << "\nPlease select an option: ";
cin >> choice;
switch (choice)
{
case 1: displayInventory(myInventory, MAX_SIZE, currentSize);
break;
case 2: readInventory(myInventory, MAX_SIZE, currentSize);
break;
case 3: writeInventory(myInventory, MAX_SIZE, currentSize);
break;
case 4: addInventory(myInventory, MAX_SIZE, currentSize);
break;
case 5: displayInventory(myInventory, MAX_SIZE, currentSize); // display then remove
removeInventory(myInventory, MAX_SIZE, currentSize);
break;
case 6: searchInventory(myInventory, MAX_SIZE, currentSize);
break;
case 7: updateInventory(myInventory, MAX_SIZE, currentSize);
break;
case 8: updateTank(myInventory, MAX_SIZE, currentSize);
break;
case 9: exit(0);
break;
}
return 0;
}
void displayMenu()
{
cout << "Tuan's Fishies\n";
cout << "***************\n";
cout << "Please chose one of the options below\n";
cout << "1. Display the Inventory\n";
cout << "2. Read Fish Inventory from a File\n";
cout << "3. Write Fish Inventory to a File\n";
cout << "4. Add Fish to Inventory\n";
cout << "5. Remove Fish from Inventory\n";
cout << "6. Search for Fish\n";
cout << "7. Update Quantity\n";
cout << "8. Update Tank/Location\n";
cout << "9. Exit\n";
}
void displayInventory( Fish myArray[], int max, int currtSize)
{
cout << "This is the Inventory\n";
for (int index = 0; index <= max; index++)
{
cout << "fish index number" << (index+1) << endl;
cout << myArray[index].name << endl;
cout << myArray[index].tank << endl;
cout << myArray[index].quantity << endl;
cout << myArray[index].price << endl << endl;
}
}
void readInventory( Fish myArray[], int max, int currtSize = 0)
{
ifstream inputFile;
string filename;
cout << "Please enter the file name\n";
getline(cin,filename);
while ((myArray[currtSize].price) > 0) // find the next empty subscript
{
currtSize++;
}
inputFile.open(filename);
if (inputFile)
{
cout << "Reading data from the file\n";
while (currtSize < max)
{
inputFile >> myArray[currtSize].name;
inputFile >> myArray[currtSize].tank;
inputFile >> myArray[currtSize].quantity;
inputFile >> myArray[currtSize].price;
currtSize++;
}
}
inputFile.close();
}
void writeInventory( Fish myArray[], int max, int currtSize)
{
ofstream outputFile;
int count;
string filename;
cout << "Please enter the desired file name\n";
cin >> filename;
filename = filename + ".dat";
outputFile.open(filename);
cout << "Writing data to the file\n";
for (count = 0; count < max; count++)
{
outputFile << myArray[count].name << endl;
outputFile << myArray[count].tank << endl;
outputFile << myArray[count].quantity << endl;
outputFile << myArray[count].price << endl;
}
outputFile.close();
cout << "Finished Writing data to the file\n";
}
void addInventory( Fish myArray[], int max, int currtSize = 0)
{ // need to sort the arrays by fish name
while (myArray[currtSize].quantity > 0){
currtSize++;
}
cout << "Enter the fish name\n";
cin >> myArray[currtSize].name;
cout << "Enter the fish tank number\n";
cin >> myArray[currtSize].tank;
cout << "Enter the fish quantity\n";
cin >> myArray[currtSize].quantity;
cout << "Enter the fish price\n";
cin >> myArray[currtSize].price;
}
void removeInventory( Fish myArray[], int max, int currtSize)
{
int index;
cout << "Please enter the fish index number you wish to remove from the inventory\n";
cin >> index;
myArray[index].name.clear(); //might work if i just have myArray[index].clear()
myArray[index].tank = 0;
myArray[index].quantity = 0;
myArray[index].price = 0;
cout << "Fish index " << index << " has been removed\n";
}
void searchInventory( Fish myArray[], int max, int currtSize)
{
string name;
int index = 0;
bool foundName = false;
cout << "Please enter the name of the fish you are looking for\n";
getline(cin,name);
while (foundName = false && index < max)
{
if(name == myArray[index].name)
{
foundName = true;
}
index++;
}
if (foundName = true)
{
cout << "The fish has been found\n";
cout << myArray[index - 1].name;
cout << myArray[index - 1].tank;
cout << myArray[index - 1].quantity;
cout << myArray[index - 1].price;
}
else
cout << "No fish with the same name has been found\n";
}
void updateInventory( Fish myArray[], int max, int currtSize)
{
int index;
int choice;
displayInventory(myArray, max, currtSize);
cout << "Please enter the fish index number you wish to take a look at\n";
cin >> index;
cout << myArray[index-1].name;
cout << myArray[index-1].tank;
cout << myArray[index-1].quantity;
cout << myArray[index-1].price;
cout << "How would you like to change the quantity\n";
cout << "1. New total quantity\n";
cout << "2. Decrease by one\n";
cout << "3. Increase by one\n";
cin >> choice;
switch (choice)
{
case 1:
cout << "Please enter the new total quantity\n";
cin >> myArray[index].quantity;
while (myArray[index].quantity < 0)
{
cout << "please enter a positive integer\n";
cin >> myArray[index].quantity;
}
cout << "Your new fish quantity is " << myArray[index].quantity << endl;
break;
case 2:
if (myArray[index].quantity = 0)
cout << "You currently have 0 fish of this breed";
else
{
myArray[index].quantity = myArray[index].quantity - 1;
cout << "Your new fish quantity is " << myArray[index].quantity << endl;
}
break;
case 3:
myArray[index].quantity = myArray[index].quantity = 1;
cout << "Your new fish quantity is " << myArray[index].quantity << endl;
break;
}
}
void updateTank( Fish myArray[], int max, int currtSize)
{
int index;
displayInventory(myArray, max, currtSize);
cout << "Please enter the fish index number you wish to take a look at\n";
cin >> index;
cout << myArray[index-1].name;
cout << myArray[index-1].tank;
cout << myArray[index-1].quantity;
cout << myArray[index-1].price;
cout << "Please enter the new tank number for the fish";
cin >> myArray[index-1].tank;
cout << myArray[index-1].name << " has been transfered to tank " << myArray[index-1].tank << endl;
}
|