Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 29.02 KB | None | 0 0
  1. // To compile:
  2. // javac -cp lib\mysql-selector-java.jar FaultReport.java
  3. // set CLASSPATH=D:\Koulu\ot01\harjoitustyö\src\tervo_juho;D:\Koulu\ot01\harjoitustyö\src\tervo_juho\lib\mysql-connector-java.jar
  4.  
  5. import java.sql.*;
  6. import java.util.Scanner;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9.  
  10. /**
  11.  * @author      Tervo Juho <juho.tervo@ee.tamk.fi>
  12.  * @version     2011-05-07
  13.  * @since       1.0
  14.  */
  15.  
  16.  class FaultReport {
  17.  
  18.     public static Scanner cin = new Scanner(System.in);
  19.    
  20.     public static String driverName = "com.mysql.jdbc.Driver";
  21.     public static String url = "jdbc:mysql://mydb.tamk.fi/dbt8jtervo1"
  22.                              + "?user=t8jtervo&password=Nimrod666";
  23.                              
  24.     public static void main (String[] args) {
  25.  
  26.         boolean personCheck = false;
  27.         boolean isAdmin = false;
  28.         int personId = 0;
  29.         int chooser;
  30.        
  31.         System.out.println("Welcome to Testing House!");
  32.         System.out.println("This program is intended for posting, receiving and reviewing fault reports.");
  33.        
  34.         System.out.println();
  35.        
  36.         System.out.println("What is your id number?");
  37.        
  38.         do {
  39.        
  40.             personId = getInt("What is your id number?");
  41.             personCheck = checkId(personId);
  42.            
  43.             if (!personCheck) {
  44.            
  45.                 System.out.println("No person with your id number were found in our database!");
  46.                
  47.             }
  48.        
  49.         }
  50.         while (!personCheck);
  51.        
  52.         do {
  53.            
  54.             System.out.println("What would you like to do?");
  55.             System.out.println("1: Post fault report.");
  56.             System.out.println("2: Receive fault reports. (For system operators only!)");
  57.             System.out.println("3: View faultreport statistics.");
  58.             System.out.println("0: Exit program.");
  59.             System.out.println("Please choose number between 0 - 3.");
  60.            
  61.             chooser = getInt("Please choose number between 0 - 3.");
  62.            
  63.             if ( chooser == 1 ) {
  64.            
  65.                 postReport(personId);
  66.                
  67.             }
  68.            
  69.             else if ( chooser == 2 ) {
  70.            
  71.                 isAdmin = operatorCheck(personId);
  72.                
  73.                 if (isAdmin) {
  74.                
  75.                     receiveReport(personId);
  76.  
  77.                 }
  78.                
  79.                 else {
  80.                
  81.                     System.out.println("You don't have privliges to use this feature!");
  82.                
  83.                 }
  84.                
  85.             }
  86.            
  87.             else if ( chooser == 3 ) {
  88.            
  89.                 viewStatistics();
  90.            
  91.             }
  92.            
  93.             else if ( chooser == 0 ) {
  94.            
  95.                 System.out.println("Thank you for using this program!");
  96.            
  97.             }
  98.            
  99.             else {
  100.            
  101.                 System.out.println("Invalid input!");
  102.                
  103.             }      
  104.            
  105.         }
  106.         while (chooser != 0);
  107.        
  108.     }
  109.    
  110.     public static void postReport(int senderId) {
  111.    
  112.         int classNumber = 1, priorityNumber = 1;
  113.         int faultId = getId();
  114.        
  115.         boolean descriptionOk = false;
  116.        
  117.         String faultClass = " ", faultPriority = " ", faultDescription = " ";
  118.         String faultTime = " ";
  119.         String faultSender = getName(senderId);
  120.    
  121.         System.out.println("What kind of problem you have?");
  122.         System.out.println("1: hardware");
  123.         System.out.println("2: software");
  124.         System.out.println("3: network");
  125.         System.out.println("4: other");
  126.        
  127.         do {
  128.        
  129.             System.out.println("Please choose number between 1 - 4");
  130.             classNumber = getInt("Please choose number between 1 - 4");
  131.            
  132.             switch (classNumber) {
  133.                
  134.                 case 1: faultClass = "hardware";
  135.                     break;
  136.                 case 2: faultClass = "software";
  137.                     break;
  138.                 case 3: faultClass = "network";
  139.                     break;
  140.                 case 4: faultClass = "other";
  141.                     break;
  142.                 default:
  143.                     break;
  144.            
  145.             }
  146.        
  147.         }
  148.         while (classNumber > 4 || classNumber < 1);
  149.        
  150.         System.out.println("What priority is your problem?");
  151.         System.out.println("1: wishlist");
  152.         System.out.println("2: low");
  153.         System.out.println("3: medium");
  154.         System.out.println("4: high");
  155.         System.out.println("5: urgent");
  156.        
  157.         do {
  158.        
  159.             System.out.println("Please choose number between 1 - 5");
  160.             classNumber = getInt("Please choose number between 1 - 5");
  161.            
  162.             switch (classNumber) {
  163.                
  164.                 case 1: faultPriority = "wishlist";
  165.                     break;
  166.                 case 2: faultPriority = "low";
  167.                     break;
  168.                 case 3: faultPriority = "medium";
  169.                     break;
  170.                 case 4: faultPriority = "high";
  171.                     break;
  172.                 case 5: faultPriority = "urgent";
  173.                     break;
  174.                 default:
  175.                     break;
  176.            
  177.             }
  178.        
  179.         }
  180.         while (priorityNumber > 4 || priorityNumber < 1);
  181.        
  182.         do {
  183.        
  184.             System.out.println("Short description about your problem: (1000 characters or less)");
  185.             faultDescription = getString("Short description about your problem: (1000 characters or less)");
  186.            
  187.             if (faultDescription.length() < 1000) {
  188.            
  189.                 descriptionOk = true;
  190.                
  191.             }
  192.            
  193.             else {
  194.            
  195.                 System.out.println("Your description is too long!");
  196.                 descriptionOk = false;
  197.                
  198.             }      
  199.            
  200.         }
  201.         while (!descriptionOk);
  202.        
  203.         faultTime = getTimestamp();
  204.                
  205.         insertReport(senderId, faultClass, faultPriority, faultDescription, faultTime, faultId);
  206.    
  207.     }
  208.    
  209.     public static void insertReport(int senderId, String faultClass, String faultPriority,
  210.                                     String faultDescription, String faultTime, int faultId) {
  211.        
  212.         try {
  213.        
  214.             Class.forName(driverName);
  215.  
  216.             Connection con = DriverManager.getConnection(url);
  217.  
  218.             String sql = ""                                     // 3
  219.                 + "INSERT INTO report "
  220.                 + "  ( id, class, description, fix, priority, status, startTime, updateTime, endTime, senderId, handlerId ) "
  221.                 + "VALUES "
  222.                 + "  ( " + faultId + " "
  223.                 + "    , '" + faultClass + "' "
  224.                 + "    , '" + faultDescription + "' "
  225.                 + "    , NULL "
  226.                 + "    , '" + faultPriority + "' "
  227.                 + "    , 'open' "
  228.                 + "    , '" + faultTime + "' "
  229.                 + "    , NULL "
  230.                 + "    , NULL "
  231.                 + "    , " + senderId + " "
  232.                 + "    , NULL "
  233.                 + "  ) "
  234.                 ;
  235.                        
  236.             Statement stmt = con.createStatement();
  237.  
  238.             int count = stmt.executeUpdate(sql);                
  239.  
  240.             stmt.close();
  241.             con.close();
  242.         }
  243.         catch (Exception error) {
  244.             System.err.println("Exception: " + error.getMessage() );
  245.         }
  246.    
  247.     }
  248.    
  249.     public static void receiveReport(int updaterId) {
  250.    
  251.         int chooser = 0;
  252.        
  253.         receiveReportMenu();
  254.        
  255.         do {
  256.        
  257.             chooser = getInt("Please choose number between 0 - 4");
  258.            
  259.             if (chooser == 1) {
  260.            
  261.                 openReport(updaterId);
  262.                 receiveReportMenu();
  263.                
  264.             }
  265.            
  266.             else if (chooser == 2) {
  267.            
  268.                 listReports("open");
  269.                 receiveReportMenu();
  270.                
  271.             }
  272.            
  273.             else if (chooser == 3) {
  274.            
  275.                 listReports("investigating");
  276.                 receiveReportMenu();
  277.                
  278.             }
  279.            
  280.             else if (chooser == 4) {
  281.            
  282.                 listReports("fixed");
  283.                 receiveReportMenu();
  284.            
  285.             }
  286.                
  287.             else if (chooser == 0) {
  288.            
  289.                 System.out.println("\nExiting to previous menu...\n");
  290.                
  291.             }  
  292.            
  293.             else {
  294.            
  295.                 System.out.println("Invalid input!");
  296.                
  297.             }
  298.        
  299.         }
  300.         while (chooser != 0);
  301.    
  302.     }
  303.    
  304.     public static void receiveReportMenu() {
  305.    
  306.         System.out.println("\nWhat would you like to do?");
  307.         System.out.println("1: open report");
  308.         System.out.println("2: list open reports");
  309.         System.out.println("3: list reports under investigation");
  310.         System.out.println("4: list fixed reports");
  311.         System.out.println("0: exit");
  312.         System.out.println("Please choose number between 0 - 4\n");
  313.    
  314.     }
  315.    
  316.     public static void openReport(int updaterId) {
  317.    
  318.         String updatetPriority = " ";
  319.         String faultFix = " ";
  320.    
  321.         boolean idChooser = false;
  322.         boolean fixOk = false;
  323.         int reportId = 0;
  324.         int actionChooser = 0;
  325.         int priorityNumber = 1;
  326.    
  327.         do {
  328.        
  329.             System.out.println("Give id-number of the wanted report:");
  330.             reportId = getInt("Give id-number of the wanted report:");
  331.            
  332.             idChooser = reportIdChecker(reportId);
  333.            
  334.             if(!idChooser) {
  335.            
  336.                 System.out.println("\nThere is no open of investigated report with given id-number!\n");
  337.                
  338.             }
  339.        
  340.         }
  341.         while (!idChooser);
  342.        
  343.         viewReport(reportId);
  344.        
  345.         do {
  346.        
  347.             actionChooser = openReportMenu();
  348.        
  349.             if (actionChooser == 1) {
  350.        
  351.                 System.out.println("New priority for report:");
  352.                 System.out.println("1: wishlist");
  353.                 System.out.println("2: low");
  354.                 System.out.println("3: medium");
  355.                 System.out.println("4: high");
  356.                 System.out.println("5: urgent");
  357.                
  358.                 do {
  359.                
  360.                     System.out.println("Please choose number between 1 - 5");
  361.                     priorityNumber = getInt("Please choose number between 1 - 5");
  362.                    
  363.                     switch (priorityNumber) {
  364.                        
  365.                         case 1: updatetPriority = "wishlist";
  366.                             break;
  367.                         case 2: updatetPriority = "low";
  368.                             break;
  369.                         case 3: updatetPriority = "medium";
  370.                             break;
  371.                         case 4: updatetPriority = "high";
  372.                             break;
  373.                         case 5: updatetPriority = "urgent";
  374.                             break;
  375.                         default:
  376.                             break;
  377.                    
  378.                     }
  379.                
  380.                 }
  381.                 while (priorityNumber > 4 || priorityNumber < 1);
  382.            
  383.                 updateReport(reportId, updatetPriority, updaterId);
  384.                
  385.                 actionChooser = openReportMenu();
  386.            
  387.             }
  388.            
  389.             else if (actionChooser == 2) {
  390.            
  391.                 do {
  392.                
  393.                     System.out.println("Short fix report: (1000 characters or less)");
  394.                     faultFix = getString("Short fix report: (1000 characters or less)");
  395.                    
  396.                     if (faultFix.length() < 1000) {
  397.                    
  398.                         fixOk = true;
  399.                        
  400.                     }
  401.                    
  402.                     else {
  403.                    
  404.                         System.out.println("Your description is too long!");
  405.                         fixOk = false;
  406.                        
  407.                     }      
  408.                    
  409.                 }
  410.                 while (!fixOk);
  411.            
  412.                 fixReport(reportId, faultFix, updaterId);
  413.                
  414.                 System.out.println("\nExiting to previous menu...\n");
  415.                
  416.                 actionChooser = 0;
  417.                
  418.             }
  419.            
  420.             else if (actionChooser == 3) {
  421.            
  422.                 viewReport(reportId);
  423.                
  424.                 actionChooser = openReportMenu();
  425.            
  426.             }
  427.            
  428.             else if (actionChooser == 0) {}
  429.            
  430.             else {
  431.            
  432.                 System.out.println("Invalid input!");
  433.            
  434.             }
  435.        
  436.         }
  437.         while (actionChooser != 0);
  438.    
  439.     }
  440.    
  441.     public static int openReportMenu() {
  442.    
  443.         System.out.println("What do you want to do?");
  444.         System.out.println("1: update report");
  445.         System.out.println("2: fix report");
  446.         System.out.println("3: view report again");
  447.         System.out.println("0: exit to previous menu");
  448.         System.out.println("Please choose number between 0 - 3");
  449.        
  450.         return getInt("Please choose number between 0 - 3");
  451.        
  452.     }
  453.    
  454.     public static void updateReport(int reportId, String reportPriority, int updaterId) {
  455.    
  456.         try {
  457.        
  458.             Class.forName(driverName);
  459.  
  460.             Connection con = DriverManager.getConnection(url);
  461.  
  462.             String sql = ""                                  
  463.                 + "UPDATE   report "
  464.                 + "SET      priority = '" + reportPriority + "' "
  465.                 + "         , status = 'investigating' "
  466.                 + "         , updateTime = '" + getTimestamp() + "' "
  467.                 + "         , handlerId = '" + updaterId + "' "
  468.                 + "WHERE    id = " + reportId + " ";
  469.                        
  470.             Statement stmt = con.createStatement();                      
  471.  
  472.             int count = stmt.executeUpdate(sql);                
  473.  
  474.             stmt.close();
  475.             con.close();
  476.         }
  477.         catch (Exception error) {
  478.             System.err.println("Exception: " + error.getMessage() );
  479.         }
  480.    
  481.     }
  482.    
  483.     public static void fixReport(int reportId, String reportFix, int updaterId) {
  484.    
  485.         try {
  486.        
  487.             Class.forName(driverName);
  488.  
  489.             Connection con = DriverManager.getConnection(url);
  490.  
  491.             String sql = ""                                  
  492.                 + "UPDATE   report "
  493.                 + "SET      fix = '" + reportFix + "' "
  494.                 + "         , status = 'fixed' "
  495.                 + "         , endTime = '" + getTimestamp() + "' "
  496.                 + "         , handlerId = '" + updaterId + "' "
  497.                 + "WHERE    id = " + reportId + " ";
  498.                        
  499.             Statement stmt = con.createStatement();                      
  500.  
  501.             int count = stmt.executeUpdate(sql);                
  502.  
  503.             stmt.close();
  504.             con.close();
  505.         }
  506.         catch (Exception error) {
  507.             System.err.println("Exception: " + error.getMessage() );
  508.         }
  509.    
  510.     }
  511.    
  512.     public static void viewReport(int reportId) {
  513.    
  514.         try {
  515.            
  516.             Class.forName(driverName);
  517.        
  518.             Connection con = DriverManager.getConnection(url);
  519.        
  520.             String sql = "SELECT    * "
  521.                        + "FROM      report "
  522.                        + "WHERE     id = " + reportId + " ";
  523.                    
  524.             Statement stmt = con.createStatement();
  525.        
  526.             ResultSet result = stmt.executeQuery(sql);
  527.            
  528.             while(result.next()) {
  529.                
  530.                 int idDatabase = result.getInt("id");
  531.                                
  532.                 if (idDatabase == reportId) {
  533.        
  534.                     System.out.println("\nReport id: " + result.getInt("id"));
  535.                     System.out.println("Class: " + result.getString("class"));
  536.                     System.out.println("Priority: " + result.getString("priority"));
  537.                     System.out.println("Status: " + result.getString("status"));
  538.                     System.out.println("Description: " + result.getString("description"));
  539.                     System.out.println("Posted: " + result.getString("startTime"));
  540.                     System.out.println("Sender: " + getName(result.getInt("senderId")) + "\n");
  541.                    
  542.                     int updater = result.getInt("handlerId");
  543.                     boolean empty = result.wasNull();
  544.                    
  545.                     if (!empty) {
  546.                    
  547.                         System.out.println("Last updater: " + getName(updater));
  548.                         System.out.println("Last update: " + result.getString("updateTime") + "\n");
  549.                        
  550.                     }
  551.                 }
  552.            
  553.             }
  554.    
  555.             result.close();
  556.             stmt.close();
  557.             con.close();
  558.            
  559.         } catch (Exception error) {
  560.            
  561.                 System.err.println("[ERROR]" + error.getMessage() );
  562.  
  563.                 error.printStackTrace();
  564.                 System.exit(1);
  565.                
  566.         }
  567.                
  568.     }
  569.    
  570.     public static void viewStatistics () {
  571.    
  572.         int chooser = viewStatisticsMenu();
  573.         boolean dateOk = false;
  574.        
  575.        
  576.         String name = " ";
  577.         String newName = " ";
  578.         String time1 = " ";
  579.         String time2 = " ";
  580.        
  581.         String startingDate = " ";
  582.         String endingDate = " ";
  583.        
  584.         int nameId = 0;
  585.        
  586.         do {
  587.        
  588.             if (chooser == 1) { // statistics by name
  589.            
  590.                 System.out.println("Give name:");
  591.                
  592.                 do {
  593.                
  594.                     name = getString("Give name:");
  595.                    
  596.                     newName = name.trim();
  597.                     newName = newName.toLowerCase();
  598.                    
  599.                     nameId = checkName(newName);
  600.                    
  601.                     if (nameId == 0) {
  602.                        
  603.                         System.out.println("There wasn't user with that name!");
  604.                        
  605.                     }
  606.                
  607.                 }
  608.                 while (nameId == 0);
  609.            
  610.                 reportsByName(name, nameId);
  611.            
  612.                 chooser = viewStatisticsMenu();
  613.            
  614.             }
  615.            
  616.             else if (chooser == 2) { // statistics by time
  617.            
  618.                 do {
  619.                    
  620.                     System.out.println("Give starting date for period in format YYYY-MM-DD:");
  621.                     startingDate = getString("Give starting date for period in format YYYY-MM-DD:");
  622.                     dateOk = checkTime(startingDate);
  623.                
  624.                 }
  625.                 while(!dateOk);
  626.                
  627.                 do {
  628.                
  629.                     System.out.println("Give ending date for period in format YYYY-MM-DD:");
  630.                     endingDate = getString("Give ending date for period in format YYYY-MM-DD:");
  631.                     dateOk = checkTime(endingDate);            
  632.                
  633.                 }
  634.                 while(!dateOk);
  635.                
  636.                 System.out.println("Sum of reports posted in given time period: " + reportsByTime(startingDate, endingDate));
  637.            
  638.                 chooser = viewStatisticsMenu();
  639.            
  640.             }
  641.        
  642.             else if (chooser == 3) { // statistics by status
  643.            
  644.                 reportsByStatus();
  645.            
  646.                 chooser = viewStatisticsMenu();
  647.            
  648.             }
  649.            
  650.             else if (chooser == 0) { } // exit to previous menu
  651.            
  652.             else {
  653.            
  654.                 System.out.println("Invalid input!");
  655.            
  656.             }
  657.        
  658.         }
  659.         while (chooser != 0);
  660.        
  661.         System.out.println("\nExiting to previous menu...\n");
  662.    
  663.     }
  664.    
  665.     public static int viewStatisticsMenu () {
  666.    
  667.         int chooser;
  668.        
  669.         System.out.println("\nWhat do you want to do?");
  670.         System.out.println("1: View statistics by name.");
  671.         System.out.println("2: View statistics by time.");
  672.         System.out.println("3: View statistics by status.");
  673.         System.out.println("0: Exit to previous menu.");
  674.         System.out.println("Please choose number between 0 - 3.");
  675.         chooser = getInt("Please choose number between 0 - 3.");
  676.        
  677.         return chooser;
  678.    
  679.     }
  680.  
  681.     public static void reportsByName (String requestName, int nameId) {
  682.    
  683.         int senderOpen = 0;
  684.         int senderInvestigating = 0;
  685.         int senderFixed = 0;
  686.         int senderTotal = 0;
  687.        
  688.         int handlerOpen = 0;
  689.         int handlerInvestigating = 0;
  690.         int handlerFixed = 0;
  691.         int handlerTotal = 0;
  692.    
  693.         try {
  694.            
  695.             Class.forName(driverName);
  696.        
  697.             Connection con = DriverManager.getConnection(url);
  698.        
  699.             String sql = "SELECT    * "
  700.                        + "FROM      report "
  701.                        + "WHERE     senderId = " + nameId + " "
  702.                        + "          OR "
  703.                        + "          handlerId = " + nameId + " ";
  704.                    
  705.             Statement stmt = con.createStatement();
  706.        
  707.             ResultSet result = stmt.executeQuery(sql);
  708.            
  709.             while(result.next()) {
  710.            
  711.                 if (result.getInt("senderId") == nameId) {
  712.                
  713.                     if (result.getString("status").equals("open")) {
  714.                    
  715.                         senderOpen++;
  716.                        
  717.                     }
  718.                    
  719.                     else if (result.getString("status").equals("investigating")) {
  720.                    
  721.                         senderInvestigating++;
  722.                    
  723.                     }
  724.                
  725.                     else if (result.getString("status").equals("fixed")) {
  726.                
  727.                         senderFixed++;
  728.                
  729.                     }
  730.                    
  731.                 }
  732.                
  733.                 if (result.getInt("handlerId") == nameId) {
  734.                
  735.                     if (result.getString("status").equals("open")) {
  736.                    
  737.                         handlerOpen++;
  738.                    
  739.                     }
  740.                    
  741.                     else if (result.getString("status").equals("investigating")) {
  742.                    
  743.                         handlerInvestigating++;
  744.                    
  745.                     }
  746.                
  747.                     else if (result.getString("status").equals("fixed")) {
  748.                
  749.                         handlerFixed++;
  750.                
  751.                     }
  752.                
  753.                
  754.                 }
  755.                
  756.            
  757.             }
  758.    
  759.             result.close();
  760.             stmt.close();
  761.             con.close();
  762.            
  763.         } catch (Exception error) {
  764.            
  765.                 System.err.println("[ERROR]" + error.getMessage() );
  766.  
  767.                 error.printStackTrace();
  768.                 System.exit(1);
  769.                
  770.         }
  771.        
  772.         senderTotal = senderOpen + senderInvestigating + senderFixed;
  773.         handlerTotal = handlerOpen + handlerInvestigating + handlerFixed;
  774.        
  775.         if (senderTotal == 0 && handlerTotal == 0) {
  776.        
  777.             System.out.println("\nThere was no reports by or handled by user " + requestName + ".\n");
  778.            
  779.         }
  780.            
  781.         if (senderTotal > 0) {
  782.        
  783.             System.out.println("\nReports sent by user " + requestName + ":");
  784.             System.out.println("Open reports: " + senderOpen);
  785.             System.out.println("Investigated reports: " + senderInvestigating);
  786.             System.out.println("Fixed reports: " + senderFixed);
  787.             System.out.println("Total: " + senderTotal + "\n");
  788.        
  789.         }
  790.        
  791.         if (handlerTotal > 0) {
  792.        
  793.             System.out.println("\nReports handled by user " + requestName + ":");
  794.             System.out.println("Open reports: " + handlerOpen);
  795.             System.out.println("Investigated reports: " + handlerInvestigating);
  796.             System.out.println("Fixed reports: " + handlerFixed);
  797.             System.out.println("Total: " + handlerTotal + "\n");
  798.        
  799.         }
  800.    
  801.     }
  802.    
  803.     public static int reportsByTime (String start, String end) {
  804.    
  805.         int reportCounter = 0;
  806.    
  807.         try {
  808.            
  809.             Class.forName(driverName);
  810.        
  811.             Connection con = DriverManager.getConnection(url);
  812.        
  813.             String sql = "SELECT    * "
  814.                        + "FROM      report "
  815.                        + "WHERE     startTime BETWEEN '" + start + " 00:00:00' AND '" + end + " 23:59:59'";
  816.                    
  817.             Statement stmt = con.createStatement();
  818.        
  819.             ResultSet result = stmt.executeQuery(sql);
  820.            
  821.             while(result.next()) {
  822.            
  823.                 reportCounter++;
  824.            
  825.             }
  826.    
  827.             result.close();
  828.             stmt.close();
  829.             con.close();
  830.            
  831.         } catch (Exception error) {
  832.            
  833.                 System.err.println("[ERROR]" + error.getMessage() );
  834.  
  835.                 error.printStackTrace();
  836.                 System.exit(1);
  837.                
  838.         }
  839.    
  840.         return reportCounter;
  841.    
  842.     }      
  843.    
  844.     public static void reportsByStatus() {
  845.    
  846.         int open = 0;
  847.         int investigating = 0;
  848.         int fixed = 0;
  849.         int total = 0;
  850.    
  851.         try {
  852.            
  853.             Class.forName(driverName);
  854.        
  855.             Connection con = DriverManager.getConnection(url);
  856.        
  857.             String sql = "SELECT    status "
  858.                        + "FROM      report ";
  859.                    
  860.             Statement stmt = con.createStatement();
  861.        
  862.             ResultSet result = stmt.executeQuery(sql);
  863.            
  864.             while(result.next()) {
  865.            
  866.                
  867.                 if (result.getString("status").equals("open")) {
  868.                    
  869.                     open++;
  870.                        
  871.                 }
  872.                    
  873.                 else if (result.getString("status").equals("investigating")) {
  874.                    
  875.                     investigating++;
  876.                    
  877.                 }
  878.                
  879.                 else if (result.getString("status").equals("fixed")) {
  880.                
  881.                     fixed++;
  882.                
  883.                 }          
  884.            
  885.             }
  886.    
  887.             result.close();
  888.             stmt.close();
  889.             con.close();
  890.            
  891.         } catch (Exception error) {
  892.            
  893.                 System.err.println("[ERROR]" + error.getMessage() );
  894.  
  895.                 error.printStackTrace();
  896.                 System.exit(1);
  897.                
  898.         }
  899.        
  900.         total = open + investigating + fixed;
  901.        
  902.         System.out.println("\nOpen reports: " + open);
  903.         System.out.println("Reports under investigation: " + investigating);
  904.         System.out.println("Fixed reports: " + fixed);
  905.         System.out.println("Reports total: " + total + "\n");
  906.    
  907.     }
  908.    
  909.     public static boolean checkTime(String time) {
  910.    
  911.         boolean ok = false;
  912.    
  913.         if (time.length() == 10 && time.charAt(4) == '-' && time.charAt(7) == '-') {
  914.    
  915.         ok = true;
  916.        
  917.         }  
  918.        
  919.         return ok;
  920.    
  921.     }
  922.    
  923.     public static int checkName(String name) {
  924.    
  925.         int idName = 0;
  926.    
  927.         try {
  928.  
  929.             Class.forName(driverName);
  930.  
  931.             Connection con = DriverManager.getConnection(url);
  932.  
  933.             String sql = "SELECT * "
  934.                        + "FROM person "
  935.                        + "WHERE LOWER(name) = '" + name + "' "
  936.                        + "ORDER BY id ASC ";
  937.  
  938.             Statement stmt = con.createStatement();
  939.  
  940.             ResultSet result = stmt.executeQuery(sql);
  941.  
  942.             while(result.next()) {
  943.  
  944.             String nameDatabase = result.getString("name");
  945.            
  946.             nameDatabase = nameDatabase.trim();
  947.             nameDatabase = nameDatabase.toLowerCase();
  948.                
  949.                 if (nameDatabase.equals(name)) {
  950.                
  951.                     idName = result.getInt("id");
  952.                 }
  953.                
  954.             }
  955.  
  956.             result.close();
  957.             stmt.close();
  958.             con.close();
  959.  
  960.         } catch (Exception error) {
  961.  
  962.             System.err.println("[ERROR]" + error.getMessage() );
  963.  
  964.             error.printStackTrace();
  965.             System.exit(1);
  966.  
  967.         }
  968.        
  969.         return idName; 
  970.    
  971.     }
  972.    
  973.     public static boolean reportIdChecker(int reportId) {
  974.    
  975.         boolean reportIdOk = false;
  976.    
  977.         try {
  978.  
  979.             Class.forName(driverName);
  980.  
  981.             Connection con = DriverManager.getConnection(url);
  982.  
  983.             String sql = "SELECT    id "
  984.                        + "FROM      report "
  985.                        + "WHERE     LOWER(status) = 'open' "
  986.                        + "          OR "       
  987.                        + "          LOWER(status) = 'investigating' "
  988.                        + "ORDER BY  id DESC ";
  989.  
  990.             Statement stmt = con.createStatement();
  991.  
  992.             ResultSet result = stmt.executeQuery(sql);
  993.  
  994.             while(result.next()) {
  995.                
  996.                 int idDatabase = result.getInt("id");
  997.                            
  998.                 if (idDatabase == reportId) {
  999.                
  1000.                     reportIdOk = true;
  1001.                    
  1002.                 }
  1003.  
  1004.             }
  1005.    
  1006.             result.close();
  1007.             stmt.close();
  1008.             con.close();
  1009.  
  1010.         } catch (Exception error) {
  1011.  
  1012.             System.err.println("[ERROR]" + error.getMessage() );
  1013.  
  1014.             error.printStackTrace();
  1015.             System.exit(1);
  1016.  
  1017.         }
  1018.        
  1019.         return reportIdOk;
  1020.    
  1021.     }
  1022.    
  1023.     public static void listReports(String status) {
  1024.    
  1025.         System.out.println("List of faultreports:");
  1026.    
  1027.         try {
  1028.            
  1029.             Class.forName(driverName);
  1030.            
  1031.             Connection con = DriverManager.getConnection(url);
  1032.            
  1033.             String sql = "SELECT    * "
  1034.                        + "FROM      report "
  1035.                        + "WHERE     LOWER(status) = '" + status + "' "
  1036.                        + "ORDER BY  id ASC ";
  1037.                        
  1038.             Statement stmt = con.createStatement();
  1039.            
  1040.             ResultSet result = stmt.executeQuery(sql);
  1041.            
  1042.            
  1043.            
  1044.             while(result.next()) {
  1045.            
  1046.                 System.out.println(result.getInt("id") + " " + getName(result.getInt("senderId")) + " "
  1047.                                    + result.getString("priority") + " " + result.getString("class") + " "
  1048.                                    + result.getString("startTime"));
  1049.                                    
  1050.             }
  1051.        
  1052.             result.close();
  1053.             stmt.close();
  1054.             con.close();
  1055.            
  1056.         } catch (Exception error) {
  1057.        
  1058.             System.err.println("[ERROR]" + error.getMessage() );
  1059.  
  1060.             error.printStackTrace();
  1061.             System.exit(1);
  1062.        
  1063.         }
  1064.        
  1065.         System.out.println();
  1066.            
  1067.     }
  1068.                                    
  1069.     public static int getId() {
  1070.    
  1071.         int yourId = 1;
  1072.    
  1073.         try {
  1074.  
  1075.             Class.forName(driverName);
  1076.  
  1077.             Connection con = DriverManager.getConnection(url);
  1078.  
  1079.             String sql = "SELECT id "
  1080.                        + "FROM report "
  1081.                        + "ORDER BY id DESC ";
  1082.  
  1083.             Statement stmt = con.createStatement();
  1084.  
  1085.             ResultSet result = stmt.executeQuery(sql);
  1086.  
  1087.             while(result.next()) {
  1088.                
  1089.                 int idDatabase = result.getInt("id");
  1090.                            
  1091.                 if (idDatabase >= yourId) {
  1092.                
  1093.                     yourId = idDatabase + 1;
  1094.                    
  1095.                 }
  1096.  
  1097.             }
  1098.  
  1099.             result.close();
  1100.             stmt.close();
  1101.             con.close();
  1102.  
  1103.         } catch (Exception error) {
  1104.  
  1105.             System.err.println("[ERROR]" + error.getMessage() );
  1106.  
  1107.             error.printStackTrace();
  1108.             System.exit(1);
  1109.  
  1110.         }
  1111.        
  1112.         return yourId;
  1113.    
  1114.     }
  1115.    
  1116.     public static String getName(int id) {
  1117.    
  1118.         String name = " ";
  1119.    
  1120.         try {
  1121.  
  1122.             Class.forName(driverName);
  1123.  
  1124.             Connection con = DriverManager.getConnection(url);
  1125.  
  1126.             String sql = "SELECT * "
  1127.                        + "FROM person "
  1128.                        + "ORDER BY id ASC ";
  1129.  
  1130.             Statement stmt = con.createStatement();
  1131.  
  1132.             ResultSet result = stmt.executeQuery(sql);
  1133.  
  1134.             while(result.next()) {
  1135.                
  1136.                 int idDatabase = result.getInt("id");
  1137.                
  1138.                 if (idDatabase == id) {
  1139.                
  1140.                     name = result.getString("name");
  1141.                    
  1142.                 }
  1143.  
  1144.             }
  1145.  
  1146.             result.close();
  1147.             stmt.close();
  1148.             con.close();
  1149.  
  1150.         } catch (Exception error) {
  1151.  
  1152.             System.err.println("[ERROR]" + error.getMessage() );
  1153.  
  1154.             error.printStackTrace();
  1155.             System.exit(1);
  1156.  
  1157.         }
  1158.        
  1159.         return name;
  1160.    
  1161.     }
  1162.    
  1163.     public static boolean operatorCheck(int id) {
  1164.    
  1165.         boolean adminOk = false;
  1166.    
  1167.         try {
  1168.  
  1169.             Class.forName(driverName);
  1170.  
  1171.             Connection con = DriverManager.getConnection(url);
  1172.            
  1173.             String sql = "SELECT * "
  1174.                        + "FROM person "
  1175.                        + "WHERE LOWER(status) LIKE 'admin%' "
  1176.                        + "      OR "
  1177.                        + "      LOWER(status) = 'operator' "
  1178.                        + "ORDER BY id ASC ";
  1179.  
  1180.             Statement stmt = con.createStatement();
  1181.  
  1182.             ResultSet result = stmt.executeQuery(sql);
  1183.  
  1184.             while(result.next()) {
  1185.                
  1186.                 int idDatabase = result.getInt("id");
  1187.                
  1188.                 if (idDatabase == id) {
  1189.                
  1190.                     adminOk = true;
  1191.                    
  1192.                 }
  1193.                
  1194.             }
  1195.  
  1196.             result.close();
  1197.             stmt.close();
  1198.             con.close();
  1199.  
  1200.         } catch (Exception error) {
  1201.  
  1202.             System.err.println("[ERROR]" + error.getMessage() );
  1203.  
  1204.             error.printStackTrace();
  1205.             System.exit(1);
  1206.  
  1207.         }
  1208.        
  1209.         return adminOk;
  1210.    
  1211.     }
  1212.    
  1213.     public static boolean checkId(int id) {
  1214.    
  1215.         boolean idOk = false;
  1216.    
  1217.         try {
  1218.  
  1219.             Class.forName(driverName);
  1220.  
  1221.             Connection con = DriverManager.getConnection(url);
  1222.  
  1223.             String sql = "SELECT * "
  1224.                        + "FROM person "
  1225.                        + "ORDER BY id ASC ";
  1226.  
  1227.             Statement stmt = con.createStatement();
  1228.  
  1229.             ResultSet result = stmt.executeQuery(sql);
  1230.  
  1231.             while(result.next()) {
  1232.  
  1233.             int idDatabase = result.getInt("id");
  1234.            
  1235.                 if (idDatabase == id) {
  1236.                
  1237.                     idOk = true;
  1238.                 }
  1239.                
  1240.             }
  1241.  
  1242.             result.close();
  1243.             stmt.close();
  1244.             con.close();
  1245.  
  1246.         } catch (Exception error) {
  1247.  
  1248.             System.err.println("[ERROR]" + error.getMessage() );
  1249.  
  1250.             error.printStackTrace();
  1251.             System.exit(1);
  1252.  
  1253.         }
  1254.        
  1255.         return idOk;
  1256.    
  1257.     }
  1258.    
  1259.     public static String getTimestamp() {
  1260.    
  1261.     String timestampFormat = "yyyy-MM-dd HH:mm";
  1262.    
  1263.     SimpleDateFormat dateFormat = new SimpleDateFormat(timestampFormat);
  1264.    
  1265.     Date date = new Date();
  1266.    
  1267.     String timestamp = dateFormat.format(date);
  1268.    
  1269.     return timestamp;
  1270.    
  1271.     }
  1272.  
  1273.     public static int getInt(String question) {
  1274.    
  1275.         int number = 0;
  1276.         boolean ok = false;
  1277.    
  1278.         do {
  1279.        
  1280.             try {
  1281.    
  1282.                 number = cin.nextInt();
  1283.                 ok = true;
  1284.                
  1285.             } catch (Exception error) {
  1286.            
  1287.                 cin.nextLine();
  1288.                 System.out.println("False input!");
  1289.                 System.out.println(question);
  1290.                 ok = false;
  1291.                
  1292.             }
  1293.    
  1294.         } while (!ok);
  1295.    
  1296.         return number;
  1297.     }
  1298.    
  1299.     public static String getString(String question) {
  1300.    
  1301.         Scanner cin = new Scanner(System.in);
  1302.         String word = "";
  1303.         boolean ok = false;
  1304.    
  1305.         do {
  1306.        
  1307.             try {
  1308.    
  1309.                 word = cin.nextLine();
  1310.                 ok = true;
  1311.                
  1312.             } catch (Exception error) {
  1313.            
  1314.                 cin.nextLine();
  1315.                 System.out.println("False input!");
  1316.                 System.out.println(question);
  1317.                 ok = false;
  1318.                
  1319.             }
  1320.    
  1321.         } while (!ok);
  1322.    
  1323.         return word;
  1324.     }
  1325.    
  1326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement