Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.text.*;
- public class DiaTrackApp
- {
- public final static String VALIDHEADER = "Date,Time,BloodSugar,Humalog,Lantis,Carbs";
- public final static String COMMA = ",";
- public final static String NEWLINE = System.lineSeparator();
- public final static String DEFAULTPATH = "C:\\Users\\DigiDuncan\\Desktop\\DiaTrack";
- public static String fileName = DEFAULTPATH + "\\diatrack.csv";
- public static File spreadsheet = new File (fileName);
- public static String dateString = "1970-1-1";
- public static String timeString = "00:00";
- public static String entry = null;
- public static String entryDate, entryTime;
- public static int entryBloodSugar, entryHumalog, entryLantis, entryCarbs;
- public static Scanner input = new Scanner(System.in);
- public static void main(String[] args) throws IOException
- {
- String choice = "n";
- final String GUI = "\n+-[DIATRACK]--------------+" +
- "\n| |" +
- "\n| (M) Manual Entry |" +
- "\n| (A) Assisted Entry |" +
- "\n| (F) Fresh Start |" +
- "\n| (X) Exit |" +
- "\n| |" +
- "\n+-------------------------+\n";
- getCurrentDateTime();
- System.out.println("Debug: " + isValidFile(fileName));
- System.out.println(GUI);
- System.out.print(">");
- choice = input.nextLine();
- switch (choice)
- {
- case "m":
- manualEntry();
- break;
- case "a":
- assistedEntry();
- break;
- case "f":
- freshStart();
- break;
- case "x":
- System.exit(0);
- break;
- default:
- System.err.println("Unrecognized menu option! (Error code UMO)");
- }
- }
- public static void getCurrentDateTime()
- {
- // Get today's date.
- Date today = Calendar.getInstance().getTime();
- // (2) create a date and a time formatter.
- SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
- SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm");
- //Set the global variables correctly.
- dateString = dateFormatter.format(today);
- timeString = timeFormatter.format(today);
- System.out.println("Debug: " + dateString + " " + timeString);
- }
- public static boolean isValidFile (String file)
- {
- try
- {
- FileReader fileReader = new FileReader(file);
- // Always wrap FileReader in BufferedReader.
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- String header = bufferedReader.readLine();
- // Always close files.
- bufferedReader.close();
- System.out.println("Debug: " + header);
- return header.equals(VALIDHEADER);
- }
- catch(FileNotFoundException ex)
- {
- System.err.println(
- "Unable to open file '" + file + "' (Error code UOF)");
- return false;
- }
- catch(IOException ex)
- {
- System.err.println(
- "Error reading file '" + file + "' (Error code ERF)");
- return false;
- }
- }
- public static void freshStart() throws IOException
- {
- FileWriter spreadsheetWriter = new FileWriter(spreadsheet);
- if (spreadsheet.exists())
- {
- spreadsheet.delete();
- spreadsheet.createNewFile();
- spreadsheetWriter.write(VALIDHEADER + NEWLINE);
- }
- else
- {
- spreadsheet.createNewFile();
- spreadsheetWriter.write(VALIDHEADER + NEWLINE);
- }
- spreadsheetWriter.close();
- System.out.println("Fresh Start successful! File located at " + fileName);
- }
- public static void manualEntry() throws IOException
- {
- FileWriter spreadsheetWriter = new FileWriter(spreadsheet, true);
- System.out.println("Input date (yyyy-MM-dd):");
- System.out.print(">");
- entryDate = input.nextLine();
- System.out.println("Input time (HH:mm, 24hr time):");
- System.out.print(">");
- entryTime = input.nextLine();
- System.out.println("Input blood sugar (mg/dL):");
- System.out.print(">");
- entryBloodSugar = input.nextInt();
- System.out.println("Input Humalog (cc):");
- System.out.print(">");
- entryHumalog = input.nextInt();
- System.out.println("Input Lantis (cc):");
- System.out.print(">");
- entryLantis = input.nextInt();
- System.out.println("Input carbohydate intake (g):");
- System.out.print(">");
- entryCarbs = input.nextInt();
- entry = entryDate + COMMA + entryTime + COMMA + entryBloodSugar + COMMA +
- entryHumalog + COMMA + entryLantis + COMMA + entryCarbs + NEWLINE;
- spreadsheetWriter.write(entry);
- spreadsheetWriter.close();
- System.out.println("Successfully wrote " + entry + " to " + fileName);
- }
- public static void assistedEntry() throws IOException
- {
- FileWriter spreadsheetWriter = new FileWriter(spreadsheet, true);
- entryDate = dateString;
- entryTime = timeString;
- System.out.println("Input blood sugar (mg/dL):");
- System.out.print(">");
- entryBloodSugar = input.nextInt();
- System.out.println("Input Humalog (cc):");
- System.out.print(">");
- entryHumalog = input.nextInt();
- System.out.println("Input Lantis (cc):");
- System.out.print(">");
- entryLantis = input.nextInt();
- System.out.println("Input carbohydate intake (g):");
- System.out.print(">");
- entryCarbs = input.nextInt();
- entry = entryDate + COMMA + entryTime + COMMA + entryBloodSugar + COMMA +
- entryHumalog + COMMA + entryLantis + COMMA + entryCarbs + NEWLINE;
- spreadsheetWriter.write(entry);
- spreadsheetWriter.close();
- System.out.println("Successfully wrote " + entry + " to " + fileName);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement