-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
149 lines (122 loc) · 5.32 KB
/
main.cpp
File metadata and controls
149 lines (122 loc) · 5.32 KB
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
#include <iostream>
#include <string>
#include "SQLServerConn.h"
#include "DBConn.h"
#include "CustomerManager.h"
#include "SupplierManager.h"
#include "SupplierNameManager.h"
#include "ProductManager.h"
#include "CartItemManager.h"
#include "TransactionManager.h"
#include "OrderItemManager.h"
#include "RetailApp.h"
int main() {
try {
// Table names
std::string connectionString = "DRIVER={SQL Server};SERVER=KN\\SQLEXPRESS;Trusted_Connection=yes;";
std::string dbName = "sample_store";
std::string customerTableName = "Customers";
std::string supplierTableName = "Suppliers";
std::string supplierNameTableName = "Supplier_Names";
std::string productTableName = "Products";
std::string cartItemTableName = "Cart_Items";
std::string transactionTableName = "Transactions";
std::string orderItemTableName = "Order_Items";
// Connect to SQL Server instance on
SQLServerConn connector;
connector.connect(connectionString);
// Connect to database
DBConn dbConn(connector.getHDBC());
if (!dbConn.dbExists(dbName)) {
dbConn.createDatabase(dbName);
}
dbConn.useDatabase(dbName);
// Create manager for 'customer'. Create table if it doesn't already exist
CustomerManager customerManager(dbConn, customerTableName);
if (!dbConn.tableExists(customerTableName)) {
customerManager.initTable();
}
/*
- Create managers for the SupplierName and Supplier.
NOTE: Then after this you must initialize the Supplier table first, adn then the
supplier name table. This is because the latter references the former, so we must make sure
the former exists before the latter.
*/
SupplierNameManager supplierNameManager(dbConn, supplierNameTableName, supplierTableName);
SupplierManager supplierManager(dbConn, supplierTableName, supplierNameManager);
if (!dbConn.tableExists(supplierTableName)) {
supplierManager.initTable();
}
if (!dbConn.tableExists(supplierNameTableName)) {
supplierNameManager.initTable();
}
// Create manager for products table
ProductManager productManager(dbConn, productTableName, supplierTableName);
if (!dbConn.tableExists(productTableName)) {
productManager.initTable();
}
// Create manager for cart items table and initialize table if it doesn't already exist
CartItemManager cartItemManager(dbConn, cartItemTableName, customerTableName, productTableName);
if (!dbConn.tableExists(cartItemTableName)) {
cartItemManager.initTable();
}
// Create manager for transactions table; needs for customer table to exist first
TransactionManager transactionManager(dbConn, transactionTableName, customerTableName);
if (!dbConn.tableExists(transactionTableName)) {
transactionManager.initTable();
}
// Create manager for order items table; needs the transaction and product table to exist first.
OrderItemManager orderItemManager(dbConn, orderItemTableName, transactionTableName, productTableName);
if (!dbConn.tableExists(orderItemTableName)) {
orderItemManager.initTable();
}
RetailApp myStore(customerManager, supplierManager, productManager, cartItemManager, transactionManager, orderItemManager);
int choice;
do {
// Display main menu and prompt input
std::cout << "Main Menu: " << std::endl;
std::cout << "1. Customers" << std::endl;
std::cout << "2. Suppliers" << std::endl;
std::cout << "3. Products" << std::endl;
std::cout << "4. Cart Items" << std::endl;
std::cout << "5. Transactions" << std::endl;
std::cout << "6. Quit" << std::endl;
std::cout << "Please enter a number to continue: ";
std::cin >> choice;
if (std::cin.fail()) {
std::cout << "Invalid input. Please enter a number!" << std::endl;
std::cin.clear(); // Clear the error flag
std::cin.ignore(64, '\n'); // Clear input buffer up to 64 characters or until newline is encountered
continue; // Restart the loop
}
switch (choice) {
case 1:
myStore.handleCustomerMenu();
break;
case 2:
myStore.handleSupplierMenu();
break;
case 3:
myStore.handleProductMenu();
break;
case 4:
myStore.handleCartMenu();
break;
case 5:
myStore.handleTransactionMenu();
break;
case 6:
std::cout << "Exiting Program!" << std::endl;
break;
default:
std::cout << "Invalid choice. Please enter a number between 1 and 5." << std::endl;
}
} while (choice != 6);
// Disconnect from SQL Server
connector.disconnect();
}
catch (const std::exception& ex) {
std::cerr << "Exception caught: " << ex.what() << std::endl;
}
return 0; // Don't forget to return a value from main
}