-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOrionSystems.cs
More file actions
277 lines (260 loc) · 10.4 KB
/
OrionSystems.cs
File metadata and controls
277 lines (260 loc) · 10.4 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
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
276
277
/*
* Author: Vishva Patel
* Problem : TCSXPLORE_OPA 26th June 2020
* Orion Systems Problem
*/
using System;
using System.Collections.Generic;
namespace Orders
{
class Program
{
static void Main(string[] args)
{
ProductManager manager = new ProductManager();
//Adding Some data to start with
manager.AddProduct("Mobile Phone", "Goods", 100, 10999);
manager.AddProduct("Television", "Goods", 50, 30999);
manager.AddProduct("Customer Relations", "Services", 0, 50000); //As in services there is no quantity.
manager.AddProduct("Backup & Restore", "Services", 0, 20943);
manager.AddProduct("Laptops", "Goods", 200, 25999);
sbyte choice = Convert.ToSByte(Console.ReadLine());
//This is where the real program begins as the above code was prewritten.
switch (choice)
{
case 1:
//Add Product Case
string Name = Console.ReadLine();
string category = Console.ReadLine();
double unitPrice = Convert.ToDouble(Console.ReadLine());
if (category == "Goods")
{
int Quantity = Convert.ToInt32(Console.ReadLine());
int Id = manager.AddProduct(Name, category, Quantity, unitPrice);
if (Id == 0)
{
Console.WriteLine($"Product already exist, cannot add again");
break;
}
else
{
Console.WriteLine($"The Product {Name} has been added with the product id {Id}");
}
}
else if (category == "Services")
{
//Services do not include the attribute quantity so we do not take anything from user.
int Id = manager.AddProduct(Name, category, 0, unitPrice);
if (Id == 0)
{
Console.WriteLine($"Product already exist, cannot add again");
break;
}
else
{
Console.WriteLine($"The Product {Name} has been added with the product id {Id}");
}
}
else
{
Console.WriteLine("Wrong Category");
}
break;
case 2:
//Updating the product
int Id1 = Convert.ToInt32(Console.ReadLine());
int quantity = Convert.ToInt32(Console.ReadLine());
double uPrice = Convert.ToDouble(Console.ReadLine());
Product pd;
pd = manager.UpdateProduct(Id1, quantity, uPrice);
if (pd == null)
{
Console.WriteLine("Prduct does not exist, cannot modify");
}
else
{
Console.WriteLine($"Product {pd.ProductName} has been updates with quantity {pd.Quantity} and unit price of {pd.unitPrice}");
}
break;
case 3:
//Viewing the Products
string cat = Console.ReadLine();
double totalprice;
int goodstax = 12, servicestax = 10;
bool goodsflag = false, servicesflag = false;
List<Product> productlist = new List<Product>();
if (cat == "Goods" || cat == "Services")
{
productlist = manager.ViewProducts(cat);
//Printing for Goods
if (cat == "Goods")
{
foreach (Product p in productlist)
{
goodsflag = true;
//tax = 12% for good category
totalprice = p.unitPrice + 0.12 * p.unitPrice;
Console.WriteLine($"Category: {p.ProductCategory}, Tax: {goodstax}%");
Console.WriteLine($"{p.ProductId}, {p.ProductName}: {p.Quantity} left, unit price: {p.unitPrice}, total price: {totalprice} ");
}
if (goodsflag == false)
{
Console.WriteLine("No Products found");
}
}
//Printing for Services
if (cat == "Services")
{
foreach (Product p in productlist)
{
servicesflag = true;
//tax = 12% for good category
totalprice = p.unitPrice + 0.10 * p.unitPrice;
Console.WriteLine($"Category: {p.ProductCategory}, Tax: {servicestax}%");
Console.WriteLine($"{p.ProductId}, {p.ProductName}: unit price: {p.unitPrice}, total price: {totalprice} ");
}
if (servicesflag == false)
{
Console.WriteLine("No Products Found");
}
}
}
else
{
Console.WriteLine("Wrong Category");
}
break;
case 4:
//Getting the statistcs of the products.
Dictionary<string, int> GroupedProducts = new Dictionary<string, int>();
GroupedProducts = manager.Statistics();
bool goodflag = false, serviceflag = false;
foreach (var d in GroupedProducts)
{
if (d.Key == "Goods")
{
goodflag = true;
Console.WriteLine($"Category: {d.Key}, products: {d.Value}");
}
if (d.Key == "Services")
{
serviceflag = true;
Console.WriteLine($"Category: {d.Key}, products: {d.Value}");
}
}
if (goodflag == false)
{
Console.WriteLine("No Products found");
}
if (serviceflag == false)
{
Console.WriteLine("No Products Found");
}
break;
}
}
}
class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ProductCategory { get; set; }
public int Quantity { get; set; }
public double unitPrice { get; set; }
public double tax { get; set; }
public double totalPrice { get; set; } //unitPrice + Tax
public Product(int Id, string pnm, string cat, int q, double up)
{
ProductId = Id;
ProductName = pnm;
ProductCategory = cat;
Quantity = q;
unitPrice = up;
}
}
class ProductManager
{
private List<Product> Products = new List<Product>();
private static int ProductId = -1;
private Product updatedProduct;
public int AddProduct(string pnm, string cat, int q, double up)
{
ProductId++;
Product p = new Product(ProductId, pnm, cat, q, up);
if (Products.Contains(p))
{
ProductId--;
return 0;
}
else
{
Products.Add(p);
return ProductId;
}
}
//Updating the product in the product list
public Product UpdateProduct(int id, int q,double up)
{
bool flag = false;
foreach(Product p in Products)
{
if(p.ProductId == id)
{
if (p.ProductCategory == "Services")
{
p.unitPrice = up;
updatedProduct = p;
flag = true;
break;
}
else
{
p.Quantity = q;
p.unitPrice = up;
updatedProduct = p;
flag = true;
break;
}
}
}
if (flag == false)
{
return updatedProduct = null;
}
else
{
return updatedProduct;
}
}
//View based on category
public List<Product> ViewProducts(string cat)
{
List<Product> Categoryproduct = new List<Product>();
foreach(Product p in Products)
{
if(p.ProductCategory == cat)
{
Categoryproduct.Add(p);
}
}
return Categoryproduct;
}
//Statistics of Products
public Dictionary<string, int> Statistics()
{
Dictionary<string, int> stats = new Dictionary<string, int>();
foreach(Product p in Products)
{
if (stats.ContainsKey(p.ProductCategory))
{
stats[p.ProductCategory] += 1;
}
else
{
stats.Add(p.ProductCategory, 1);
}
}
return stats;
}
}
}