Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Calendar;
- import java.util.GregorianCalendar;
- import java.util.Scanner;
- public class susie {
- int user = 0;
- String userString = "";
- String[] errors = new String[2];
- int i = 0;
- File textDataFile = new File("TextData.txt");
- File userSettingsFile = new File("Settings.txt");
- String message = "";
- String subject = "";
- // Define the time and date
- static Calendar calendar = new GregorianCalendar();
- // Used to prompt for user input
- InputStreamReader istream = new InputStreamReader(System.in);
- BufferedReader bufRead = new BufferedReader(istream);
- // Check for the files - returns false if they don't exist
- public boolean checkForFiles() {
- if (!textDataFile.exists()) {
- errors[0] = ("We were not able to find the user data file in the current directory.");
- return false;
- }
- if (!userSettingsFile.exists()) {
- errors[1] = ("We were not able to find the user settings file in the current directory.");
- return false;
- }
- return true;
- }
- // Check if there are errors - return true/false
- public boolean checkForErrors() {
- if (errors[0] != null) {
- return true;
- }
- return false;
- }
- // Return errors in array form
- public String[] returnErrors() {
- return errors;
- }
- // create the text data file since it does not exist
- public void createTextDataFile() {
- try {
- // Create file
- FileWriter stream = new FileWriter("TextData.txt");
- BufferedWriter out = new BufferedWriter(stream);
- // Close the output stream
- out.close();
- } catch (Exception e) {// Catch exception if any
- System.err.println("Error: " + e.getMessage());
- }
- this.checkForFiles();
- }
- // create the text data file since it does not exist
- public void createSettingsFile() {
- try {
- // Create file
- FileWriter stream = new FileWriter("Settings.txt");
- BufferedWriter out = new BufferedWriter(stream);
- // Close the output stream
- out.close();
- } catch (Exception e) {// Catch exception if any
- System.err.println("Error: " + e.getMessage());
- }
- this.checkForFiles();
- }
- public void sendMessage() {
- try {
- // Message
- System.out.println("Message: ");
- message = bufRead.readLine();
- while (message.isEmpty()) {
- System.out
- .println("Please fill in this field before continuing!");
- System.out.println("Message: ");
- message = bufRead.readLine();
- }
- // Confirmation of text sent
- System.out
- .println("\n------------------------\n\nSummary of message sent:\nSubject: "
- + subject + "\nMessage: " + message);
- } catch (IOException err) {
- System.out.println("Error reading line(s).");
- }
- }
- // Get the current time/date, and return it as a string
- public static String getTimeStamp() {
- // Get the current time/date
- String am_pm;
- int hour = calendar.get(Calendar.HOUR);
- int minute = calendar.get(Calendar.MINUTE);
- int second = calendar.get(Calendar.SECOND);
- String hourString = Integer.toString(hour);
- String minuteString = Integer.toString(minute);
- String secondString = Integer.toString(second);
- if (calendar.get(Calendar.AM_PM) == 0)
- am_pm = "AM";
- else
- am_pm = "PM";
- String timeStamp = hourString + ":" + minuteString + ":" + secondString;
- return timeStamp;
- }
- public void writeToTextData() {
- try {
- BufferedWriter out = new BufferedWriter(new FileWriter(
- "TextData.txt", true));
- out.write("\r\n" + user + "Ø" + hour + minute + second + am_pm
- + "Ø");
- out.write(message);
- out.close();
- } catch (Exception e) {
- System.err.println("\nError: " + e.getMessage());
- }
- }
- public int getUser() {
- // Get the user #
- if (userSettingsFile.exists()) {
- try {
- Scanner scanner = new Scanner(userSettingsFile);
- while (scanner.hasNextLine()) {
- userString = scanner.nextLine();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- // Set the user #
- if (userString.equals("1")) {
- user = 1;
- } else if (userString.equals("2")) {
- user = 2;
- }
- return user;
- }
- public static void main(String[] args0) {
- // this method is only a place holder
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement