-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFCITacounts.java
More file actions
200 lines (121 loc) · 6.07 KB
/
FCITacounts.java
File metadata and controls
200 lines (121 loc) · 6.07 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
/* Reem khalil - ID : 1505841 - section number : BAR - EMAIL : rkhalil0006@stu.kau.edu.sa */
package fcitmobile2;
import java.io.PrintWriter;
public class FCITacounts { /* This is the main linked list class ... This linked list will store objects of type FCITcustomer ... */
// ................. Data Members .................. //
private FCITcustomer head;
// .................... Methods .................... //
// Constructor
FCITacounts() {
head = null;
}
// setter method
public void setHead(FCITcustomer head) {
this.head = head;
}
// getter method
public FCITcustomer getHead() {
return head;
}
// serves methods .. necessary methods for linked-list operations
public boolean isEmpty() { // Method to check if list is empty
return head == null;
}
public void printAllCustomer(PrintWriter print) { // Method to Traverse and print linked list
FCITcustomer helpPtr = head;
while (helpPtr != null) { // Traverse to correct insertion point
print.println("\tName: " + helpPtr.getFirstName() + " " + helpPtr.getLastName());
print.println("\tCustomer ID: " + helpPtr.getID());
print.println("\tPhone Number: " + helpPtr.getPhoneNumber());
print.printf("\tBalance: %.2f", (helpPtr.getBalance()));
print.println();
helpPtr = helpPtr.getNext(); // Step one node over
}
}
public void add(FCITcustomer newCustomer) { // Method to Adding Nodes to a sorted Linked List
// First // we take care of Insertion into an empty list OR Insertion at the front of a non-empty list
if (head == null || head.getID() > newCustomer.getID()) {
newCustomer.setNext(head); // Point the new node to its successor
head = newCustomer; // Point the predecessor node to the new node
return;
}
// ELSE // the insertion spot is somewhere later in the list So traverse list to correct location and insert!
FCITcustomer helpPtr = head;
while (helpPtr.getNext() != null) {
if (helpPtr.getNext().getID() > newCustomer.getID()) {
break; // we found our spot and break out of the while loop
}
helpPtr = helpPtr.getNext(); // Step one node over
}
newCustomer.setNext(helpPtr.getNext()); // Point the new node to its successor
helpPtr.setNext(newCustomer); // Point the predecessor node to the new node
}
public void delete(int ID) { // Method to delete specific node .
if (!isEmpty()) {
if (head.getID() == ID) // if “ID” is in the first node
{
head = head.getNext();
} else { // ELSE, ID is (possibly) elsewhere in the list
FCITcustomer help_ptr = head;
while (help_ptr.getNext() != null) {
if (help_ptr.getNext().getID() == ID) {
help_ptr.setNext(help_ptr.getNext().getNext());
break;
}
help_ptr = help_ptr.getNext();
}
}
}
}
public boolean search(int data) { // 2 methods to check If the record is found in the accounts linked-list
return search(head, data);
}
private boolean search(FCITcustomer p, int ID) {
// We need to traverse...so we need a help ptr
FCITcustomer helpPtr = p;
while (helpPtr != null) {
if (helpPtr.getID() == ID) {
return true;
}
helpPtr = helpPtr.getNext();
}
return false;
}
public FCITcustomer findNode(int ID) { // 2 methods to check If the record is found in the accounts linked-list & return the node
return findNode(head, ID);
}
private static FCITcustomer findNode(FCITcustomer p, int ID) {
// We need to traverse...so we need a help ptr
FCITcustomer helpPtr = p;
while (helpPtr != null) {
if (helpPtr.getID() == ID) {
return helpPtr; // return the node
}
helpPtr = helpPtr.getNext();
}
return null;
}
public void ModifyNodes( PrintWriter print ) { // 2 methods to Modify node that have a balance less than the SMS rate (0.08 sr) in the accounts linked-list
ModifyNodes(head , print);
}
private void ModifyNodes(FCITcustomer head , PrintWriter print) {
print.println("\tThe following bills have been paid:");
boolean paid = false ;
// We need to traverse...so we need a help ptr
FCITcustomer helpPtr = head;
// Traverse to correct insertion point
while (helpPtr != null) {
if (helpPtr.getBalance() < 0.08) { // check if there are accounts have a balance less than the SMS rate (0.08 sr)
paid = true;
helpPtr.setBalance(30); // the balance will be reset to 30sr
helpPtr.getCalls().delete(); // and the call/SMS history will be reset
helpPtr.setTextedNumbers(new String [10]);
print.println("\t>ID #" +helpPtr.getID()+ " ("+helpPtr.getName()+")" );
}
// Step one node over
helpPtr = helpPtr.getNext();
}
if (!paid)
print.println("\t\tNo bills were paid. All accounts had a balance greater than zero.");
}
}