-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBamazonCustomer.js
More file actions
180 lines (162 loc) · 6.14 KB
/
BamazonCustomer.js
File metadata and controls
180 lines (162 loc) · 6.14 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
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
//Welcome to Bamazon, use this application to find and purchase
//your favorite Bamazon items
var inquirer = require('inquirer');
var Table = require('cli-table');
var colors = require('colors');
//connects to the Bamazon_db database
var mysql = require('mysql');
var con = mysql.createConnection({
host: "127.0.0.1",
port: 3306,
user: "root", //Your username
password: "root", //Your password
database: "Bamazon_db"
});
//shows if you're connected or not
con.connect(function(err, res, fields) {
if (err) throw err;
loadProcucts();
});
function loadProcucts() {
//Display All Items available for sale
con.query('SELECT * FROM products;', function(err, res) {
if (err) throw err;
//creates a table for the information from the mysql database to be placed
var table = new Table({
head: ['Item Id#', 'Product Name', 'Price', 'Quantity'],
style: {
head: ['yellow'],
compact: false,
colAligns: ['center'],
}
});
var productArray = [];
var pName = [];
//Creates the pretty table and also array to get the product information from
for (var i = 0; i < res.length; i++) {
table.push(
[res[i].ItemID, res[i].ProductName, res[i].Price, res[i].StockQuanitiy]
);
productArray.push(
[res[i].ItemID, res[i].ProductName, res[i].Price, res[i].StockQuanitiy]
);
pName.push(res[i].ProductName);
}
console.log(" Welcome to the Nooga Delivery CLI Store".america);
console.log(" Our Available inventory ");
console.log("********************************************************************");
console.log(table.toString());
promptCustomer(pName, productArray);
});
}
function promptCustomer(inventory, table) {
inquirer
.prompt([{
type: "list",
name: "option",
message: ("What is the name of the item would you like to buy?".green),
choices: inventory
}])
.then(function(val) {
var choiceProduct = val.option;
var product;
for (var i = 0; i < table.length; i++) {
if (table[i][1] === choiceProduct) {
console.log(choiceProduct + " Chosen option");
product = checkInventory(choiceProduct, table[i]);
var price = table[i][2];
}
}
// If there is a product with the name the user chose, prompt the customer for a desired quantity
if (product) {
console.log("There is a product " + choiceProduct.red + " available for purchase");
// Pass the chosen product to promptCustomerForQuantity
promptCustomerForQuantity(product, choiceProduct, price);
} else {
// // Otherwise let them know the item is not in the inventory, re-run loadProducts
console.log("\nThat item is not in the inventory. " + product).red;
loadProducts();
}
});
}
// Check to see if the product the user chose exists in the inventory
function checkInventory(choiceProduct, inventory) {
console.log(choiceProduct + " Chosen | " + inventory[3] + " available");
var currentInventory = inventory[3];
for (var i = 0; i < currentInventory; i++) {
if (choiceProduct === currentInventory[i]) {}
// If a matching product is found, return the product
return currentInventory;
}
// Otherwise return null
return null;
}
var quantityQuestions = [{
type: "input",
name: "quantity",
message: ("How many would you like to buy?".green),
validate: function(val) {
return !isNaN(val) || val.toLowerCase() === "q";
}
}];
function promptCustomerForQuantity(product, choiceProduct, price) {
inquirer
.prompt(quantityQuestions)
.then((val) => {
checkIfExit(val.quantity);
var quantity = parseInt(val.quantity);
// If there isn't enough of the chosen product and quantity, let the user know and offer to shop more
if (quantity > product) {
console.log("\nInsufficient quantity!");
shopMore();
// loadProducts();
} else {
// Otherwise run makePurchase, give it the product information and desired quantity to purchase
console.log("Purchased!");
makePurchase(choiceProduct, quantity, price);
}
console.log(quantity + " Is the Quantity requested");
console.log(product + " is the quantity on hand");
});
}
// Purchase the desired quantity of the desired item
function makePurchase(choiceProduct, quantity, price) {
let itemTotal = price * quantity;
con.query(
"UPDATE products SET StockQuanitiy = StockQuanitiy - ? WHERE ProductName = ?", [quantity, choiceProduct],
function(err, res) {
if (err) throw err;
// console.log(res);
// Let the user know the purchase was successful, offer to shop more
console.log("\nSuccessfully purchased " + quantity + " " + choiceProduct + "'s!");
console.log(`Each ${choiceProduct} is ${price.toFixed(2)}`);
console.log("\nYour total is: $" + itemTotal.toFixed(2));
shopMore();
}
);
}
function shopMore() {
inquirer.prompt(
[{
type: "list",
name: "shop",
message: ("Have you finished Shopping?".green),
choices: ['Yes', 'No']
}]
)
.then(answers => {
if (answers.shop === "No") {
loadProcucts();
} else {
console.log("Thank you for Shopping with us!");
process.exit(0);
}
});
}
function checkIfExit(choice) {
if (choice.toLowerCase() === "q") {
// Log a message and exit the current node process
console.log("Goodbye!");
process.exit(0);
}
}