-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstocksystem.h
78 lines (62 loc) · 3.12 KB
/
stocksystem.h
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
// File: stocksystem.h
// Date: 2016-02-28
// Description: Declaration and partial implementation of a StockSystem class
#pragma once
#include <math.h>
#include <sstream>
#include "stockitem.h"
#include "redblacktree.h"
class StockSystem {
private:
RedBlackTree<StockItem> records;
double balance; // how much money you have in the bank
public:
// default constructor;
// begin with a balance of $100,000.00
StockSystem();
// returns the balance member
double GetBalance();
// Add a new SKU to the system. Do not allow insertion of duplicate sku
bool StockNewItem(StockItem item);
// Locate the item with key itemsku and update its description field.
// Return false if itemsku is not found.
bool EditStockItemDescription(unsigned int itemsku, string desc);
// Locate the item with key itemsku and update its description field.
// Return false if itemsku is not found or retailprice is negative.
bool EditStockItemPrice(unsigned int itemsku, double retailprice);
// Purchase quantity of item at unitprice each, to reach a maximum (post-purchase) on-hand stock quantity of 1000.
// Return false if balance is not sufficient to make the purchase,
// or if SKU does not exist, or if quantity or unitprice are negative.
// Otherwise, return true and increase the item's on-hand stock by quantity,
// and reduce balance by quantity*unitprice.
bool Restock(unsigned int itemsku, unsigned int quantity, double unitprice);
// Sell an item to a customer, if quantity of stock is available and SKU exists.
// Reduce stock by quantity, increase balance by quantity*price, and return true if stock available.
// If partial stock (less than quantity) available, sell the available stock and return true.
// If no stock, sku does not exist, or quantity is negative, return false.
bool Sell(unsigned int itemsku, unsigned int quantity);
// Return a formatted string containing complete stock catalogue information in the following format:
// <sku> <description> <quantity> <price> <newline>
string GetCatalogue() {
ostringstream strcatalogue;
int desclengthdiff;
int cataloguesize = 0; // create a variable which will be modified by tree's Dump function
StockItem* catalogue = records.Dump(cataloguesize);
strcatalogue << "SKU\tDESCRIPTION\t\t\tQTY\tPRICE\n";
for (int i = 0; i < cataloguesize; i++) {
strcatalogue << catalogue[i].GetSKU() << "\t" << catalogue[i].GetDescription();
// pad description to fill to next column. Tab width is up to 8 characters
desclengthdiff = 32 - catalogue[i].GetDescription().length();
for (int j = 0; j < ceil((double) desclengthdiff / 8); j++)
strcatalogue << "\t";
strcatalogue << catalogue[i].GetStock() << "\t$" << catalogue[i].GetPrice() << "\n";
}
return strcatalogue.str();
}
// Provides access to internal RedBlackTree.
// Used for grading.
// Note that this is dangerous in practice!
RedBlackTree<StockItem>& GetRecords() {
return records;
}
};