City State Locator
You are given following packages which contains some classes and interfacescom.exception : CityNotFoundException, InvalidStateException.
com.util : DataManager, DataManagerImpl.
com.psl : Client.
Read the following problem statement and write a code in the method below to read this file and create an in-memory data structure to maintain the State and City data. Use the above classes to get your results.
1. Data Initialization
In the given java project, CityStateLocator, you will find a StateCityDetails.txt file. The file StateCityDetails.txt has a delimiter '-' which separates the State and the City. In the file you will find, there are multiple occurrences of the same state. For example:Maharashtra - Mumbai
Maharashtra - Nagpur
Maharashtra - Nashik
In such scenarios, there should be a single occurrence of the State as a key and multiple cities should be stored in the list as a value. Method that you should implement for this:
Map<String, List<String>> populateCityDataMap(String fileName)
2. Get Cities
Write a code using the method below to find the list of Cities in the State. In case the State passed to the method is invalid or is not available in the existing data, throw InvalidStateException. Method that you should implement for this:List<String> getCities(Map<String, List<String>> stateCityMap, String state) throws InvalidStateException;
3. Get State
Get the State using the city name passed to the method below. In case the city is invalid or not available in the data given, throw CityNotFoundException. Method that you should implement for this:String getState(Map<String, List<String>> stateCityMap, String city) throws CityNotFoundException;
Download the problem statement : Download problem structure
Solutions
CityNotFoundException.java
package com.exception;
public class CityNotFoundException extends Exception {
public CityNotFoundException() {
}
}
public class CityNotFoundException extends Exception {
public CityNotFoundException() {
}
}
InvalidStateException.java
package com.exception;
public class InvalidStateException extends Exception {
public InvalidStateException() {
}
}
public class InvalidStateException extends Exception {
public InvalidStateException() {
}
}
DataManager.java
package com.util;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;
import com.exception.CityNotFoundException;
import com.exception.InvalidStateException;
public interface DataManager {
Map<String,List<String>> populateCityDataMap(String fileName)throws FileNotFoundException;
List<String> getCities(Map<String,List<String>> stateCityMap, String state) throws InvalidStateException;
String getState(Map<String, List<String>> stateCityMap, String city) throws CityNotFoundException;
}
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;
import com.exception.CityNotFoundException;
import com.exception.InvalidStateException;
public interface DataManager {
Map<String,List<String>> populateCityDataMap(String fileName)throws FileNotFoundException;
List<String> getCities(Map<String,List<String>> stateCityMap, String state) throws InvalidStateException;
String getState(Map<String, List<String>> stateCityMap, String city) throws CityNotFoundException;
}
DataManagerImpl.java
package com.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import com.exception.CityNotFoundException;
import com.exception.InvalidStateException;
public class DataManagerImpl implements DataManager {
@Override
public Map<String, List<String>> populateCityDataMap(String fileName)
throws FileNotFoundException {
// TODO Auto-generated method stub
Map<String, List<String>> cityStateMap = new HashMap<String, List<String>>();
Scanner sc = null;
sc = new Scanner(new File(fileName));
while(sc.hasNext()){
String line = sc.nextLine();
String data[] = line.split("-");
if(cityStateMap.containsKey(data[0].trim())){
cityStateMap.get(data[0].trim()).add(data[1].trim());
} else {
List<String> cityList = new ArrayList<String>();
cityList.add(data[1].trim());
cityStateMap.put(data[0].trim(), cityList);
}
}
return cityStateMap;
}
@Override
public List<String> getCities(Map<String, List<String>> stateCityMap,
String state) throws InvalidStateException {
// TODO Auto-generated method stub
if(stateCityMap.containsKey(state))
return stateCityMap.get(state);
else
throw new InvalidStateException();
}
@Override
public String getState(Map<String, List<String>> stateCityMap, String city)
throws CityNotFoundException {
// TODO Auto-generated method stub
boolean foundflag = false;
Set<String> states = stateCityMap.keySet();
Iterator<String> itr = states.iterator();
String state = null;
while(itr.hasNext()) {
state = itr.next();
List<String> cityList = stateCityMap.get(state);
if(cityList.contains(city)){
foundflag = true;
break;
}
}
if(!foundflag)
throw new CityNotFoundException();
return state;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import com.exception.CityNotFoundException;
import com.exception.InvalidStateException;
public class DataManagerImpl implements DataManager {
@Override
public Map<String, List<String>> populateCityDataMap(String fileName)
throws FileNotFoundException {
// TODO Auto-generated method stub
Map<String, List<String>> cityStateMap = new HashMap<String, List<String>>();
Scanner sc = null;
sc = new Scanner(new File(fileName));
while(sc.hasNext()){
String line = sc.nextLine();
String data[] = line.split("-");
if(cityStateMap.containsKey(data[0].trim())){
cityStateMap.get(data[0].trim()).add(data[1].trim());
} else {
List<String> cityList = new ArrayList<String>();
cityList.add(data[1].trim());
cityStateMap.put(data[0].trim(), cityList);
}
}
return cityStateMap;
}
@Override
public List<String> getCities(Map<String, List<String>> stateCityMap,
String state) throws InvalidStateException {
// TODO Auto-generated method stub
if(stateCityMap.containsKey(state))
return stateCityMap.get(state);
else
throw new InvalidStateException();
}
@Override
public String getState(Map<String, List<String>> stateCityMap, String city)
throws CityNotFoundException {
// TODO Auto-generated method stub
boolean foundflag = false;
Set<String> states = stateCityMap.keySet();
Iterator<String> itr = states.iterator();
String state = null;
while(itr.hasNext()) {
state = itr.next();
List<String> cityList = stateCityMap.get(state);
if(cityList.contains(city)){
foundflag = true;
break;
}
}
if(!foundflag)
throw new CityNotFoundException();
return state;
}
}
Client.java
package com.psl;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;
import com.exception.CityNotFoundException;
import com.exception.InvalidStateException;
import com.util.DataManager;
import com.util.DataManagerImpl;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DataManager demo = new DataManagerImpl();
Map<String, List<String>> stateCityMap = null;
try {
stateCityMap = demo.populateCityDataMap("StateCityDetails.txt");
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println(stateCityMap);
try {
String state = demo.getState(stateCityMap, "Mumbai");
System.out.println("State of Mumbai is : " + state);
} catch (CityNotFoundException e) {
e.printStackTrace();
// TODO: handle exception
}
try {
List<String> cities = demo.getCities(stateCityMap, "Goa");
System.out.println("Cities in Goa " + cities);
} catch (InvalidStateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;
import com.exception.CityNotFoundException;
import com.exception.InvalidStateException;
import com.util.DataManager;
import com.util.DataManagerImpl;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DataManager demo = new DataManagerImpl();
Map<String, List<String>> stateCityMap = null;
try {
stateCityMap = demo.populateCityDataMap("StateCityDetails.txt");
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println(stateCityMap);
try {
String state = demo.getState(stateCityMap, "Mumbai");
System.out.println("State of Mumbai is : " + state);
} catch (CityNotFoundException e) {
e.printStackTrace();
// TODO: handle exception
}
try {
List<String> cities = demo.getCities(stateCityMap, "Goa");
System.out.println("Cities in Goa " + cities);
} catch (InvalidStateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
No comments:
Post a Comment