-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTravel.java
More file actions
125 lines (96 loc) · 5.14 KB
/
Travel.java
File metadata and controls
125 lines (96 loc) · 5.14 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
/*
Richard Clarke
Salesman's Travel
https://www.codewars.com/kata/salesmans-travel/
************************************************************************************************************************************************************
BACKGROUND
************************************************************************************************************************************************************
A traveling salesman has to visit clients. He got each client's address e.g. "432 Main Long Road St. Louisville OH 43071" as a list.
The basic zipcode format usually consists of two capital letters followed by a white space and five digits. The list of clients to visit was given
as a string of all addresses, each separated from the others by a comma, e.g. :
"123 Main Street St. Louisville OH 43071,432 Main Long Road St. Louisville OH 43071,786 High Street Pollocksville NY 56432".
To ease his travel he wants to group the list by zipcode.
************************************************************************************************************************************************************
TASK
************************************************************************************************************************************************************
The function travel will take two parameters r (addresses' list of all clients' as a string) and zipcode and returns a string in the following format:
zipcode:street and town,street and town,.../house number,house number,...
The street numbers must be in the same order as the streets where they belong.
If a given zipcode doesn't exist in the list of clients' addresses return "zipcode:/"
************************************************************************************************************************************************************
EXAMPLES
************************************************************************************************************************************************************
r = "123 Main Street St. Louisville OH 43071,432 Main Long Road St. Louisville OH 43071,786 High Street Pollocksville NY 56432"
travel(r, "OH 43071") --> "OH 43071:Main Street St. Louisville,Main Long Road St. Louisville/123,432"
travel(r, "NY 56432") --> "NY 56432:High Street Pollocksville/786"
travel(r, "NY 5643") --> "NY 5643:/"
*/
import java.util.ArrayList;
public class Travel {
public static void main (String[] args) {
String addresses = "123 Main Street St. Louisville OH 4307,432 Main Long Road St. Louisville OH 43071,786 High Street Pollocksville OH 43071";
String zipcode = "OH 43071";
travel(addresses, zipcode);
}
public static void travel(String r, String zipcode) {
ArrayList<String> showMe = new ArrayList<>();
String address = "";
;
//Confirm zipcode is of proper length
if (zipcode.length() != 8)
System.out.println(zipcode + ":/");
//Confirm zipcode is in the list r
//Use a do while loop for extracting data and adding to the list of street numbers and street names while the original list contains the zipcode
if (r.contains(zipcode)) {
do{
//See if the address is at the beginning of the list and remove the first address
if (r.indexOf(zipcode) == r.indexOf(",") - 8){
address = getAddress(1, r, zipcode);
r = r.substring(address.length() + 1);
}
//See if the address is at the end of the list and remove the last address
else if ((r.indexOf(zipcode) == r.length() - 8) && (r.charAt(r.length() - 1) != ',')){
address = getAddress(3, r, zipcode);
r = r.substring(0, (r.indexOf(address) - 1));
}
//See if the address is in the middle of the list and remove it
else {
String beforeAddress = "", afterAddress = "";
address = getAddress(2, r, zipcode);
beforeAddress = r.substring(0, (r.indexOf(address) - 1));
afterAddress =r.substring(r.indexOf(address) + address.length());
r = (beforeAddress + afterAddress);
}
//Get the street number and street name of the address
String streetNumber = getStreetNumber(address);
String streetName = address.substring((streetNumber.length() + 1), (address.indexOf(zipcode) - 1));
showMe.add(streetNumber + "\n" + streetName);
}while(r.contains(zipcode));
}
else {
showMe.add("NO RESULTS");
}
System.out.println(showMe.size());
showMe.forEach(addy -> System.out.println(addy + "\n\n"));
}
public static String getAddress (int option, String list, String zipcode){
String address = "";
int commaIndex = 0;
switch (option){
case(1): commaIndex = list.indexOf(",");
address = list.substring(0, commaIndex);
break;
case(2): list = list.substring(0, list.indexOf(zipcode) + 8);
case(3): commaIndex = list.lastIndexOf(",");
address = list.substring(++commaIndex, list.length());
}
return address;
}
public static String getStreetNumber (String address){
String streetNumber = "";
int spaceIndex = 0;
spaceIndex = address.indexOf(" ");
streetNumber = address.substring(0, spaceIndex);
return streetNumber;
}
}