-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathairbnbSingleLinkedList
More file actions
397 lines (374 loc) · 11.2 KB
/
airbnbSingleLinkedList
File metadata and controls
397 lines (374 loc) · 11.2 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Hailey Martin
* Description:
* This program will create two linked lists to hold
* Airbnbs, the information for the Airbnbs will be read
* from file. A single linked list will order the Airbnbs
* by the number of guests. The double linked list will
* order Airbnbs as they were read from file. The single
* linked list will print linked list moving forward and
* the doubly linked list will print backwards. Using a
* priority queue, Airbnbs will be checked if they meet
* the requirements(read from another file), and if they
* do they will be then placed in the priority queue.
* Airbnbs will then be removed from single linked list
* if they are of a specific type. Last the single linked
* list will print the list after removal of specific type
* of Airbnbs
*/
import java.io.IOException;
import java.util.PriorityQueue;
import java.io.File;
import java.util.Scanner;
public class MartinHaileyAssignment9 {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
//fill single and double linked lists with airbnbs
// Name of files to read from
final String LINKEDLIST_FILE_NAME = "Airbnbs.txt";
final String REQUIREMENTS_FILE_NAME = "AirbnbRequirements.txt";
// Setup file reference variables to refer to each text file
File LinkedListFileName = new File(LINKEDLIST_FILE_NAME);
File RequirementsFileName = new File(REQUIREMENTS_FILE_NAME);
//create linked lists
AirbnbLinkedList singleLL = new AirbnbLinkedList();
DoubleLinkedList doubleLL = new DoubleLinkedList();
//fill Linked lists with file information
Scanner linkedListFile = new Scanner (LinkedListFileName);
Scanner requirementsFile = new Scanner (RequirementsFileName);
while(linkedListFile.hasNext()){
// Read Airbnb information from the file into separate variables
int rate = linkedListFile.nextInt();
int numberGuests = linkedListFile.nextInt();
int numberBedrooms = linkedListFile.nextInt();
String airbnbType = linkedListFile.nextLine().trim();
// Create a Airbnb object
Airbnb airbnb = new Airbnb(rate, numberGuests, numberBedrooms,
airbnbType);
//add instance of Airbnb to single linked list
singleLL.addByGuests(airbnb);
//add instance of airbnb
doubleLL.addToEnd(airbnb);
}//end while loop
//Priority queue of airbnbs that meet requirements
PriorityQueue<Airbnb> airbnbMeetsRequirements = new PriorityQueue<>();
while(requirementsFile.hasNext()) {
// Read requirements info from the file into separate variables
final int RATE = requirementsFile.nextInt();
final int NUMBER_GUESTS = requirementsFile.nextInt();
final String AIRBNB_TYPE = requirementsFile.nextLine().trim();
//find the airbnbs that meet these requirements
airbnbMeetsRequirements = singleLL.findAirbnb(RATE,
NUMBER_GUESTS, AIRBNB_TYPE);
}//end while loop
System.out.println("Airbnbs in Single Linked List - Ordered "
+ "by Number of Guests\r\n");
//display single linked list ordered
singleLL.print();
//display double linked list backwards
System.out.println("Airbnbs in Doubly Linked List - Airbnbs Printed Backwards");
doubleLL.printBackwards();
//find airbnbs that meet requirements
System.out.println("Airbnbs in Priority Queue Meeting Parrot Requirements:\n");
System.out.println("Airbnbs Type \t\tNightly Rate \t\tGuests \t\tBedrooms\r\n"
+ "--------------------------------------------------------------");
//loop through priority queue to print, until queue is empty
while(!airbnbMeetsRequirements.isEmpty()) {
System.out.println(airbnbMeetsRequirements.remove().toString());
}
//remove airbnbs form single linked list
int numApartments = singleLL.removeSpecificType("Apartment");
int numCondos = singleLL.removeSpecificType("Condo");
System.out.printf("Airbnbs in Single Linked List - Apartments and "
+ "Condos Removed\nNumber apartments removed: %d\nNumber "
+ "condos removed: %d\n",numApartments,numCondos);
//display single linked list without apartments and condos
singleLL.print();
linkedListFile.close();
requirementsFile.close();
}//end main
}//end class
/*
* Represents an Airbnb which could be a house,
* apartment, condo, townhome, etc. Implement
* Comparable, so Airbnbs can be compared.
*/
class Airbnb implements Comparable<Airbnb>{
//Private Date fields
private int nightlyRate;
private int numGuests;
private int numBedrooms;
private String type;
//Public Methods
//Constructor
public Airbnb(int nightlyRate, int numGuests,
int numBedrooms,String type) {
//initializes all private data fields with
//incoming values read from file
this.nightlyRate = nightlyRate;
this.numGuests = numGuests;
this.numBedrooms = numBedrooms;
this.type = type;
}
//Getters for private data fields
public int getNightlyRate() {
return nightlyRate;
}
public int getNumGuests() {
return numGuests;
}
public int getNumBedrooms() {
return numBedrooms;
}
public String getType() {
return type;
}
/*
* toString method returns formated string
*/
@Override
public String toString() {
//Returns a string that combines the Airbnb’s
//type, nightly rate, # guests, #bedrooms
//in a nice format:
return String.format("\n%-10s\t\t %-10d\t\t "
+ "%-10d\t\t %-10d\n",
type, nightlyRate, numGuests,numBedrooms);
}
/*
* Parameters: Airbnb otherAirbnb
* Return: and int based on otherAirbnb
*/
@Override
public int compareTo(Airbnb otherAirbnb) {
//Compares two Airbnbs based on the number of guests
//where a lower number of guests has higher priority
//Returns integer value (-1, 0, 1) based on result of
//comparing two Airbnbs This method is used by the
//priority queue.
if(this.numGuests < otherAirbnb.numGuests) {
return -1;
}else if(this.numGuests > otherAirbnb.numGuests){
return 1;
}else {
return 0;
}
}
}//end Airbnb class
//A singly-linked list constructed from nodes
//containing Airbnbs
class AirbnbLinkedList{
//Private Data fields
private Node head;//reference to the 1st node in the linked list
//public methods
//Constructor
public AirbnbLinkedList() {
//Initializes head reference to null
head = null;
}
/*
* Parameters: Airbnb airbnbToAdd
* Return: void
*/
public void addByGuests (Airbnb airbnbToAdd) {
//Add a new node to the linked list based on the
//Airbnb’s number of guests. The Airbnb’s compareTo
//method will be used to help find the correct
//location to insert the new Airbnb
Node newNode = new Node(airbnbToAdd);
Node current = head;
Node previous = null;
//first check if head is null, if it is then
//list is empty and this is the first node
if(head == null) {
head = newNode;
}else {
while(current != null && current.airbnb.compareTo(airbnbToAdd)< 0) {
//move current and previous
previous = current;
current = current.next;
}
//if previous is null assign head to newNode otherwise
//previous next will point to newNode
if(previous == null) {
head = newNode;
}else {
previous.next = newNode;
}
//newnode will point to current
newNode.next = current;
}//end of else
}//end of add guests
/*
* Parameters: integer-nightlyRate,
* numGuests, type
* Return: PriorityQueue
*/
public PriorityQueue<Airbnb> findAirbnb (int nightlyRate,
int numGuests, String type){
//Traverse the single linked list and find all Airbnbs
//that are: Less than or equal to nightly rate
//Greater than or equal to number of guests
//equal to the type
//place these Airbnbs into a priority queue
PriorityQueue<Airbnb> priority = new PriorityQueue<>();
Node currentNode = head;
while(currentNode!= null) {
if(currentNode.airbnb.getNightlyRate() <= nightlyRate) {
if(currentNode.airbnb.getNumGuests() >= numGuests) {
if(currentNode.airbnb.getType().equals(type)) {
priority.add(currentNode.airbnb);
}
}
}
currentNode = currentNode.next;
}
return priority;
}
/*
* Parameters: String typeToRemove
* Return: int
*/
public int removeSpecificType(String typeToRemove) {
//Remove all Airbnbs from the single linked list
//with the incoming specified type. Return the
//number of Airbnbs removed
int numberAirbnbsRemoved = 0;
Node currentNode = head;
Node previousNode = null;
//while current is not null search for type based on incoming
//type to be removed. If type is found exit loop
while(currentNode!=null) {
boolean found = false;
while(currentNode!=null && !found) {
if(currentNode.airbnb.getType().equals(typeToRemove)) {
found = true;
numberAirbnbsRemoved++;
}else {
//move pointers to loop forward
previousNode = currentNode;
currentNode = currentNode.next;
}
}
//if node was found then remove
if(found) {
if(previousNode == null) {
//removing first node
head = currentNode.next;
}else {
previousNode.next = currentNode.next;
}
currentNode = currentNode.next;
}
}
return numberAirbnbsRemoved;
}
/*
* Parameters: NA
* Return: void
*/
public void print() {
//Display Airbnbs in order from the head of the
//linked list to the end. Be sure head is still
//referring to the 1st node when print is complete.
//Use the Airbnbs’ toString() method to print the
//Airbnb details.
// Walk list and create a string with the values
Node current = head;
//while current does not point to null print current
//node using toString method
System.out.println("\nAirbnb Type\t\t Nightly Rate "
+ "\t\tGuests \t\tBedrooms\r\n------------------------------"
+ "--------------------------------");
while(current != null) {
String result = current.airbnb.toString();
current = current.next;
System.out.println(result);
}
}
//Private Inner Class
//Represents one node in the linked list A node
//stores an Airbnb object in the data field and a
//reference to the next node (Airbnb) in the next
//field
private class Node{
//Private Data Fields
private Airbnb airbnb;//data stored in a node
private Node next;//reference to next node in linked list
//Public Methods
//Constructor
public Node(Airbnb airbnb) {
//Initializes airbnb to the incoming Airbnb and
//next reference to null
this.airbnb = airbnb;
next = null;
}
}//end Node class
}//end AirbnbLinkedList class
//A doubly-linked list constructed from nodes containing
//Airbnbs
class DoubleLinkedList {
//Private Data fields
private Node head;
private Node tail;
//public methods
//constructor
public DoubleLinkedList() {
//Initializes head and tail references to null
head = null;
tail = null;
}
/*
* Parameter: Airbnb airbnbToAdd
* Return: void
*/
public void addToEnd (Airbnb airbnbToAdd) {
//Create a new node, placing the incoming element into the
//data field
Node newNode = new Node(airbnbToAdd);
// If list is empty
if(tail == null) {
head = tail = newNode;
}else {
// add node to last node in list by setting the tail's
//next reference to newNode and the tail to newNode
tail.next = newNode;
newNode.previous = tail;
tail = newNode;
}
}//end addToEnd
/*
* Parameter: NA
* Return: void
*/
public void printBackwards() {
Node current = tail;
System.out.println("\nAirbnb Type\t\t Nightly Rate "
+ "\t\tGuests \t\tBedrooms\r\n------------------------------"
+ "--------------------------------");
//current starts at the tail and moves to previous reference
while(current != null) {
System.out.println(current.airbnb.toString());
current = current.previous;
}
}
//Private Inner Class
//Represents one node in the linked list. A node
//stores an Airbnb in the data field and a reference
//to the next node (Airbnb)in the next field
private class Node{
//Private Data Fields
private Airbnb airbnb;
private Node previous;
private Node next;
//Public Methods
//Constructor
public Node(Airbnb airbnb) {
//Initializes airbnb to the incoming Airbnb and
//next and previous references to null
this.airbnb = airbnb;
previous = null;
next = null;
}
}//end Node class
}//end DoubleLinkedList