package review;
public interface ITour {
String Suggest(String city);
}
package review;
import java.util.*;
import java.io.*;
public class CityTour implements ITour {
HashMap<String,String> tourMap=new HashMap<>();
private String fileName;
public CityTour(String fileName){
this.fileName=fileName;
try(Scanner sc=new Scanner(new FileReader(this.fileName))){
while(sc.hasNextLine()){
String line=sc.nextLine();
String []pair=line.split(":");
tourMap.put(pair[0], pair[1]);
}
}catch(Exception e){
System.out.println("NO SUCH FILE FOUND");
}
}
public String Suggest(String city){
return tourMap.getOrDefault(city, "CITY NOT FOUND");
}
}
package review;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class TestTour extends JFrame {
ITour tourSuggest=new CityTour("D:\\java import\\BlankJavaProject\\cities.txt.txt");
public static void main(String[] args){
new TestTour();
}
JTextField text1=new JTextField(20);
JTextField text2=new JTextField(20);
public TestTour(){
JFrame frame=new JFrame("Urban attraction inquire");
Dimension d=new Dimension();
d.setSize(400,200);
frame.setSize(d);
frame.setBackground(Color.red);
Point p=new Point(100,100);
JPanel jpanel=new JPanel();
JLabel cityLabel=new JLabel("City");
JLabel tourLabel=new JLabel("Tour Attractions");
JButton button=new JButton("OK");
jpanel.setLayout(new BoxLayout(jpanel,BoxLayout.Y_AXIS));
JPanel panel1=new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel1.add(cityLabel);
panel1.add(text1);
panel1.setAlignmentX(Component.CENTER_ALIGNMENT);
jpanel.add(panel1);
JPanel panel2=new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel2.add(tourLabel);
panel2.add(text2);
panel2.setAlignmentX(Component.CENTER_ALIGNMENT);
jpanel.add(panel2);
JPanel panel3=new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel3.add(button);
panel3.setAlignmentX(Component.CENTER_ALIGNMENT);
jpanel.add(panel3);
frame.add(jpanel);
frame.setVisible(true);
frame.setLocation(p);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String city=String.valueOf(text1.getText());
text2.setText(tourSuggest.Suggest(city));
}
});
}
}