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
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cctype>
using namespace std;
class Inventory
{
public:
Inventory()
{ code = 0; gName = " "; bName = " "; dForm = " "; dStr = " "; day = 0; month = 0; year = 0; qty = 0; price = 0.00; }
Inventory(int c, string gN, string bN, string dF, string dS, int d, int m, int y, int q, double p)
{ code = c; gName = gN; bName = bN; dForm = dF; dStr = dS; day = d; month = m; year = y; qty = q; price = p;}
int getCode()
{ return code;}
string getGName()
{ string gN = gName; return gN;}
string getBName()
{ string bN = bName; return bN;}
string getDForm()
{ string dF = dForm; return dF;}
string getDStr()
{ string dS = dStr; return dS;}
int getDay()
{return day;}
int getMonth()
{return month;}
int getYear()
{return year;}
int getQty()
{return qty;}
double getPrice()
{return price;}
void Add(Inventory[], int, string, string, string, string, int, int, int, int, double);
void Search();
void displayAll(Inventory[]);
private:
int code;
string gName, bName, dForm, dStr;
int day, month, year, qty;
double price;
};
int searching(Inventory[], int , int);
void Menu();
bool redo();
const int SIZE = 100;
int product[SIZE];
int recNum = 0;
fstream file;
bool again = true;
void Inventory::Add(Inventory product[], int code, string gName, string bName, string dForm, string dStr,int day, int month, int year, int qty, double price)
{
file.open("zzz.txt", ios::out | ios::app);
if(!file)
{
cout << "Error opening file."<< endl;
system("PAUSE");
exit(1);
}
cout << "ADD ME" << endl;
do
{
if(recNum < SIZE)
{
cout << "- Code: ";
cin >> product[recNum].code;
file << product[recNum].code << endl;
cin.ignore();
cout << "- Generic Name: ";
getline(cin, product[recNum].gName);
file << product[recNum].gName << endl;
cout << "- Brand Name: ";
getline(cin, product[recNum].bName);
file << product[recNum].bName << endl;
cout << "- Dosage Form: ";
getline(cin, product[recNum].dForm);
file << product[recNum].dForm << endl;
cout << "- Dosage Strength: ";
getline(cin, product[recNum].dStr);
file << product[recNum].dStr << endl;
cout << "- Date of Expiry" << endl;
cout << " (DD): ";
cin >> product[recNum].day;
file << product[recNum].day << endl;
cin.ignore();
cout << " (MM): ";
cin >> product[recNum].month;
file << product[recNum].month << endl;
cin.ignore();
cout << " (YYYY): ";
cin >> product[recNum].year;
file << product[recNum].year << endl;
cin.ignore();
cout << "- Quantity: ";
cin >> product[recNum].qty;
file << product[recNum].qty << endl;
cin.ignore();
cout << "- Price: RM ";
cin >> product[recNum].price;
file << product[recNum].price << endl << endl;
cin.ignore();
recNum++;
file.close();
}
cout << "\nRecord Saved!" << endl;
redo();
}while (again == true);
system("PAUSE");
Menu();
}
void Inventory::displayAll(Inventory product[])
{
file.open("zzz.txt", ios::in);
if(!file)
{
cout << "No Records in File" << endl;
system("PAUSE");
Menu();
}
for(int index = 0; index < recNum; index++)
{
cout << "\nCode: " << product[index].getCode() << endl;
cout << "\nGeneric Name: " << product[index].getGName() << endl;
cout << "\nBrand Name: " << product[index].getBName() << endl;
cout << "\nDosage Form: " << product[index].getDForm() << endl;
cout << "\nDosage Strength: " << product[index].getDStr() << endl;
cout << "\nDate of Expiry: " << product[index].getDay() << "-" << product[index].getMonth() << "-" << product[index].getYear() << endl;
cout << "\nQuantity: " << product[index].getQty() << endl;
cout << fixed << showpoint << setprecision(2);
cout << "\nUnit Price: " << product[index].getPrice() << endl;
cout << "\n" << "-----------------------------------------------------------------" << endl;
}
file.close();
system("PAUSE");
}
void Inventory::Search()
{
Inventory product[SIZE];
int scode;
int pos;
file.open("zzz.txt", ios::out);
if(!file)
{
cout << "No Records in File" << endl;
system("PAUSE");
Menu();
}
do
{
cout << "Search Code: ";
cin >> scode;
cin.ignore();
pos = searching(product, SIZE, scode);
if (pos == -1)
cout << "Record not found" << endl;
else
{
cout << "Record found!" << endl;
cout << "\nCode: " << product[pos].getCode() << endl;
cout << "\nGeneric Name: " << product[pos].getGName() << endl;
cout << "\nBrand Name: " << product[pos].getBName() << endl;
cout << "\nDosage Form: " << product[pos].getDForm() << endl;
cout << "\nDosage Strength: " << product[pos].getDStr() << endl;
cout << "\nDate of Expiry: " << product[pos].getDay()
<< "-" << product[pos].getMonth()
<< "-" << product[pos].getYear() << endl;
cout << "\nQuantity: " << product[pos].getQty() << endl;
cout << fixed << showpoint << setprecision(2);
cout << "\nUnit Price: " << product[pos].getPrice() << endl;
file.close();
}
redo();
}while(again == true);
}
void Menu()
{
int opt;
Inventory a,s, dA;
cout << "1. Add" << endl;
cout << "2. Display" << endl;
cout << "3. Search" << endl;
cout << "SELECTION: ";
cin >> opt;
cin.ignore();
switch(opt)
{
case 1: a.Inventory::Add();
break;
case 2: dA.displayAll();
break;
case 3:s.Search();
break;
default: cout << "Invalid Selection!" << endl;
break;
}
}
int main()
{
Menu();
system("PAUSE");
return 0;
}
int searching(Inventory object[], int size, int value)
{
int index = 0;
int position = -1;
bool found = false;
while (index < size && !found)
{
if(object[index].getCode() == value)
{
found = true;
position = index;
}
index++;
}
return position;
}
bool redo()
{
char x;
bool v = true;
do
{
cout << "\nWould you like to do another?";
cin >> x;
cin.ignore();
if (toupper(x) == 'Y')
{
v = true;
again = true;
}
else if (toupper(x) == 'N')
{
v = true;
again = false;
}
else
{
cout << "Invalid Option. Please try again" << endl;
v = false;
}
}while(v == false);
return again;
}
|