-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomerQueue.cs
107 lines (95 loc) · 2.72 KB
/
CustomerQueue.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise1
{
public class CustomerQueue
{
private readonly int maxSize = 5;
private Customer[] store;
private int head = 0;
private int tail = 0;
private int numItems = 0;
public CustomerQueue()
{
store = new Customer[maxSize];
}
public Customer GetCustomerAt(int index)
{
if (index < 0 || index >= numItems)
{
throw new IndexOutOfRangeException("Index out of range");
}
return store[(head + index) % maxSize];
}
public void Enqueue(Customer customer)
{
if (IsFull())
{
throw new InvalidOperationException("Queue is full");
}
store[tail] = customer;
tail += 1;
if (tail == maxSize)
{
tail = 0;
}
numItems += 1;
}
public Customer Dequeue()
{
if (IsEmpty())
{
throw new InvalidOperationException("Queue is empty");
}
Customer headItem = store[head];
head = head + 1;
if (head == maxSize)
{
head = 0;
}
numItems = numItems - 1;
return headItem;
}
public Customer Peek()
{
if (IsEmpty())
{
throw new InvalidOperationException("Queue is empty");
}
return store[head];
}
public bool IsEmpty() => numItems == 0;
public bool IsFull() => numItems == maxSize;
public int Count => numItems;
public float TotalAmountOwed()
{
float total = 0;
for (int i = 0; i < numItems; i++)
{
total += store[(head + i) % maxSize].AmountOwed;
}
return total;
}
public Customer MaxDebtCustomer()
{
if (IsEmpty())
{
return null;
}
Customer maxDebtCustomer = store[head];
int current = head;
for (int i = 0; i < numItems; i++)
{
int index = (current + i) % maxSize;
if (store[index].AmountOwed > maxDebtCustomer.AmountOwed)
{
maxDebtCustomer = store[index];
}
}
return maxDebtCustomer;
}
}
}