Guest User

Untitled

a guest
May 19th, 2018
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.37 KB | None | 0 0
  1. package sample;
  2.  
  3. //Ashwin Randhir
  4. //5/7/2018
  5. //CSC 112
  6. //Interm Java
  7. //Final
  8. //atrandhir0001@student.stcc.edu
  9. //Gui for Final, with regex field check (P2)
  10.  
  11. import java.sql.*;
  12.  
  13. import javafx.application.Application;
  14. import javafx.geometry.Insets;
  15. import javafx.geometry.Pos;
  16. import javafx.scene.Scene;
  17. import javafx.scene.control.*;
  18. import javafx.scene.layout.*;
  19. import javafx.scene.paint.Color;
  20. import javafx.scene.shape.Line;
  21. import javafx.stage.Stage;
  22.  
  23. public class Main extends Application {
  24.  
  25. private final TextField ZipCodeText = new TextField();
  26. private final TextField MiddleCityText = new TextField();
  27. private final TextField TopCityText = new TextField();
  28. private final TextField TopStateText = new TextField();
  29. private final TextField MiddleStateText = new TextField();
  30. private final Button ZiptoCityButton = new Button("Zip To City");
  31. private final Button CitytoZipButton = new Button("City to Zip");
  32. private final TextField ZipCodeField = new TextField("Zip Code");
  33.  
  34. private PreparedStatement preparedStatement;
  35. Connection conn;
  36.  
  37. @Override
  38. public void start(Stage primaryStage) {
  39.  
  40. StartDatabase();
  41.  
  42.  
  43. //--Build Pane
  44. BorderPane pane = new BorderPane();
  45. pane.setTop(TopRow());
  46. pane.setCenter(CenterRow());
  47. pane.setBottom(BottomRow());
  48.  
  49. //--Lambda
  50. ZiptoCityButton.setOnAction(e -> CheckZiptoCity());
  51. CitytoZipButton.setOnAction(e -> CheckCitytoZip());
  52.  
  53. //--Create scene
  54. Scene scene = new Scene(pane, 800, 300);
  55. primaryStage.setTitle("ZipCode Translation System");
  56. primaryStage.setScene(scene);
  57. primaryStage.show();
  58. primaryStage.setResizable(false);
  59. }
  60.  
  61. //--TextStatus Field
  62. private final TextField StatusText = new TextField();
  63.  
  64.  
  65. //--Button Code
  66. //--Zip to City
  67. private void CheckZiptoCity() {
  68. if (ZipCodeText.getText().matches("(\\d{5})")) {
  69. zipDatabase();
  70. StatusText.setText("Success - Valid ZipCode");
  71. } else {
  72. StatusText.setText("Error - Invalid ZipCode");
  73.  
  74. }
  75. }
  76.  
  77. //--City to Zip
  78. private void CheckCitytoZip() {
  79. if (MiddleCityText.getText().matches("^[a-zA-Z\\s-]+$") && MiddleStateText.getText().matches("^[A-Za-z]{2}")) {
  80. cityDatabase();
  81. StatusText.setText("Success - Valid City / State");
  82. } else {
  83. StatusText.setText("Error - Invalid City / State");
  84.  
  85. }
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92. //--Top Row (Zip to City)
  93. private VBox TopRow() {
  94.  
  95. HBox hbox = new HBox();
  96. hbox.setPadding(new Insets(15, 10, 15, 10));
  97. hbox.setSpacing(15);
  98. hbox.setAlignment(Pos.CENTER);
  99.  
  100. //--VBox
  101. VBox vbox = new VBox();
  102. vbox.setAlignment(Pos.CENTER);
  103.  
  104. //--Line (Divider)
  105. Line separator = new Line();
  106. separator.setStrokeWidth(5);
  107. separator.setStroke(Color.BLACK);
  108. separator.setEndX(800.0);
  109. separator.autosize();
  110.  
  111. //--Labels
  112. Label ZipCode = new Label("Zip Code:");
  113. Label City = new Label("City:");
  114. Label State = new Label("State:");
  115.  
  116. //--Properties
  117. ZipCodeText.setPrefColumnCount(5);
  118. ZipCodeText.setPrefWidth(80);
  119.  
  120. //
  121. TopCityText.setPrefColumnCount(25);
  122. TopCityText.setPrefWidth(200);
  123. TopCityText.setEditable(false);
  124. TopCityText.setMouseTransparent(true);
  125. TopCityText.setFocusTraversable(false);
  126.  
  127. //
  128. TopStateText.setPrefColumnCount(2);
  129. TopStateText.setEditable(false);
  130. TopStateText.setMouseTransparent(true);
  131. TopStateText.setFocusTraversable(false);
  132. TopStateText.setPrefWidth(50);
  133.  
  134. //--Add to hbox
  135. hbox.getChildren().addAll(ZipCode, ZipCodeText, ZiptoCityButton, City, TopCityText, State, TopStateText);
  136. vbox.getChildren().addAll(hbox, separator);
  137. return vbox;
  138. }
  139.  
  140. //--Center of program (City to Zip)
  141. private VBox CenterRow() {
  142.  
  143. HBox hbox = new HBox();
  144. hbox.setPadding(new Insets(15, 10, 15, 10));
  145. hbox.setSpacing(15);
  146. hbox.setAlignment(Pos.CENTER);
  147.  
  148. //--VBox
  149. VBox vbox = new VBox();
  150. vbox.setAlignment(Pos.CENTER);
  151.  
  152. //--Line
  153. Line separator = new Line();
  154. separator.setStrokeWidth(5);
  155. separator.setStroke(Color.BLACK);
  156. separator.setEndX(800.0);
  157. separator.autosize();
  158.  
  159. //--Labels
  160. Label City = new Label("City:");
  161. Label State = new Label("State:");
  162.  
  163. //--Properties
  164. MiddleStateText.setPrefColumnCount(2);
  165. MiddleStateText.setPrefWidth(50);
  166.  
  167. //
  168. ZipCodeField.setPrefColumnCount(5);
  169. ZipCodeField.setEditable(false);
  170. ZipCodeField.setMouseTransparent(true);
  171. ZipCodeField.setFocusTraversable(false);
  172. ZipCodeField.setPrefHeight(100);
  173. ZipCodeField.setPrefWidth(140);
  174.  
  175. //--Add to hbox
  176. hbox.getChildren().addAll(City, MiddleCityText, State, MiddleStateText, CitytoZipButton, ZipCodeField);
  177. vbox.getChildren().addAll(hbox, separator);
  178.  
  179. return vbox;
  180. }
  181.  
  182. //--Status Row
  183. private HBox BottomRow() {
  184.  
  185. HBox hbox = new HBox();
  186. hbox.setPadding(new Insets(15, 10, 15, 10));
  187. hbox.setAlignment(Pos.CENTER);
  188.  
  189. //--Label
  190. Label Status = new Label("Status: ");
  191.  
  192. //--Properties
  193. StatusText.setEditable(false);
  194. StatusText.setMouseTransparent(true);
  195. StatusText.setFocusTraversable(false);
  196. StatusText.setPrefWidth(500);
  197.  
  198. //--Add to hbox
  199. hbox.getChildren().addAll(Status, StatusText);
  200.  
  201. return hbox;
  202. }
  203.  
  204.  
  205. private void StartDatabase() {
  206. String DATABASE = "silvestri";
  207. String USERNAME = "readonly";
  208. String PASSWORD = "readonly";
  209. String driver = "com.mysql.jdbc.Driver";
  210. String url = "jdbc:mysql://cs.stcc.edu/" + DATABASE + "?user=" + USERNAME + "&password=" + PASSWORD;
  211. String zip = ZipCodeText.getText();
  212. String state = MiddleStateText.getText();
  213. String city = MiddleCityText.getText();
  214. try {
  215. Class.forName(driver);
  216. System.out.println("Driver loaded successfully");
  217.  
  218. Connection connection = DriverManager.getConnection(url);
  219. System.out.println("Database Connected");
  220. String zipCode = ("Select city, state FROM Zipcodes WHERE zipcode= " + zip);
  221. String City = ("Select zipcode from Zipcodes where state ='" + state + "' && city ='" + city + "';");
  222.  
  223. preparedStatement = connection.prepareStatement(zipCode);
  224. preparedStatement = connection.prepareStatement(City);
  225. } catch (Exception ex) {
  226. ex.printStackTrace();
  227. }
  228. }
  229.  
  230. //ZipCode to Database
  231. private void zipDatabase() {
  232. try {
  233. String zip = ZipCodeText.getText();
  234. String zipCode = ("Select city, state FROM Zipcodes WHERE zipcode= " + zip);
  235. ResultSet rset = preparedStatement.executeQuery(zipCode);
  236. if (rset.next()) {
  237. TopCityText.setText(rset.getString(1));
  238. TopStateText.setText(rset.getString(2));
  239. StatusText.setText("City and State were retrieved from Database");
  240. } else {
  241. StatusText.setText("Error Database Connection Fail");
  242. }
  243. } catch (SQLException ex) {
  244. ex.printStackTrace();
  245. }
  246. }
  247.  
  248. //City & State to Database
  249. private void cityDatabase() {
  250. try {
  251. String state = MiddleStateText.getText();
  252. String city = MiddleCityText.getText();
  253. String City = ("Select zipcode from Zipcodes where state ='" + state + "' && city ='" + city + "';");
  254. ResultSet rset = preparedStatement.executeQuery(City);
  255. if (rset.next()) {
  256. String zipCode = rset.getString("zipcode");
  257. ZipCodeField.setText(zipCode + ", " + System.lineSeparator());
  258. while (rset.next()) {
  259. zipCode = rset.getString("zipcode");
  260. ZipCodeField.setText(ZipCodeField.getText() + zipCode + ", " + System.lineSeparator());}
  261. } else {
  262. StatusText.setText("Error Invalid City or State");
  263. ZipCodeField.setText("No Zip Codes Found");
  264. }
  265. } catch (Exception ex) {
  266. ex.getMessage();
  267. }
  268. }
  269. public static void main(String[] args) {
  270. launch(args);
  271. }
  272. }
Add Comment
Please, Sign In to add comment