Advertisement
rjsantiago0001

Helpdesk Tally

Jul 30th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.86 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.URL;
  3. import java.nio.charset.Charset;
  4. import java.text.DateFormat;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import java.nio.file.Files;
  9. import java.nio.file.Path;
  10. import java.nio.file.Paths;
  11.  
  12. import javafx.application.*;
  13. import javafx.event.ActionEvent;
  14. import javafx.event.EventHandler;
  15. import javafx.geometry.Insets;
  16. import javafx.geometry.Pos;
  17. import javafx.scene.Scene;
  18. import javafx.scene.control.Button;
  19. import javafx.scene.control.RadioButton;
  20. import javafx.scene.control.TextArea;
  21. import javafx.scene.control.ToggleGroup;
  22. import javafx.scene.layout.HBox;
  23. import javafx.scene.layout.VBox;
  24. import javafx.scene.text.Text;
  25. import javafx.stage.Stage;
  26.  
  27. import java.io.IOException;
  28.  
  29. import net.aksingh.owmjapis.CurrentWeather;
  30. import net.aksingh.owmjapis.OpenWeatherMap;
  31. import org.json.JSONException;
  32. import org.json.JSONObject;
  33.  
  34. public class HelpDeskTally extends Application {
  35.  
  36.     /**
  37.      * @param args
  38.      */
  39.  
  40.     int[] totals = new int[6];
  41.     String city = "Holyoke";
  42.     String state = "MA";
  43.     String currentWeather;
  44.     String moonPhase;
  45.     String dayOfWeek;
  46.  
  47.     String username = System.getProperty("user.name");
  48.     Path path = Paths.get("C:\\users\\"+username+"\\desktop\\Helpdesk Tally");
  49.     File countFile = new File("C:\\users\\" + username + "\\desktop\\Helpdesk Tally\\tallyCount.txt");
  50.     File logFile = new File("C:\\users\\" + username + "\\desktop\\Helpdesk Tally\\tallyLog.txt");
  51.  
  52.     RadioButton rbOS = new RadioButton();
  53.     RadioButton rbAD = new RadioButton();
  54.     RadioButton rbCall = new RadioButton();
  55.     RadioButton rbEmail = new RadioButton();
  56.     RadioButton rbWalkin = new RadioButton();
  57.     TextArea taInfo = new TextArea();
  58.     TextArea taStats = new TextArea();
  59.  
  60.     JSONObject json;
  61.  
  62.     public static void main(String[] args) {
  63.         // TODO Auto-generated method stub
  64.         Application.launch(args);
  65.     }
  66.  
  67.     public void start(Stage primaryStage) throws Exception {
  68.         // TODO Auto-generated method stub
  69.         initializeFiles();
  70.         fetchMoonPhase();
  71.         fetchCurrentWeather(city);
  72.         updateInfoPane();
  73.  
  74.         VBox gui = new VBox(03);
  75.         gui.getChildren().addAll(getSelectionPane(), getInfoPane(), getStatusPane());
  76.  
  77.         Scene scene = new Scene(gui, 500, 500);
  78.         primaryStage.setTitle("Helpdesk Tally Assistant");
  79.         primaryStage.setScene(scene);
  80.         primaryStage.show();
  81.         primaryStage.setResizable(false);
  82.     }
  83.  
  84.     private VBox getSelectionPane() {
  85.         VBox selectionPane = new VBox(10);
  86.         selectionPane.setStyle("-fx-border-color: Black;");
  87.         selectionPane.setAlignment(Pos.CENTER);
  88.         selectionPane.setPadding(new Insets(10, 10, 10, 10));
  89.  
  90.         selectionPane.getChildren().addAll(getMessagePane(), getOptionPane());
  91.  
  92.         return selectionPane;
  93.     }
  94.  
  95.     private VBox getServicePane() {
  96.  
  97.         VBox servicePane = new VBox(30);
  98.         servicePane.setStyle("-fx-border-color: Black;");
  99.         servicePane.setAlignment(Pos.TOP_LEFT);
  100.         servicePane.setPadding(new Insets(20, 20, 20, 20));
  101.  
  102.         Text serviceText = new Text();
  103.         serviceText.setText("Service");
  104.         ToggleGroup serviceGroup = new ToggleGroup();
  105.  
  106.         rbOS.setText("Online Services");
  107.         rbOS.setToggleGroup(serviceGroup);
  108.         rbAD.setText("Active Directory");
  109.         rbAD.setToggleGroup(serviceGroup);
  110.  
  111.         servicePane.getChildren().addAll(serviceText, rbOS, rbAD);
  112.         return servicePane;
  113.     }
  114.  
  115.     private VBox getIssuePane() {
  116.  
  117.         VBox issuePane = new VBox(30);
  118.         issuePane.setStyle("-fx-border-color: Black;");
  119.         issuePane.setAlignment(Pos.TOP_LEFT);
  120.         issuePane.setPadding(new Insets(20, 20, 20, 20));
  121.  
  122.         Text issueText = new Text();
  123.         issueText.setText("Issue");
  124.  
  125.         ToggleGroup serviceGroup = new ToggleGroup();
  126.  
  127.         rbCall.setText("Call");
  128.         rbCall.setToggleGroup(serviceGroup);
  129.         rbEmail.setText("Email");
  130.         rbEmail.setToggleGroup(serviceGroup);
  131.         rbWalkin.setText("Walk-in");
  132.         rbWalkin.setToggleGroup(serviceGroup);
  133.  
  134.         issuePane.getChildren().addAll(issueText, rbCall, rbEmail, rbWalkin);
  135.         return issuePane;
  136.     }
  137.  
  138.     private VBox getButtonPane() {
  139.  
  140.         VBox buttonPane = new VBox(30);
  141.         buttonPane.setAlignment(Pos.CENTER);
  142.         buttonPane.setPadding(new Insets(10, 10, 10, 10));
  143.  
  144.         Button btSubmit = new Button("submit");
  145.  
  146.         btSubmit.setOnAction(new EventHandler<ActionEvent>() {
  147.             public void handle(ActionEvent event) {
  148.                 // parse selection to string
  149.                 incrementTotal(verifySelection());
  150.  
  151.                 try {
  152.                     updateCountFile();
  153.                     updateLogFile(verifySelection());
  154.                     updateInfoPane();
  155.  
  156.                 } catch (IOException e) {
  157.                     // TODO Auto-generated catch block
  158.                     e.printStackTrace();
  159.                 } catch (JSONException e) {
  160.                     e.printStackTrace();
  161.                 }
  162.             }
  163.         });
  164.  
  165.         buttonPane.getChildren().addAll(btSubmit);
  166.         return buttonPane;
  167.     }
  168.  
  169.     private HBox getOptionPane() {
  170.  
  171.         HBox optionPane = new HBox(20);
  172.         optionPane.setAlignment(Pos.CENTER);
  173.         optionPane.setPadding(new Insets(10, 10, 10, 10));
  174.  
  175.         optionPane.getChildren().addAll(getServicePane(), getIssuePane(), getButtonPane());
  176.         return optionPane;
  177.     }
  178.  
  179.     private HBox getMessagePane() {
  180.  
  181.         HBox messagePane = new HBox(10);
  182.         messagePane.setAlignment(Pos.CENTER);
  183.  
  184.         Text txtMessage = new Text();
  185.         txtMessage.setDisable(true);
  186.         txtMessage.setText("Hello " + username + ", Make a selection");
  187.  
  188.         messagePane.getChildren().addAll(txtMessage);
  189.         return messagePane;
  190.     }
  191.  
  192.     private HBox getStatusPane() {
  193.  
  194.         HBox statusPane = new HBox(10);
  195.         statusPane.setAlignment(Pos.CENTER);
  196.         statusPane.setStyle("-fx-border-color: Black;");
  197.  
  198.         Text txtStatus = new Text();
  199.         txtStatus.setText("In Progress");
  200.  
  201.         statusPane.getChildren().addAll(txtStatus);
  202.         return statusPane;
  203.     }
  204.  
  205.     private HBox getInfoPane() throws JSONException {
  206.  
  207.         HBox infoPane = new HBox();
  208.         infoPane.setAlignment(Pos.CENTER);
  209.         taStats.setEditable(false);
  210.         taInfo.setEditable(false);
  211.  
  212.         infoPane.setPrefHeight(500);
  213.  
  214.         updateInfoPane();
  215.  
  216.         infoPane.getChildren().addAll(taInfo, taStats);
  217.  
  218.         return infoPane;
  219.     }
  220.  
  221.     private void initializeFiles() throws IOException {
  222.  
  223.        
  224.         if (Files.exists(path)){
  225.             initializeArray();
  226.         }else{
  227.             path = Paths.get("C:\\users\\"+username+"\\desktop\\Helpdesk Tally");
  228.             Files.createDirectories(path);
  229.             countFile.createNewFile();
  230.             logFile.createNewFile();
  231.         }
  232.     }
  233.  
  234.     private void initializeArray() throws IOException {
  235.  
  236.         BufferedReader br = new BufferedReader(new FileReader(countFile));
  237.  
  238.         String total = null;
  239.         for (int i = 0; i < 6; ++i) {
  240.             total = br.readLine();
  241.             int count = Integer.parseInt(total);
  242.             totals[i] = count;
  243.         }
  244.         br.close();
  245.     }
  246.  
  247.     private void incrementTotal(String selection) {
  248.  
  249.         if (selection.contains("Online Services")) {
  250.  
  251.             if (selection.contains("Call")) {
  252.                 totals[0]++;
  253.             }
  254.  
  255.             if (selection.contains("Email")) {
  256.                 totals[1]++;
  257.             }
  258.  
  259.             if (selection.contains("Walkin")) {
  260.                 totals[2]++;
  261.             }
  262.         } else if (selection.contains("Active Directory")) {
  263.  
  264.             if (selection.contains("Call")) {
  265.                 totals[3]++;
  266.             }
  267.  
  268.             if (selection.contains("Email")) {
  269.                 totals[4]++;
  270.             }
  271.  
  272.             if (selection.contains("Walkin")) {
  273.                 totals[5]++;
  274.             }
  275.         }
  276.     }
  277.  
  278.     private String verifySelection() {
  279.  
  280.         String selection = null;
  281.  
  282.         if (rbOS.isSelected()) {
  283.           selection= "Online Services";
  284.         }
  285.  
  286.         if (rbAD.isSelected()) {
  287.            selection= "Active Directory";
  288.         }
  289.  
  290.         if (rbCall.isSelected()) {
  291.             selection+= (" Call");
  292.         }
  293.  
  294.         if (rbEmail.isSelected()) {
  295.             selection+=(" Email");
  296.         }
  297.  
  298.         if (rbWalkin.isSelected()) {
  299.             selection+=(" Walkin");
  300.         }
  301.         return (selection);
  302.     }
  303.  
  304.     private void updateCountFile() throws IOException {
  305.  
  306.         FileOutputStream fos = new FileOutputStream(countFile);
  307.         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
  308.  
  309.         for (int i = 0; i < 6; i++) {
  310.             bw.write(Integer.toString(totals[i]));
  311.             bw.newLine();
  312.         }
  313.         bw.close();
  314.     }
  315.  
  316.     private void updateLogFile(String selection) {
  317.  
  318.         try (FileWriter fw = new FileWriter(logFile, true);
  319.              BufferedWriter bw = new BufferedWriter(fw);
  320.              PrintWriter out = new PrintWriter(bw)) {
  321.             out.println(fetchDate("MM/dd/yyyy HH:mm:ss") + " " + selection);
  322.             bw.close();
  323.  
  324.         } catch (IOException e) {
  325.             //exception handling left as an exercise for the reader
  326.         }
  327.     }
  328.  
  329.     private String fetchDate(String format) {
  330.  
  331.         // " MM/dd/yyyy HH:mm:ss"
  332.         DateFormat df = new SimpleDateFormat(format);
  333.         Date today = Calendar.getInstance().getTime();
  334.         String reportDate = df.format(today);
  335.  
  336.         return reportDate;
  337.     }
  338.  
  339.     private void fetchCurrentWeather(String location) {
  340.  
  341.         OpenWeatherMap owm = new OpenWeatherMap("ea950a82a1470f29811e9fe0592d6afa");
  342.  
  343.         CurrentWeather cwd = null;
  344.         try {
  345.             cwd = owm.currentWeatherByCityName(location);
  346.         } catch (IOException e) {
  347.             e.printStackTrace();
  348.         } catch (JSONException e) {
  349.             e.printStackTrace();
  350.         }
  351.  
  352.         currentWeather = (location + "\nTemperature: " + cwd.getMainInstance().getMaxTemperature()
  353.                 + "/" + cwd.getMainInstance().getMinTemperature() + "\'F\nHumidity: " + cwd.getMainInstance().getHumidity()) + "\nPressure: " + cwd.getMainInstance().getPressure();
  354.     }
  355.  
  356.     private void updateInfoPane() throws JSONException {
  357.         taInfo.setText("Online Services\nCalls: " + totals[0] + "\nEmails: " + totals[1] + "\nWalkin: " + totals[2] + "\n\nActive Directory\nCalls: " + totals[3] + "\nEmails: " + totals[4] + "\nWalkin: " + totals[5]);
  358.         taStats.setText(fetchDate("MM/dd/yyyy") + "\n" + dayOfWeek + "\n\n" + currentWeather + "\n\nlunar phase: " + moonPhase);
  359.     }
  360.    
  361.     private void fetchMoonPhase() throws IOException, JSONException {
  362.         json = readJsonFromUrl("http://api.usno.navy.mil/rstt/oneday?date=" + fetchDate("MM/dd/yyyy") + "&loc=" + city + ",%20" + state);
  363.         Object phase = json.get("curphase");
  364.         Object day = json.get("dayofweek");
  365.        
  366.         moonPhase = phase.toString();
  367.         dayOfWeek = day.toString();  
  368.     }
  369.    
  370.     private static String readAll(Reader rd) throws IOException {
  371.         StringBuilder sb = new StringBuilder();
  372.         int cp;
  373.         while ((cp = rd.read()) != -1) {
  374.             sb.append((char) cp);
  375.         }
  376.         return sb.toString();
  377.     }
  378.  
  379.     public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
  380.         InputStream is = new URL(url).openStream();
  381.         try {
  382.             BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
  383.             String jsonText = readAll(rd);
  384.             JSONObject json = new JSONObject(jsonText);
  385.             return json;
  386.         } finally {
  387.             is.close();
  388.         }
  389.     }
  390.  
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement