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
|
#include <iostream>
#include <string>
#include <Windows.h> // for gotoxy function
using namespace std;
struct Stock
{
int prodNo;
string prodName;
string custName;
double price;
int stock;
int sold;
int left;
void setStock(int id, string pName, string cName, double cost, int stk, int sld, int lft)
{
prodNo = id;
prodName = pName;
custName = cName;
price = cost;
stock = stk;
sold = sld;
left = lft;
}
};
void gotoxy(int x, int y);
int main()
{
const int NUM_ITEMS = 5;
Stock Items[NUM_ITEMS];
Stock temp; // for bubble sort
// create some dummy data
Items[0].setStock(1, "Shampoo", "Michael", 10.99, 6, 2, 4);
Items[1].setStock(2, "Pepsi", "John", 0.59, 6, 1, 5);
Items[2].setStock(3, "Batteries", "Debbie", 0.99, 11, 2, 9);
Items[3].setStock(1, "Shampoo", "Debbie", 10.99, 4, 2, 2);
Items[4].setStock(1, "Shampoo", "Paul", 10.99, 2, 1, 1);
cout << " ***** INVENTORY SYSTEM CS127L 4TH QTR*****" << endl;
cout << "PROD NO. PRODUCT NAME PRICE STOCK SOLD LEFT\n";
// before displaying, bubble sort the list so that
// its listed in ascending order.
for (int passes = 0; passes < NUM_ITEMS; passes++)
{
for (int j = 0; j < NUM_ITEMS - passes - 1; j++)
{
if (Items[j].prodNo > Items[j + 1].prodNo)
{
temp = Items[j];
Items[j] = Items[j + 1];
Items[j + 1] = temp;
}
}
}
// now it will look like
// ***** INVENTORY SYSTEM CS127L 4TH QTR*****
// PROD NO.PRODUCT NAME PRICE STOCK SOLD LEFT
// [1] Shampoo Michael 10.99 6 2 4
// [1] Shampoo Debbie 10.99 4 2 2
// [1] Shampoo Paul 10.99 2 1 1
// [2] Pepsi John 0.59 6 1 5
// [3] Batteries Debbie 0.99 11 2 9
int last = 0;
for (int orders = 0; orders < NUM_ITEMS; orders++)
{
// display whats common to both
gotoxy(25, orders + 2); cout << Items[orders].custName;
gotoxy(36, orders + 2); cout << Items[orders].price;
gotoxy(47, orders + 2); cout << Items[orders].stock;
gotoxy(55, orders + 2); cout << Items[orders].sold;
gotoxy(62, orders + 2); cout << Items[orders].left;
// is the current one the same as the last?
if (Items[orders].prodNo != last) {
// no so show the item number and product name
gotoxy(2, orders + 2); cout << "[" << Items[orders].prodNo << "]";
gotoxy(11, orders + 2); cout << Items[orders].prodName;
}
// store the current in our last for next check.
last = Items[orders].prodNo;
}
return 0;
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
|