-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.cpp
94 lines (86 loc) · 2.51 KB
/
LinkedList.cpp
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
// LinkedList.cpp
#include "LinkedList.h"
#include <iostream>
using namespace std;
Node::Node(Customer customer) : customer(customer), next(nullptr) {}
LinkedList::LinkedList() : head(nullptr) {}
void LinkedList::insert(Customer customer, bool& check) {
if (head == nullptr) {
Node* newNode = new Node(customer);
newNode->next = head;
head = newNode;
}
else {
Customer* existingCustomer = findCustomerById(customer.getId());
if (existingCustomer == nullptr) {
Node* newNode = new Node(customer);
newNode->next = head;
head = newNode;
}
else {
cout << "Please check the ID you received from customer services as there is an existing one.\n";
cout << "Enter the right one as there shouldn't be two IDs equal.\n";
check = true;
}
}
}
void LinkedList::display() {
Node* temp = head;
while (temp != nullptr) {
temp->customer.displayAccountInfo();
temp = temp->next;
}
}
Customer* LinkedList::findCustomerById(int id) {
Node* temp = head;
while (temp != nullptr) {
if (temp->customer.getId() == id) {
return &(temp->customer);
}
temp = temp->next;
}
return nullptr;
}
void LinkedList::deleteCustomerById(int id, bool& check) {
Node* temp = head;
Node* prev = nullptr;
Customer* customer = findCustomerById(id);
if (customer == nullptr) {
cout << "There isn't an account with this ID " << id << "\n";
check = true;
}
else {
while (temp != nullptr) {
if (temp->customer.getId() == id) {
if (prev == nullptr) {
head = temp->next;
}
else {
prev->next = temp->next;
}
delete temp;
return;
}
prev = temp;
temp = temp->next;
}
}
}
void LinkedList::withdrawFromAccount(int id, double amount) {
Customer* customer = findCustomerById(id);
if (customer != nullptr) {
customer->withdraw(amount);
}
else {
cout << "Customer with ID " << id << " not found" << endl;
}
}
void LinkedList::displayAccountBalance(int id) {
Customer* customer = findCustomerById(id);
if (customer != nullptr) {
cout << "Account " << id << " balance: $" << customer->getBalance() << endl;
}
else {
cout << "Customer with ID " << id << " not found" << endl;
}
}