-
Notifications
You must be signed in to change notification settings - Fork 1
/
Buyer1.cs
161 lines (134 loc) · 4.17 KB
/
Buyer1.cs
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
namespace task_2
{
internal class Buyer : User, IComparable<Buyer>
{
private Order[] orders;
private Product[] products;
private int logicSize = 0;
private int productSizeLogic = 0;
private int size = 2;
public Buyer(string name, string password, Address address) : base(name, password, address)
{
products = new Product[size];
}
public Buyer(User others) : base(others)
{
products = new Product[size];
}
public Buyer() : base() { }
public bool SetProduct(Product productDetails, bool hasSpecialBox)
{
ProductExtraFields productExtraFields = new ProductExtraFields(productDetails.Id,productDetails.Name,
productDetails.Price,productDetails.Category,hasSpecialBox);
if (productExtraFields == null)
throw new ArgumentException("adding product fails");
Product[] tempNewProducts;
if (productSizeLogic == products.Length)
{
tempNewProducts = new Product[products.Length * size];
products.CopyTo(tempNewProducts, 0);
products = tempNewProducts;
}
products[productSizeLogic] = productExtraFields;
productSizeLogic++;
return true;
}
public int GetPriceCart()
{
int priceCart = 0;
if (products != null)
foreach (Product productDetail in products)
{
priceCart += productDetail.Price;
}
return priceCart;
}
public bool SetOrderArr()
{
Order[] newOrder;
if (orders == null)
{
orders = new Order[1];
}
else
{
newOrder = new Order[orders.Length + 1];
orders.CopyTo(newOrder, 0);
orders = newOrder;
}
this.orders[orders.Length - 1] = new Order(products, products.Length, new User(Name, Password, Address));
return true;
}
public bool RemoveAllCartProducts()
{
for (int i = 0; i < products.Length; i++)
{
products[i] = null;
}
logicSize = 0;
productSizeLogic = 0;
return true;
}
public override string ToString()
{
string baseString = base.ToString();
foreach (Product product in products)
{
if (product != null)
baseString += product.ToString();
}
if (orders != null)
{
baseString += "\n\tHistory shopping:\n";
foreach (Order order in orders)
{
baseString += order.ToString();
}
}
return baseString;
}
public override bool Equals(object obj)
{
if(Name == (string)obj)
return true;
else
{
return false;
}
}
public int CompareTo(Buyer other)
{
float totalSumOthers = 0;
float totalSum = 0;
if(other.orders == null && orders == null)
{
Console.WriteLine("Shopping cart is empty");
// add trhow
}
else
{
foreach (Order order in other.orders)
{
totalSumOthers += order.TotalPrice;
}
foreach (Order order in orders)
{
totalSum += order.TotalPrice;
}
}
if (totalSumOthers > totalSum)
return -1;
else if (totalSumOthers < totalSum)
return 1;
return 0;
}
}
}