-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrder.cs
More file actions
161 lines (144 loc) · 4.3 KB
/
Order.cs
File metadata and controls
161 lines (144 loc) · 4.3 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SDP_Project
{
class Order
{
public required string Id { get; set; }
public required string DeliveryAddress { get; set; }
public required DateTime DeliveryTime { get; set; }
public required double Amount { get; set; }
public PaymentStatus PaymentStatus { get; set; }
public OrderState? State { get; set; }
public required PaymentStrategy PaymentStrategy { get; set; }
public List<OrderItem> OrderItems { get; set; }
public Restaurant? Restaurant { get; set; }
public double TotalPrice { get; set; }
public Order()
{
OrderItems = new List<OrderItem>();
PaymentStatus = PaymentStatus.Pending;
State = new InCartState(this);
}
public void Submit()
{
if (State == null)
{
Console.WriteLine("Order is no longer active (cancelled or archived).");
return;
}
State.Submit();
}
public void Pay()
{
if (State == null)
{
Console.WriteLine("Order is no longer active (cancelled or archived).");
return;
}
State.Pay();
}
public void Confirm()
{
if (State == null)
{
Console.WriteLine("Order is no longer active (cancelled or archived).");
return;
}
State.Confirm();
}
public void Reject()
{
if (State == null)
{
Console.WriteLine("Order is no longer active (cancelled or archived).");
return;
}
State.Reject();
}
public void Cancel()
{
if (State == null)
{
Console.WriteLine("Order is no longer active (cancelled or archived).");
return;
}
State.Cancel();
}
public void StartPreparation()
{
if (State == null)
{
Console.WriteLine("Order is no longer active (cancelled or archived).");
return;
}
State.StartPreparation();
}
public void StartDelivery()
{
if (State == null)
{
Console.WriteLine("Order is no longer active (cancelled or archived).");
return;
}
State.StartDelivery();
}
public void CompleteDelivery()
{
if (State == null)
{
Console.WriteLine("Order is no longer active (cancelled or archived).");
return;
}
State.CompleteDelivery();
}
public void ProcessPayment()
{
bool success = PaymentStrategy.Pay(TotalPrice);
if (success)
{
Console.WriteLine("Payment received.");
PaymentStatus = PaymentStatus.Paid;
State = new PendingConfirmationState(this);
}
else
{
Console.WriteLine("Payment failed.");
PaymentStatus = PaymentStatus.Failed;
}
}
public void Refund()
{
bool success = PaymentStrategy.Refund(TotalPrice);
PaymentStatus = PaymentStatus.Refunded;
if (success)
Console.WriteLine("Order refunded.");
else
Console.WriteLine("Refund failed or not applicable.");
}
public void AddOrderItem(OrderItem item)
{
OrderItems.Add(item);
RecalculateTotal();
}
public void RecalculateTotal()
{
if (Restaurant != null)
{
Amount = Restaurant.CalculateDiscountedTotal(OrderItems);
}
else
{
Amount = OrderItems.Sum(item => item.Cost);
}
}
public void SetRestaurant(Restaurant restaurant)
{
Restaurant = restaurant;
RecalculateTotal();
}
}
}