Advertisement
droidus

Untitled

Dec 14th, 2011
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.33 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.util.Calendar;
  9. import java.util.GregorianCalendar;
  10. import java.util.Scanner;
  11.  
  12. public class susie {
  13.     int user = 0;
  14.     String userString = "";
  15.     String[] errors = new String[2];
  16.     int i = 0;
  17.     File textDataFile = new File("TextData.txt");
  18.     File userSettingsFile = new File("Settings.txt");
  19.     String message = "";
  20.     String subject = "";
  21.     // Define the time and date
  22.     static Calendar calendar = new GregorianCalendar();
  23.     // Used to prompt for user input
  24.     InputStreamReader istream = new InputStreamReader(System.in);
  25.     BufferedReader bufRead = new BufferedReader(istream);
  26.  
  27.     // Check for the files - returns false if they don't exist
  28.     public boolean checkForFiles() {
  29.         if (!textDataFile.exists()) {
  30.             errors[0] = ("We were not able to find the user data file in the current directory.");
  31.             return false;
  32.         }
  33.         if (!userSettingsFile.exists()) {
  34.             errors[1] = ("We were not able to find the user settings file in the current directory.");
  35.             return false;
  36.         }
  37.         return true;
  38.     }
  39.  
  40.     // Check if there are errors - return true/false
  41.     public boolean checkForErrors() {
  42.         if (errors[0] != null) {
  43.             return true;
  44.         }
  45.         return false;
  46.     }
  47.  
  48.     // Return errors in array form
  49.     public String[] returnErrors() {
  50.         return errors;
  51.     }
  52.  
  53.     // create the text data file since it does not exist
  54.     public void createTextDataFile() {
  55.         try {
  56.             // Create file
  57.             FileWriter stream = new FileWriter("TextData.txt");
  58.             BufferedWriter out = new BufferedWriter(stream);
  59.             // Close the output stream
  60.             out.close();
  61.         } catch (Exception e) {// Catch exception if any
  62.             System.err.println("Error: " + e.getMessage());
  63.         }
  64.         this.checkForFiles();
  65.     }
  66.  
  67.     // create the text data file since it does not exist
  68.     public void createSettingsFile() {
  69.         try {
  70.             // Create file
  71.             FileWriter stream = new FileWriter("Settings.txt");
  72.             BufferedWriter out = new BufferedWriter(stream);
  73.             // Close the output stream
  74.             out.close();
  75.         } catch (Exception e) {// Catch exception if any
  76.             System.err.println("Error: " + e.getMessage());
  77.         }
  78.         this.checkForFiles();
  79.     }
  80.  
  81.     public void sendMessage() {
  82.         try {
  83.             // Message
  84.             System.out.println("Message: ");
  85.             message = bufRead.readLine();
  86.             while (message.isEmpty()) {
  87.                 System.out
  88.                         .println("Please fill in this field before continuing!");
  89.                 System.out.println("Message: ");
  90.                 message = bufRead.readLine();
  91.             }
  92.             // Confirmation of text sent
  93.             System.out
  94.                     .println("\n------------------------\n\nSummary of message sent:\nSubject: "
  95.                             + subject + "\nMessage: " + message);
  96.         } catch (IOException err) {
  97.             System.out.println("Error reading line(s).");
  98.         }
  99.     }
  100.  
  101.     // Get the current time/date, and return it as a string
  102.     public static String getTimeStamp() {
  103.         // Get the current time/date
  104.         String am_pm;
  105.         int hour = calendar.get(Calendar.HOUR);
  106.         int minute = calendar.get(Calendar.MINUTE);
  107.         int second = calendar.get(Calendar.SECOND);
  108.        
  109.         String hourString = Integer.toString(hour);
  110.         String minuteString = Integer.toString(minute);
  111.         String secondString = Integer.toString(second);
  112.        
  113.         if (calendar.get(Calendar.AM_PM) == 0)
  114.             am_pm = "AM";
  115.         else
  116.             am_pm = "PM";
  117.         String timeStamp = hourString + ":" + minuteString + ":" + secondString;
  118.        
  119.         return timeStamp;
  120.     }
  121.  
  122.     public void writeToTextData() {
  123.         try {
  124.             BufferedWriter out = new BufferedWriter(new FileWriter(
  125.                     "TextData.txt", true));
  126.  
  127.             out.write("\r\n" + user + "Ø" + hour + minute + second + am_pm
  128.                     + "Ø");
  129.             out.write(message);
  130.             out.close();
  131.         } catch (Exception e) {
  132.             System.err.println("\nError: " + e.getMessage());
  133.         }
  134.     }
  135.  
  136.     public int getUser() {
  137.         // Get the user #
  138.         if (userSettingsFile.exists()) {
  139.             try {
  140.                 Scanner scanner = new Scanner(userSettingsFile);
  141.                 while (scanner.hasNextLine()) {
  142.                     userString = scanner.nextLine();
  143.                 }
  144.             } catch (FileNotFoundException e) {
  145.                 e.printStackTrace();
  146.             }
  147.         }
  148.         // Set the user #
  149.         if (userString.equals("1")) {
  150.             user = 1;
  151.         } else if (userString.equals("2")) {
  152.             user = 2;
  153.         }
  154.         return user;
  155.     }
  156.  
  157.     public static void main(String[] args0) {
  158.         // this method is only a place holder
  159.     }
  160.  
  161. }
  162.  
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement