Beer Mapping API

import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.EmptyBorder; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableColumn; import javax.swing.JLabel; import javax.swing.JComboBox; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.awt.event.ActionEvent; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.Arrays; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class FindMeBeer extends JFrame { private JPanel contentPane; private JTextField txtZipCode; private JTable table; private JScrollPane scrollPane; private JLabel lblLoading; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { FindMeBeer frame = new FindMeBeer(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public FindMeBeer() { setTitle("Find Me Beer"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 780, 550); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new FlowLayout()); JLabel lblNewLabel = new JLabel("What state do you live in?"); lblNewLabel.setBounds(6, 6, 174, 16); contentPane.add(lblNewLabel); JComboBox<String> comboStates = new JComboBox<String>(); comboStates.setBounds(169, 2, 81, 27); contentPane.add(comboStates); comboStates.addItem("AL"); comboStates.addItem("AK"); comboStates.addItem("AZ"); comboStates.addItem("AR"); comboStates.addItem("CA"); comboStates.addItem("CO"); comboStates.addItem("CT"); comboStates.addItem("DE"); comboStates.addItem("FL"); comboStates.addItem("GA"); comboStates.addItem("HI"); comboStates.addItem("ID"); comboStates.addItem("IL"); comboStates.addItem("IN"); comboStates.addItem("IA"); comboStates.addItem("KS"); comboStates.addItem("KY"); comboStates.addItem("LA"); comboStates.addItem("ME"); comboStates.addItem("MD"); comboStates.addItem("MA"); comboStates.addItem("MI"); comboStates.addItem("MN"); comboStates.addItem("MS"); comboStates.addItem("MO"); comboStates.addItem("MT"); comboStates.addItem("NE"); comboStates.addItem("NV"); comboStates.addItem("NH"); comboStates.addItem("NJ"); comboStates.addItem("NM"); comboStates.addItem("NY"); comboStates.addItem("NC"); comboStates.addItem("ND"); comboStates.addItem("OH"); comboStates.addItem("OK"); comboStates.addItem("OR"); comboStates.addItem("PA"); comboStates.addItem("RI"); comboStates.addItem("SC"); comboStates.addItem("SD"); comboStates.addItem("TN"); comboStates.addItem("TX"); comboStates.addItem("UT"); comboStates.addItem("VT"); comboStates.addItem("VA"); comboStates.addItem("WA"); comboStates.addItem("WV"); comboStates.addItem("WI"); comboStates.addItem("WY"); JLabel lblWhatsYourZipcode = new JLabel("What's your 5 digit zip code?"); lblWhatsYourZipcode.setBounds(269, 6, 187, 16); contentPane.add(lblWhatsYourZipcode); txtZipCode = new JTextField(); txtZipCode.setBounds(456, 1, 81, 26); contentPane.add(txtZipCode); txtZipCode.setColumns(10); JButton btnFindBreweries = new JButton("Find Breweries"); btnFindBreweries.addActionListener(new ActionListener() { // BUTTON Click public void actionPerformed(ActionEvent e) { String state = comboStates.getSelectedItem().toString(); String zipCodeString = txtZipCode.getText(); int userZipCode = Integer.parseInt(zipCodeString); try { if(table != null) { //contentPane.removeAll(); contentPane.remove(scrollPane); contentPane.remove(table); contentPane.revalidate(); contentPane.repaint(); } apiConnection(state); } catch (Exception e1) { e1.printStackTrace(); } } }); btnFindBreweries.setBounds(6, 34, 117, 29); contentPane.add(btnFindBreweries); lblLoading = new JLabel("Loading..."); contentPane.add(lblLoading); lblLoading.setVisible(false); } public void apiConnection(String userState) throws Exception { lblLoading.setVisible(true); //String key = "insert API key here"; String url = "http://beermapping.com/webservice/locstate/" + key + "/" + userState + "&s=json"; ///////////////////////////// Make Connection to API ////////////////////////// URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("GET"); // add request header con.setRequestProperty("User-Agent", "Mozilla/5.0"); int responseCode = con.getResponseCode(); System.out.println("Sending 'GET' request to URL : " + url); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); /////////////////////////// Finished Connection to API ///////////////////////// filterJson(response, userState); } public void filterJson(StringBuffer response, String state) throws JSONException { JSONArray myResponse = new JSONArray(response.toString()); String[] names = new String[myResponse.length()]; String[] statuses = new String[myResponse.length()]; String[] zipCodes = new String[myResponse.length()]; String[] cities = new String[myResponse.length()]; String[] websites = new String[myResponse.length()]; int counter = 0; for(int i = 0; i < myResponse.length(); i++) { JSONObject element = myResponse.getJSONObject(i); if(element.getString("status").equals("Brewery") || element.getString("status").equals("Brewpub")) { names[counter] = element.getString("name"); statuses[counter] = element.getString("status"); zipCodes[counter] = element.getString("zip"); cities[counter] = element.getString("city"); websites[counter] = element.getString("phone"); counter++; } } names = Arrays.stream(names) .filter(s -> (s != null)) .toArray(String[]::new); zipCodes = Arrays.stream(zipCodes) .filter(s -> (s != null)) .toArray(String[]::new); cities = Arrays.stream(cities) .filter(s -> (s != null)) .toArray(String[]::new); websites = Arrays.stream(websites) .filter(s -> (s != null)) .toArray(String[]::new); statuses = Arrays.stream(statuses) .filter(s -> (s != null)) .toArray(String[]::new); int[] zips = new int[zipCodes.length]; for(int i = 0; i < zipCodes.length; i++) { if(zipCodes[i].equals("")) { zips[i] = 0; } else { zips[i] = Integer.parseInt(zipCodes[i].substring(0,5)); } } nearbyBrews(Integer.parseInt(txtZipCode.getText()), names, zips, cities, websites, statuses); } public void nearbyBrews(int zipCode, String[] names, int[] zips, String[] cities, String[] websites, String[] statuses) { int lowBound = (zipCode/100) * 100; int highBound = lowBound + 99; String[] closeNames = new String[names.length]; int[] closeZips = new int[names.length]; String[] closeCities = new String[names.length]; String[] closeWebsites = new String[names.length]; String[] closeStatuses = new String[names.length]; int count = 0; for(int i = 0; i < names.length; i++) { if(zips[i] <= highBound && zips[i] >= lowBound) { closeNames[count] = names[i]; closeZips[count] = zips[i]; closeCities[count] = cities[i]; closeWebsites[count] = websites[i]; closeStatuses[count] = statuses[i]; count++; } } String[] closeZipsString = Arrays.stream(closeZips) .mapToObj(String::valueOf) .filter(s -> (s != null && !"0".equals(s))) .toArray(String[]::new); closeNames = Arrays.stream(closeNames) .filter(s -> (s != null && s.length() > 0)) .toArray(String[]::new); closeCities = Arrays.stream(closeCities) .filter(s -> (s != null && s.length() > 0)) .toArray(String[]::new); closeWebsites = Arrays.stream(closeWebsites) .filter(s -> (s != null)) .toArray(String[]::new); closeStatuses = Arrays.stream(closeStatuses) .filter(s -> (s != null && s.length() > 0)) .toArray(String[]::new); lblLoading.setText("Showing results for " + closeNames.length + " breweries in your area..."); populateJTable(closeNames.length, closeNames, closeCities, closeWebsites, closeStatuses); } public void populateJTable(int resultCount, String[] closeNames, String[] closeCities, String[] closeWebsites, String[] closeStatuses) { String[] columnTitles = {"Brewery", "City", "phone #", "Status"}; String[][] data = new String[resultCount][columnTitles.length]; for(int i = 0; i < closeNames.length; i++) { String tempBrewery = closeNames[i]; String tempCity = closeCities[i]; String tempWebsite = closeWebsites[i]; String tempStatus = closeStatuses[i]; data[i][0] = tempBrewery; data[i][1] = tempCity; data[i][2] = tempWebsite; data[i][3] = tempStatus; } table = new JTable(data, columnTitles) { public boolean isCellEditable(int row, int column) { return false; } }; table.setPreferredScrollableViewportSize(new Dimension(750, 400)); table.setFillsViewportHeight(true); TableColumn column1 = table.getColumnModel().getColumn(0); column1.setPreferredWidth(180); TableColumn column2 = table.getColumnModel().getColumn(1); column2.setPreferredWidth(30); TableColumn column3 = table.getColumnModel().getColumn(2); column3.setPreferredWidth(30); TableColumn column4 = table.getColumnModel().getColumn(3); column4.setPreferredWidth(30); scrollPane = new JScrollPane(table); scrollPane.setVisible(true); contentPane.add(scrollPane); contentPane.revalidate(); contentPane.repaint(); } }
1