-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraverse.java
More file actions
177 lines (105 loc) · 3.84 KB
/
traverse.java
File metadata and controls
177 lines (105 loc) · 3.84 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
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Scanner;
import java.util.Set;
public class traverse {
public static record Place(String name , int distance){
@Override
public String toString() {
return String.format("%s,(%d)",name,distance);
}
}
private static Scanner scanner;
public static void main(String[] args) {
scanner = new Scanner(System.in);
boolean flag = true;
boolean forward = true;
LinkedList<Place> placesToVisit = new LinkedList<>();
Place adelaide = new Place("Adelaide", 1374);
addPlace(placesToVisit, adelaide);
addPlace(placesToVisit, new Place("adelaide", 1374));
addPlace(placesToVisit, new Place("Brisbane", 917));
addPlace(placesToVisit, new Place("Perth", 3923));
addPlace(placesToVisit, new Place("Alice Springs", 2771));
addPlace(placesToVisit, new Place("Darwin", 3972));
addPlace(placesToVisit, new Place("Melbourne", 877));
placesToVisit.addFirst(new Place("Sydney", 0));
System.out.println(placesToVisit);
ListIterator iterator = placesToVisit.listIterator(0);
menu();
while (flag){
String s = scanner.nextLine().substring(0,1);
if(!iterator.hasNext()){
System.out.println("final stop : " +iterator.previous());
forward = false;
}
if(!iterator.hasPrevious()){
System.out.println(" The first stop : "+ iterator.next());
forward = true;
}
switch (s){
default :
System.out.println("Thank u for your time");
flag = false;
break;
case "F":
if(!forward){
forward= true;
if(iterator.hasNext()){
iterator.next();
}
}
if(iterator.hasNext()){
System.out.println(" The next stop is "+ iterator.next());
}
break;
case "B":
if(forward){
forward = false;
if(iterator.hasPrevious()){
iterator.previous();
}
}
if(iterator.hasPrevious()){
System.out.println(" The previous stop is "+ iterator.previous());
}
break;
case "M":
menu();
break;
}
}
}
public static void addPlace(LinkedList<Place>list,Place place){
if(list.contains(place)){
System.out.println("Duplicate place contained "+ place.name());
return;
}
for(Place p: list){
if(p.name().equalsIgnoreCase(place.name())){
System.out.println("Duplicate place contained "+p.name());
return;
}
}
int i = 0;
for(Place p: list){
if(place.distance()<p.distance()){
list.add(i,place);
return;
}
i++;
}
list.add(place);
}
private static void menu(){
String s = """
Available actions (select word or letter):
(F)orward
(B)ackwards
(L)ist Places
(M)enu
(Q)uit
""";
System.out.println(s);
}
}