Advertisement
Guest User

Untitled

a guest
Apr 11th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.49 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' to exit: ");
  25.         String userResponse = scanner.next();
  26.  
  27.         return userResponse;
  28.     }
  29.  
  30.     static Connection getConnection() {
  31.         Connection connection = null;
  32.         try {
  33.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  34.             connection = DriverManager.getConnection("jdbc:mysql://184.154.73.113/davidlno_StockHistory?"
  35.                     + "user=davidlno_student&password=ez24get!&allowMultiQueries=true");
  36.         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  37.             e.printStackTrace();
  38.         } catch (SQLException e) {
  39.             // TODO Auto-generated catch block
  40.             System.out.println("SQLException: " + e.getMessage());
  41.             System.out.println("SQLState: " + e.getSQLState());
  42.             System.out.println("VendorError: " + e.getErrorCode());
  43.         }
  44.         return connection;
  45.     }
  46.  
  47.     static void getAllTickers() {
  48.         Connection connection = getConnection();
  49.         String query = "SELECT GROUP_CONCAT(DISTINCT Ticker SEPARATOR ', ') as Ticker FROM TickerHistory";
  50.  
  51.         try {
  52.             Statement statement = connection.createStatement();
  53.             ResultSet rs = statement.executeQuery(query);
  54.  
  55.             while (rs.next()) {
  56.                 String ticker = rs.getString("Ticker");
  57.                 System.out.print(ticker);
  58.             }
  59.             System.out.println("");
  60.             rs.close();
  61.             statement.close();
  62.             connection.close();
  63.             rs = null;
  64.             statement = null;
  65.             connection = null;
  66.         } catch (SQLException e) {
  67.             System.out.println("SQLException: " + e.getMessage());
  68.             System.out.println("SQLState: " + e.getSQLState());
  69.             System.out.println("VendorError: " + e.getErrorCode());
  70.         }
  71.     }
  72.  
  73.     public static void GetGeneralTickerInfo() {
  74.         Scanner inputTicker = new Scanner(System.in);
  75.         NumberFormat formatter = NumberFormat.getCurrencyInstance();
  76.         NumberFormat formatterII = NumberFormat.getNumberInstance();
  77.  
  78.         System.out.println("Please enter a valid ticker: ");
  79.         String userResponse = inputTicker.nextLine();
  80.  
  81.         Connection connection = getConnection();
  82.         String query = "select Ticker, CompanyName, HistoryDate, OpenPrice, ClosePrice, HighPrice, LowPrice, Volume from TickerHistory where Ticker ='"
  83.                 + userResponse + "' order by HistoryDate desc limit 1";
  84.         try {
  85.             Statement statement = connection.createStatement();
  86.             ResultSet rs = statement.executeQuery(query);
  87.             if (!rs.isBeforeFirst()) {
  88.                 System.out.println("There is no data matching your criteria");
  89.             } else {
  90.                 while (rs.next()) {
  91.                     String ticker = rs.getString("Ticker");
  92.                     String companyName = rs.getString("CompanyName");
  93.                     Date historyDate = rs.getDate("HistoryDate");
  94.                     double openPrice = rs.getDouble("OpenPrice");
  95.                     double closePrice = rs.getDouble("ClosePrice");
  96.                     double highPrice = rs.getDouble("HighPrice");
  97.                     double lowPrice = rs.getDouble("LowPrice");
  98.                     int volume = rs.getInt("Volume");
  99.  
  100.                     System.out.println(ticker + "    " + companyName + "  " + "Volume: " + (formatterII.format(volume))
  101.                             + "\n      " + "History Date: " + historyDate + "\n      " + "Open:  "
  102.                             + (formatter.format(openPrice)) + "\n      " + "High:  " + (formatter.format(highPrice))
  103.                             + "\n      " + "Low:   " + (formatter.format(lowPrice)) + "\n      " + "Close: "
  104.                             + (formatter.format(closePrice)));
  105.                 }
  106.             }
  107.             System.out.println("");
  108.             rs.close();
  109.             statement.close();
  110.             connection.close();
  111.             rs = null;
  112.             statement = null;
  113.             connection = null;
  114.         } catch (SQLException e) {
  115.             System.out.println("SQLException: " + e.getMessage());
  116.             System.out.println("SQLState: " + e.getSQLState());
  117.             System.out.println("VendorError: " + e.getErrorCode());
  118.         }
  119.     }
  120.  
  121.     public static void CheckCurrentHighPrice() {
  122.         Scanner inputTicker = new Scanner(System.in);
  123.         NumberFormat formatter = NumberFormat.getCurrencyInstance();
  124.         NumberFormat formatterII = NumberFormat.getPercentInstance();
  125.         formatterII.setMinimumFractionDigits(0);
  126.  
  127.         System.out.println("Please enter ticker: ");
  128.         String userResponse = inputTicker.nextLine();
  129.  
  130.         Connection connection = getConnection();
  131.         String query = "SELECT HistoryDate as HistoryDateI, ClosePrice, ClosePrice FROM TickerHistory WHERE Ticker ='"
  132.                 + userResponse + "' order by ClosePrice desc limit 1";
  133.  
  134.         String queryII = "SELECT HistoryDate as HistoryDateII, ClosePrice as ClosePriceII FROM TickerHistory WHERE Ticker ='"
  135.                 + userResponse + "' order by HistoryDate desc limit 1";
  136.  
  137.         try {
  138.             Statement statement = connection.createStatement();
  139.             Statement statementII = connection.createStatement();
  140.  
  141.             ResultSet rs = statement.executeQuery(query);
  142.             ResultSet rsII = statementII.executeQuery(queryII);
  143.             if (!rs.isBeforeFirst()) {
  144.                 System.out.println("There is no data matching your criteria");
  145.             } else {
  146.                 while (rs.next() && rsII.next()) {
  147.                     Date historyDate = rs.getDate("HistoryDateI");
  148.                     Date historyDateII = rsII.getDate("HistoryDateII");
  149.                     double closePrice = rs.getDouble("ClosePrice");
  150.                     double closePriceII = rsII.getDouble("ClosePriceII");
  151.                     double decrease = (closePriceII - closePrice);
  152.                     double differencePercentage = (decrease / closePrice) * 100;
  153.  
  154.                     System.out.println(historyDate + "  " + (formatter.format(closePrice)) + "\n" + historyDateII + " "
  155.                             + (formatter.format(closePriceII)) + "\n" + "Perfect Difference is: "
  156.                             + (formatterII.format(differencePercentage / 100)));
  157.                 }
  158.             }
  159.             System.out.println("");
  160.             rs.close();
  161.             rsII.close();
  162.             statement.close();
  163.             statementII.close();
  164.             connection.close();
  165.  
  166.             rs = null;
  167.             rsII = null;
  168.             statement = null;
  169.             statementII = null;
  170.             connection = null;
  171.  
  172.         } catch (SQLException e) {
  173.             System.out.println("SQLException: " + e.getMessage());
  174.             System.out.println("SQLState: " + e.getSQLState());
  175.             System.out.println("VendorError: " + e.getErrorCode());
  176.         }
  177.  
  178.     }
  179.  
  180.     public static void CheckDateTicker() {
  181.         Scanner inputTicker = new Scanner(System.in);
  182.         NumberFormat formatter = NumberFormat.getNumberInstance();
  183.  
  184.         System.out.println("Please enter valid date (yyyy-mm-dd): ");
  185.         String userDate = inputTicker.next();
  186.  
  187.         Connection connection = getConnection();
  188.         String query = "select Ticker, Volume from TickerHistory where HistoryDate ='" + userDate
  189.                 + "' order by Volume desc limit 5";
  190.         try {
  191.             Statement statement = connection.createStatement();
  192.             ResultSet rs = statement.executeQuery(query);
  193.             if (!rs.isBeforeFirst()) {
  194.                 System.out.println("There is no data matching your criteria");
  195.             } else {
  196.  
  197.                 while (rs.next()) {
  198.                     String ticker = rs.getString("Ticker");
  199.                     int volume = rs.getInt("Volume");
  200.                     System.out.println("Ticker:      " + ticker + "    " + (formatter.format(volume)));
  201.                 }
  202.             }
  203.             System.out.println("");
  204.             rs.close();
  205.             statement.close();
  206.             connection.close();
  207.             rs = null;
  208.             statement = null;
  209.             connection = null;
  210.         } catch (SQLException e) {
  211.             System.out.println("SQLException: " + e.getMessage());
  212.             System.out.println("SQLState: " + e.getSQLState());
  213.             System.out.println("VendorError: " + e.getErrorCode());
  214.         }
  215.     }
  216.  
  217.     public static void main(String[] args) {
  218.  
  219.         Scanner input = new Scanner(System.in);
  220.         boolean rerun = false;
  221.  
  222.         do {
  223.             System.out.println("");
  224.             String selection = EnterSelection(input);
  225.  
  226.             switch (selection) {
  227.             case "1":
  228.                 getAllTickers();
  229.                 rerun = true;
  230.                 break;
  231.             case "2":
  232.                 GetGeneralTickerInfo();
  233.                 rerun = true;
  234.                 break;
  235.             case "3":
  236.                 CheckCurrentHighPrice();
  237.                 rerun = true;
  238.                 break;
  239.             case "4":
  240.                 CheckDateTicker();
  241.                 rerun = true;
  242.                 break;
  243.             case "quit":
  244.                 rerun = false;
  245.                 System.out.println("...Application Ended...");
  246.                 return;
  247.             default:
  248.                 System.out.println("Invalid selection");
  249.                 rerun = true;
  250.                 break;
  251.             }
  252.  
  253.         } while (rerun = true);
  254.     }
  255.  
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement