Advertisement
Guest User

sadasd

a guest
Aug 28th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.33 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class TextBuddy {
  5.     public static final String WELCOME_MESSAGE = "Welcome to TextBuddy! %1$s is ready for use!";
  6.     public static final String COMMAND_MESSAGE = "command: ";
  7.     public static final String ADD_MESSAGE = "Added %1$s to %2$s";
  8.     public static final String DELETE_MESSAGE = "Deleted from %1$s: \"%2$s\"";
  9.     public static final String CLEAR_MESSAGE = "All data cleared from %1$s";
  10.    
  11.     public static final String ERROR_IS_EMPTY_MESSAGE = "Error! %1$s is empty!";
  12.     public static final String ERROR_CANNOT_ADD_MESSAGE = "Error! No data to add!";
  13.     public static final String ERROR_CANNOT_DELETE_NULL_MESSAGE = "Error! No specified data to delete!";
  14.     public static final String ERROR_CANNOT_DELETE_MESSAGE = "Error! Unable to delete %1$s from %2$s";
  15.     public static final String ERROR_CANNOT_SAVE_MESSAGE = "Error! Cannot save data to ";
  16.     public static final String ERROR_INVALID_COMMAND_MESSAGE = "Error! Invalid command!";
  17.    
  18.     public static final String STRING_ADD = "add";
  19.     public static final String STRING_DELETE = "delete";
  20.     public static final String STRING_CLEAR = "clear";
  21.     public static final String STRING_DISPLAY = "display";
  22.     public static final String STRING_EXIT = "exit";
  23.    
  24.     public static final int NOT_FOUND = -1;
  25.    
  26.     public static void main(String[] args){
  27.         String fileName = args[0];
  28.         File outputFile = new File(fileName);
  29.         ArrayList<String> textFile = new ArrayList<String>();
  30.         Scanner sc = new Scanner(System.in);
  31.         linePrinter(WELCOME_MESSAGE, fileName, null);
  32.         executeCommand(textFile, fileName, outputFile, sc);
  33.     }
  34.    
  35.     // this method is the main driver for the program as
  36.     // it reads inputs from the user or file and executes them
  37.     private static void executeCommand(ArrayList<String> textFile, String fileName, File outputFile, Scanner sc){
  38.         String commandEntered = null;
  39.         do{
  40.             commandLinePrinter(COMMAND_MESSAGE);
  41.             commandEntered = sc.nextLine();
  42.             runCommands(commandEntered, textFile, fileName);
  43.             saveDataToFile(textFile, fileName, outputFile);
  44.         } while (commandEntered != null);
  45.     }
  46.    
  47.     private static void runCommands(String commandEntered, ArrayList<String> textFile, String fileName){
  48.         if (commandEntered == null){
  49.             return;
  50.         }
  51.        
  52.         String commandWord = getCommandWord(commandEntered);
  53.        
  54.         switch (commandWord){
  55.         case STRING_ADD:
  56.             addData(textFile, commandEntered, fileName);
  57.             break;
  58.         case STRING_DELETE:
  59.             deleteData(textFile, commandEntered, fileName);
  60.             break;
  61.         case STRING_DISPLAY:
  62.             displayData(textFile, fileName);
  63.             break;
  64.         case STRING_CLEAR:
  65.             clearData(textFile, fileName);
  66.             break;
  67.         case STRING_EXIT:
  68.             exit();
  69.             break;
  70.         default:
  71.             linePrinter(ERROR_INVALID_COMMAND_MESSAGE, null, null);
  72.             break;
  73.         }
  74.     }
  75.    
  76.     private static String getCommandWord (String commandEntered){
  77.         int indexOfFirstSpace = commandEntered.indexOf(' ');
  78.         if (indexOfFirstSpace == NOT_FOUND){
  79.             return commandEntered;
  80.         }
  81.         String commandWord = commandEntered.substring(0, indexOfFirstSpace);
  82.         return commandWord;
  83.     }
  84.        
  85.     private static String getCommandData(String commandEntered){
  86.         int lengthOfCommand = commandEntered.length();
  87.         int indexOfFirstSpace = commandEntered.indexOf(' ');
  88.         if (indexOfFirstSpace == NOT_FOUND){
  89.             return null;
  90.         }
  91.         String commandData = commandEntered.substring(indexOfFirstSpace + 1, lengthOfCommand);
  92.         return commandData;
  93.     }
  94.    
  95.     private static void addData(ArrayList<String> textFile, String commandEntered, String fileName){   
  96.         String commandData = getCommandData(commandEntered);
  97.        
  98.         if(commandData != null){
  99.             textFile.add(commandData);
  100.             linePrinter(ADD_MESSAGE, commandData, fileName);
  101.         }
  102.         else{
  103.             linePrinter(ERROR_CANNOT_ADD_MESSAGE, null, null);
  104.         }
  105.     }
  106.    
  107.     private static void deleteData(ArrayList<String> textFile, String commandEntered, String fileName){
  108.         String commandData = getCommandData(commandEntered);
  109.        
  110.         if(isEmptyCommandData(commandData)){
  111.             linePrinter(ERROR_CANNOT_DELETE_NULL_MESSAGE, null, null);
  112.         }
  113.         else{
  114.             try{
  115.                 int commandDataToInteger = Integer.parseInt(commandData);
  116.                 String deletedData = textFile.get(commandDataToInteger - 1);
  117.                 textFile.remove(commandDataToInteger - 1);
  118.                 linePrinter(DELETE_MESSAGE, fileName, deletedData);
  119.             } catch(IndexOutOfBoundsException e) {
  120.                 linePrinter(ERROR_CANNOT_DELETE_MESSAGE, commandData, fileName);
  121.             }
  122.         }      
  123.     }
  124.    
  125.     private static boolean isEmptyCommandData(String commandData){
  126.         if (commandData == null){
  127.             return true;
  128.         }
  129.         return false;
  130.     }
  131.    
  132.     private static void displayData(ArrayList<String> textFile, String fileName){
  133.         if (textFile.size() > 0){
  134.             printData(textFile);
  135.         }
  136.         else {
  137.             linePrinter(ERROR_IS_EMPTY_MESSAGE, fileName, null);
  138.         }
  139.     }
  140.    
  141.     private static void printData(ArrayList<String> textFile){
  142.         for (int counter = 0; counter < textFile.size(); counter++){
  143.             System.out.println((counter + 1) + ". " + textFile.get(counter));
  144.         }
  145.     }
  146.    
  147.     private static void clearData(ArrayList<String> textFile, String fileName){
  148.         if (isEmptyTextFile(textFile)){
  149.             linePrinter(ERROR_IS_EMPTY_MESSAGE, fileName, null);
  150.         }
  151.         else{
  152.             textFile.clear();
  153.             linePrinter(CLEAR_MESSAGE, fileName, null);
  154.         }      
  155.     }
  156.    
  157.     private static boolean isEmptyTextFile(ArrayList<String> textFile){
  158.         if (textFile.isEmpty()){
  159.             return true;
  160.         }
  161.         return false;
  162.     }
  163.    
  164.     private static void exit(){
  165.         System.exit(0);
  166.     }
  167.    
  168.     private static void saveDataToFile(ArrayList<String> textFile, String fileName, File outputFile){
  169.         try{
  170.             FileWriter writer = new FileWriter(outputFile);
  171.             saveData(writer, textFile, fileName, outputFile);
  172.             writer.close();
  173.         } catch (IOException e){
  174.             linePrinter(ERROR_CANNOT_SAVE_MESSAGE, fileName, null);
  175.         }
  176.     }
  177.    
  178.     private static void saveData(FileWriter writer, ArrayList<String> textFile, String fileName, File outputFile){
  179.         try{
  180.             for (int counter = 0; counter < textFile.size(); counter++){
  181.                 writer.write(textFile.get(counter) + "\r\n");
  182.             }
  183.         } catch (IOException e){
  184.             linePrinter(ERROR_CANNOT_SAVE_MESSAGE, fileName, null);
  185.         }
  186.     }
  187.    
  188.     private static void commandLinePrinter(String commandLine){
  189.         System.out.print(commandLine);
  190.     }
  191.    
  192.     private static void linePrinter(String textConstant, String variableText1, String variableText2){
  193.         String text = String.format(textConstant, variableText1, variableText2);
  194.         System.out.println(text);
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement