Advertisement
DigiDuncan

DiaTrack

Aug 7th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.74 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4.  
  5. public class DiaTrackApp
  6. {
  7.     public final static String VALIDHEADER = "Date,Time,BloodSugar,Humalog,Lantis,Carbs";
  8.     public final static String COMMA = ",";
  9.     public final static String NEWLINE = System.lineSeparator();
  10.     public final static String DEFAULTPATH = "C:\\Users\\DigiDuncan\\Desktop\\DiaTrack";
  11.     public static String fileName = DEFAULTPATH + "\\diatrack.csv";
  12.     public static File spreadsheet = new File (fileName);
  13.     public static String dateString = "1970-1-1";
  14.     public static String timeString = "00:00";
  15.     public static String entry = null;
  16.     public static String entryDate, entryTime;
  17.     public static int entryBloodSugar, entryHumalog, entryLantis, entryCarbs;
  18.     public static Scanner input = new Scanner(System.in);
  19.    
  20.     public static void main(String[] args) throws IOException
  21.     {
  22.         String choice = "n";
  23.        
  24.         final String GUI =  "\n+-[DIATRACK]--------------+" +
  25.                             "\n|                         |" +
  26.                             "\n| (M) Manual Entry        |" +
  27.                             "\n| (A) Assisted Entry      |" +
  28.                             "\n| (F) Fresh Start         |" +
  29.                             "\n| (X) Exit                |" +
  30.                             "\n|                         |" +
  31.                             "\n+-------------------------+\n";
  32.        
  33.         getCurrentDateTime();
  34.         System.out.println("Debug: " + isValidFile(fileName));
  35.         System.out.println(GUI);
  36.        
  37.         System.out.print(">");
  38.         choice = input.nextLine();
  39.    
  40.         switch (choice)
  41.         {
  42.             case "m":
  43.                 manualEntry();
  44.                 break;
  45.             case "a":
  46.                 assistedEntry();
  47.                 break;
  48.             case "f":
  49.                 freshStart();
  50.                 break;
  51.             case "x":
  52.                 System.exit(0);
  53.                 break;
  54.             default:
  55.                 System.err.println("Unrecognized menu option! (Error code UMO)");
  56.         }
  57.     }
  58.    
  59.     public static void getCurrentDateTime()
  60.     {
  61.         // Get today's date.
  62.         Date today = Calendar.getInstance().getTime();
  63.        
  64.         // (2) create a date and a time formatter.
  65.         SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
  66.         SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm");
  67.        
  68.         //Set the global variables correctly.
  69.         dateString = dateFormatter.format(today);
  70.         timeString = timeFormatter.format(today);
  71.  
  72.         System.out.println("Debug: " + dateString + " " + timeString);
  73.     }
  74.    
  75.     public static boolean isValidFile (String file)
  76.     {
  77.         try
  78.         {
  79.             FileReader fileReader = new FileReader(file);
  80.  
  81.             // Always wrap FileReader in BufferedReader.
  82.             BufferedReader bufferedReader = new BufferedReader(fileReader);
  83.            
  84.             String header = bufferedReader.readLine();
  85.            
  86.             // Always close files.
  87.             bufferedReader.close();
  88.            
  89.             System.out.println("Debug: " + header);
  90.            
  91.             return header.equals(VALIDHEADER);
  92.         }
  93.         catch(FileNotFoundException ex)
  94.         {
  95.             System.err.println(
  96.                 "Unable to open file '" + file + "' (Error code UOF)");  
  97.             return false;
  98.         }
  99.         catch(IOException ex)
  100.         {
  101.             System.err.println(
  102.                 "Error reading file '" + file + "' (Error code ERF)");
  103.             return false;
  104.         }
  105.     }
  106.    
  107.     public static void freshStart() throws IOException
  108.     {
  109.         FileWriter spreadsheetWriter = new FileWriter(spreadsheet);
  110.        
  111.         if (spreadsheet.exists())
  112.         {
  113.             spreadsheet.delete();
  114.             spreadsheet.createNewFile();
  115.             spreadsheetWriter.write(VALIDHEADER + NEWLINE);
  116.         }
  117.         else
  118.         {
  119.             spreadsheet.createNewFile();
  120.             spreadsheetWriter.write(VALIDHEADER + NEWLINE);
  121.         }
  122.        
  123.         spreadsheetWriter.close();
  124.        
  125.         System.out.println("Fresh Start successful! File located at " + fileName);
  126.     }
  127.    
  128.     public static void manualEntry() throws IOException
  129.     {
  130.         FileWriter spreadsheetWriter = new FileWriter(spreadsheet, true);
  131.        
  132.         System.out.println("Input date (yyyy-MM-dd):");
  133.        
  134.         System.out.print(">");
  135.         entryDate = input.nextLine();
  136.        
  137.         System.out.println("Input time (HH:mm, 24hr time):");
  138.        
  139.         System.out.print(">");
  140.         entryTime = input.nextLine();
  141.        
  142.         System.out.println("Input blood sugar (mg/dL):");
  143.        
  144.         System.out.print(">");
  145.         entryBloodSugar = input.nextInt();
  146.        
  147.         System.out.println("Input Humalog (cc):");
  148.        
  149.         System.out.print(">");
  150.         entryHumalog = input.nextInt();
  151.        
  152.         System.out.println("Input Lantis (cc):");
  153.        
  154.         System.out.print(">");
  155.         entryLantis = input.nextInt();
  156.        
  157.         System.out.println("Input carbohydate intake (g):");
  158.        
  159.         System.out.print(">");
  160.         entryCarbs = input.nextInt();
  161.        
  162.         entry = entryDate + COMMA + entryTime + COMMA + entryBloodSugar + COMMA +
  163.                 entryHumalog + COMMA + entryLantis + COMMA + entryCarbs + NEWLINE;
  164.        
  165.         spreadsheetWriter.write(entry);
  166.        
  167.         spreadsheetWriter.close();
  168.        
  169.         System.out.println("Successfully wrote " + entry + " to " + fileName);
  170.     }
  171.     public static void assistedEntry() throws IOException
  172.     {
  173.         FileWriter spreadsheetWriter = new FileWriter(spreadsheet, true);
  174.        
  175.         entryDate = dateString;
  176.         entryTime = timeString;
  177.        
  178.         System.out.println("Input blood sugar (mg/dL):");
  179.        
  180.         System.out.print(">");
  181.         entryBloodSugar = input.nextInt();
  182.        
  183.         System.out.println("Input Humalog (cc):");
  184.        
  185.         System.out.print(">");
  186.         entryHumalog = input.nextInt();
  187.        
  188.         System.out.println("Input Lantis (cc):");
  189.        
  190.         System.out.print(">");
  191.         entryLantis = input.nextInt();
  192.        
  193.         System.out.println("Input carbohydate intake (g):");
  194.        
  195.         System.out.print(">");
  196.         entryCarbs = input.nextInt();
  197.        
  198.         entry = entryDate + COMMA + entryTime + COMMA + entryBloodSugar + COMMA +
  199.                 entryHumalog + COMMA + entryLantis + COMMA + entryCarbs + NEWLINE;
  200.        
  201.         spreadsheetWriter.write(entry);
  202.        
  203.         spreadsheetWriter.close();
  204.        
  205.         System.out.println("Successfully wrote " + entry + " to " + fileName);
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement