Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.22 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.Date;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.text.NumberFormat;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Scanner;
  11.  
  12. public class Database_Reader {
  13.  
  14.     private static String EnterSelection(Scanner scanner) {
  15.         System.out.println("--------------Stock Reporting Menu--------------");
  16.         System.out.println("MENU OPTIONS:");
  17.         System.out.println("");
  18.         System.out.println("    1. List all tickers");
  19.         System.out.println("    2. Display most recent data for ticker");
  20.         System.out.println("    3. Display high and current close price for ticker");
  21.         System.out.println("    4. Display top five active tickers for date");
  22.         System.out.println("");
  23.  
  24.         System.out.println("Select number or type quit: ");
  25.         String userResponse = scanner.next();
  26.  
  27.         return userResponse;
  28.     }
  29.  
  30.     static Connection getConnection() {
  31.  
  32.         Connection connection = null;
  33.  
  34.         try {
  35.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  36.             connection = DriverManager.getConnection("jdbc:mysql://184.154.73.113/davidlno_StockHistory?"
  37.                     + "user=davidlno_student&password=ez24get!&allowMultiQueries=true");
  38.         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  39.             e.printStackTrace();
  40.         } catch (SQLException e) {
  41.             // TODO Auto-generated catch block
  42.             System.out.println("SQLException: " + e.getMessage());
  43.             System.out.println("SQLState: " + e.getSQLState());
  44.             System.out.println("VendorError: " + e.getErrorCode());
  45.         }
  46.  
  47.         return connection;
  48.     }
  49.  
  50.     static Connection getConnectionII() {
  51.  
  52.         Connection connection = null;
  53.  
  54.         try {
  55.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  56.             connection = DriverManager.getConnection("jdbc:mysql://184.154.73.113/davidlno_StockHistory?"
  57.                     + "user=davidlno_student&password=ez24get!&allowMultiQueries=true");
  58.         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  59.             e.printStackTrace();
  60.         } catch (SQLException e) {
  61.             // TODO Auto-generated catch block
  62.             System.out.println("SQLException: " + e.getMessage());
  63.             System.out.println("SQLState: " + e.getSQLState());
  64.             System.out.println("VendorError: " + e.getErrorCode());
  65.         }
  66.  
  67.         return connection;
  68.     }
  69.  
  70.     static void getAllTickers() {
  71.         Connection connection = getConnection();
  72.         String query = "SELECT GROUP_CONCAT(DISTINCT Ticker SEPARATOR ', ') as Ticker FROM TickerHistory";
  73.  
  74.         try {
  75.             Statement statement = connection.createStatement();
  76.             ResultSet rs = statement.executeQuery(query);
  77.  
  78.             while (rs.next()) {
  79.  
  80.                 String ticker = rs.getString("Ticker");
  81.                 System.out.print(ticker);
  82.  
  83.             }
  84.             System.out.println("");
  85.  
  86.             rs.close();
  87.             statement.close();
  88.             connection.close();
  89.  
  90.             rs = null;
  91.             statement = null;
  92.             connection = null;
  93.  
  94.         } catch (SQLException e) {
  95.             System.out.println("SQLException: " + e.getMessage());
  96.             System.out.println("SQLState: " + e.getSQLState());
  97.             System.out.println("VendorError: " + e.getErrorCode());
  98.         }
  99.  
  100.     }
  101.  
  102.     public static void EnterTicker() {
  103.         Scanner inputTicker = new Scanner(System.in);
  104.         NumberFormat formatter = NumberFormat.getCurrencyInstance();
  105.         NumberFormat formatterII = NumberFormat.getNumberInstance();
  106.  
  107.         System.out.println("Please enter a valid ticker: ");
  108.         String userResponse = inputTicker.nextLine();
  109.  
  110.         Connection connection = getConnection();
  111.         String query = "select Ticker, CompanyName, HistoryDate, OpenPrice, ClosePrice, HighPrice, LowPrice, Volume from TickerHistory where Ticker ='"
  112.                 + userResponse + "' order by HistoryDate desc limit 1";
  113.         try {
  114.             Statement statement = connection.createStatement();
  115.             ResultSet rs = statement.executeQuery(query);
  116.             if (!rs.isBeforeFirst()) {
  117.                 System.out.println("Invalid Ticker Entered");
  118.             } else {
  119.                 while (rs.next()) {
  120.                     String ticker = rs.getString("Ticker");
  121.                     String companyName = rs.getString("CompanyName");
  122.                     Date historyDate = rs.getDate("HistoryDate");
  123.                     double openPrice = rs.getDouble("OpenPrice");
  124.                     double closePrice = rs.getDouble("ClosePrice");
  125.                     double highPrice = rs.getDouble("HighPrice");
  126.                     double lowPrice = rs.getDouble("LowPrice");
  127.                     int volume = rs.getInt("Volume");
  128.  
  129.                     System.out.println(ticker + "    " + companyName + "  " + "Volume: " + (formatterII.format(volume))
  130.                             + "\n      " + "History Date: " + historyDate + "\n      " + "Open:  "
  131.                             + (formatter.format(openPrice)) + "\n      " + "High:  " + (formatter.format(highPrice))
  132.                             + "\n      " + "Low:   " + (formatter.format(lowPrice)) + "\n      " + "Close: "
  133.                             + (formatter.format(closePrice)));
  134.  
  135.                 }
  136.             }
  137.             System.out.println("");
  138.             rs.close();
  139.             statement.close();
  140.             connection.close();
  141.  
  142.             rs = null;
  143.             statement = null;
  144.             connection = null;
  145.         } catch (SQLException e) {
  146.             System.out.println("SQLException: " + e.getMessage());
  147.             System.out.println("SQLState: " + e.getSQLState());
  148.             System.out.println("VendorError: " + e.getErrorCode());
  149.         }
  150.  
  151.     }
  152.  
  153.     public static void EnterTickerII() {
  154.         Scanner inputTicker = new Scanner(System.in);
  155.         NumberFormat formatter = NumberFormat.getCurrencyInstance();
  156.         NumberFormat formatterII = NumberFormat.getPercentInstance();
  157.         formatterII.setMinimumFractionDigits(0);
  158.  
  159.         System.out.println("Please enter ticker: ");
  160.         String userResponse = inputTicker.nextLine();
  161.  
  162.         Connection connection = getConnection();
  163.         String query = "SELECT HistoryDate as HistoryDateI, ClosePrice, ClosePrice FROM TickerHistory WHERE Ticker ='"
  164.                 + userResponse + "' order by ClosePrice desc limit 1";
  165.  
  166.         Connection connectionII = getConnectionII();
  167.         String queryII = "SELECT HistoryDate as HistoryDateII, ClosePrice as ClosePriceII FROM TickerHistory WHERE Ticker ='"
  168.                 + userResponse + "' order by HistoryDate desc limit 1";
  169.  
  170.         try {
  171.             Statement statement = connection.createStatement();
  172.             Statement statementII = connectionII.createStatement();
  173.  
  174.             ResultSet rs = statement.executeQuery(query);
  175.             ResultSet rsII = statementII.executeQuery(queryII);
  176.             if (!rs.isBeforeFirst()) {
  177.                 System.out.println("Invalid Ticker Entered");
  178.             } else {
  179.                 while (rs.next() && rsII.next()) {
  180.                     Date historyDate = rs.getDate("HistoryDateI");
  181.                     Date historyDateII = rsII.getDate("HistoryDateII");
  182.                     double closePrice = rs.getDouble("ClosePrice");
  183.                     double closePriceII = rsII.getDouble("ClosePriceII");
  184.                     double decrease = (closePriceII - closePrice);
  185.                     double differencePercentage = (decrease / closePrice) * 100;
  186.  
  187.                     System.out.println(historyDate + "  " + (formatter.format(closePrice)) + "\n" + historyDateII + " "
  188.                             + (formatter.format(closePriceII)) + "\n" + "Perfect Difference is: "
  189.                             + (formatterII.format(differencePercentage / 100)));
  190.                 }
  191.             }
  192.             System.out.println("");
  193.  
  194.             rs.close();
  195.             rsII.close();
  196.             statement.close();
  197.             statementII.close();
  198.             connection.close();
  199.             connectionII.close();
  200.  
  201.             rs = null;
  202.             statement = null;
  203.             connection = null;
  204.  
  205.             rsII = null;
  206.             statementII = null;
  207.             connectionII = null;
  208.  
  209.         } catch (SQLException e) {
  210.             System.out.println("SQLException: " + e.getMessage());
  211.             System.out.println("SQLState: " + e.getSQLState());
  212.             System.out.println("VendorError: " + e.getErrorCode());
  213.         }
  214.  
  215.     }
  216.  
  217.     public static void EnterTickerIII() {
  218.         Scanner inputTicker = new Scanner(System.in);
  219.         NumberFormat formatter = NumberFormat.getNumberInstance();
  220.  
  221.         System.out.println("Please enter valid date (yyyy-mm-dd): ");
  222.         String userDate = inputTicker.next();
  223.  
  224.         Connection connection = getConnection();
  225.         String query = "select Ticker, Volume from TickerHistory where HistoryDate ='" + userDate
  226.                 + "' order by Volume desc limit 5";
  227.  
  228.         try {
  229.             Statement statement = connection.createStatement();
  230.             ResultSet rs = statement.executeQuery(query);
  231.             if (!rs.isBeforeFirst()) {
  232.                 System.out.println("Invalid date entered");
  233.             } else {
  234.  
  235.                 while (rs.next()) {
  236.                     String ticker = rs.getString("Ticker");
  237.                     int volume = rs.getInt("Volume");
  238.                     System.out.println("Ticker:      " + ticker + "    " + (formatter.format(volume)));
  239.                 }
  240.             }
  241.             System.out.println("");
  242.             rs.close();
  243.             statement.close();
  244.             connection.close();
  245.  
  246.             rs = null;
  247.             statement = null;
  248.             connection = null;
  249.         } catch (SQLException e) {
  250.             System.out.println("SQLException: " + e.getMessage());
  251.             System.out.println("SQLState: " + e.getSQLState());
  252.             System.out.println("VendorError: " + e.getErrorCode());
  253.         }
  254.  
  255.     }
  256.  
  257.     public static void main(String[] args) {
  258.  
  259.         Scanner input = new Scanner(System.in);
  260.         boolean rerun = false;
  261.  
  262.         do {
  263.             System.out.println("");
  264.             String selection = EnterSelection(input);
  265.  
  266.             switch (selection) {
  267.  
  268.             case "1":
  269.                 getAllTickers();
  270.                 rerun = true;
  271.                 break;
  272.             case "2":
  273.                 EnterTicker();
  274.                 rerun = true;
  275.                 break;
  276.             case "3":
  277.                 EnterTickerII();
  278.                 rerun = true;
  279.                 break;
  280.             case "4":
  281.                 EnterTickerIII();
  282.                 rerun = true;
  283.                 break;
  284.             case "quit":
  285.                 rerun = false;
  286.                 System.out.println("...Application Ended...");
  287.                 return;
  288.             default:
  289.                 System.out.println("Invalid selection");
  290.                 rerun = true;
  291.                 break;
  292.             }
  293.  
  294.         } while (rerun = true);
  295.     }
  296.  
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement