Advertisement
droidus

sender2

Dec 13th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 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.sql.Timestamp;
  9. import java.util.Scanner;
  10.  
  11. public class sender {
  12.     int user = 0;
  13.     String userString = "";
  14.     String[] errors = new String[1];
  15.     int i = 0;
  16.     File textDataFile = new File("TextData.txt");
  17.     File userSettingsFile = new File("Settings.txt");
  18.     String message = "";
  19.     String subject = "";
  20.     // Define the time and date
  21.     java.util.Date date = new java.util.Date();
  22.     // Used to prompt for user input
  23.     InputStreamReader istream = new InputStreamReader(System.in);
  24.     BufferedReader bufRead = new BufferedReader(istream);
  25.  
  26.     public boolean checkForFile() {
  27.         if (!textDataFile.exists()) {
  28.             errors[0] = ("We were not able to find the user data file in the current directory.");
  29.             return false;
  30.         }
  31.         return true;
  32.     }
  33.  
  34.     public void createTextData() {
  35.         // If there are errors, print them out
  36.         if (errors[0] != null) {
  37.             if (errors[0] != null) {
  38.                 System.out.println(errors[i]);
  39.             }
  40.             System.out
  41.                     .println("\nWould you like to attempt to resolve this issue? (Strike the \"f\" key to fix): ");
  42.             Scanner sc = new Scanner(System.in);
  43.             String a = sc.next();
  44.             if (a.equals("f")) {
  45.                 System.out.println("Please wait...");
  46.                 // progressBar.setValue(50);
  47.                 try {
  48.                     // Create file
  49.                     FileWriter fstream = new FileWriter("TextData.txt");
  50.                     BufferedWriter out = new BufferedWriter(fstream);
  51.                     // Close the output stream
  52.                     out.close();
  53.                 } catch (Exception e) {// Catch exception if any
  54.                     System.err.println("Error: " + e.getMessage());
  55.                 }
  56.                 if (textDataFile.exists()) {
  57.                     System.out.println("The file was created!");
  58.                 } else {
  59.                     System.out
  60.                             .println("The file does not exist.  There were errors. Please contact us.");
  61.                 }
  62.             } else {
  63.                 System.out.println("Program terminated by user.");
  64.                 System.exit(0);
  65.             }
  66.         }
  67.     }
  68.  
  69.     public void sendMessage() {
  70.         try {
  71.             // Subject
  72.             System.out.println("Subject: ");
  73.             subject = bufRead.readLine();
  74.             while (subject.isEmpty()) {
  75.                 System.out
  76.                         .println("Please fill in this field before continuing!");
  77.                 System.out.println("Subject: ");
  78.                 subject = bufRead.readLine();
  79.             }
  80.             // Message
  81.             System.out.println("Message: ");
  82.             message = bufRead.readLine();
  83.             while (message.isEmpty()) {
  84.                 System.out
  85.                         .println("Please fill in this field before continuing!");
  86.                 System.out.println("Message: ");
  87.                 message = bufRead.readLine();
  88.             }
  89.             // Confirmation of text sent
  90.             System.out
  91.                     .println("\n------------------------\n\nSummary of message sent:\nSubject: "
  92.                             + subject + "\nMessage: " + message);
  93.         } catch (IOException err) {
  94.             System.out.println("Error reading line(s).");
  95.         }
  96.     }
  97.  
  98.     public void writeToFile() {
  99.         try {
  100.             BufferedWriter out = new BufferedWriter(new FileWriter(
  101.                     "TextData.txt", true));
  102.             // Get the current time/date
  103.             Object timenDate = new Timestamp(date.getTime());
  104.             // Convert the date/time object to a string
  105.             String dateNTime = timenDate.toString();
  106.             out.write("\r\n" + user + "Ø" + dateNTime + "Ø");
  107.             out.write(subject + "Ø" + message);
  108.             out.close();
  109.         } catch (Exception e) {
  110.             System.err.println("\nError: " + e.getMessage());
  111.         }
  112.     }
  113.  
  114.     public int getUser() {
  115.         // Get the user #
  116.         if (userSettingsFile.exists()) {
  117.             try {
  118.                 Scanner scanner = new Scanner(userSettingsFile);
  119.                 while (scanner.hasNextLine()) {
  120.                     userString = scanner.nextLine();
  121.                 }
  122.             } catch (FileNotFoundException e) {
  123.                 e.printStackTrace();
  124.             }
  125.         }
  126.  
  127.         // Set the user #
  128.         if (userString.equals("1")) {
  129.             user = 1;
  130.  
  131.         } else if (userString.equals("2")) {
  132.             user = 2;
  133.  
  134.         }
  135.         return user;
  136.     }
  137.  
  138.     public static void main(String[] args0) {
  139.  
  140.     }
  141.  
  142. }
  143.  
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement