Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class TextBuddy {
- public static final String WELCOME_MESSAGE = "Welcome to TextBuddy! %1$s is ready for use!";
- public static final String COMMAND_MESSAGE = "command: ";
- public static final String ADD_MESSAGE = "Added %1$s to %2$s";
- public static final String DELETE_MESSAGE = "Deleted from %1$s: \"%2$s\"";
- public static final String CLEAR_MESSAGE = "All data cleared from %1$s";
- public static final String ERROR_IS_EMPTY_MESSAGE = "Error! %1$s is empty!";
- public static final String ERROR_CANNOT_ADD_MESSAGE = "Error! No data to add!";
- public static final String ERROR_CANNOT_DELETE_NULL_MESSAGE = "Error! No specified data to delete!";
- public static final String ERROR_CANNOT_DELETE_MESSAGE = "Error! Unable to delete %1$s from %2$s";
- public static final String ERROR_CANNOT_SAVE_MESSAGE = "Error! Cannot save data to ";
- public static final String ERROR_INVALID_COMMAND_MESSAGE = "Error! Invalid command!";
- public static final String STRING_ADD = "add";
- public static final String STRING_DELETE = "delete";
- public static final String STRING_CLEAR = "clear";
- public static final String STRING_DISPLAY = "display";
- public static final String STRING_EXIT = "exit";
- public static final int NOT_FOUND = -1;
- public static void main(String[] args){
- String fileName = args[0];
- File outputFile = new File(fileName);
- ArrayList<String> textFile = new ArrayList<String>();
- Scanner sc = new Scanner(System.in);
- linePrinter(WELCOME_MESSAGE, fileName, null);
- executeCommand(textFile, fileName, outputFile, sc);
- }
- // this method is the main driver for the program as
- // it reads inputs from the user or file and executes them
- private static void executeCommand(ArrayList<String> textFile, String fileName, File outputFile, Scanner sc){
- String commandEntered = null;
- do{
- commandLinePrinter(COMMAND_MESSAGE);
- commandEntered = sc.nextLine();
- runCommands(commandEntered, textFile, fileName);
- saveDataToFile(textFile, fileName, outputFile);
- } while (commandEntered != null);
- }
- private static void runCommands(String commandEntered, ArrayList<String> textFile, String fileName){
- if (commandEntered == null){
- return;
- }
- String commandWord = getCommandWord(commandEntered);
- switch (commandWord){
- case STRING_ADD:
- addData(textFile, commandEntered, fileName);
- break;
- case STRING_DELETE:
- deleteData(textFile, commandEntered, fileName);
- break;
- case STRING_DISPLAY:
- displayData(textFile, fileName);
- break;
- case STRING_CLEAR:
- clearData(textFile, fileName);
- break;
- case STRING_EXIT:
- exit();
- break;
- default:
- linePrinter(ERROR_INVALID_COMMAND_MESSAGE, null, null);
- break;
- }
- }
- private static String getCommandWord (String commandEntered){
- int indexOfFirstSpace = commandEntered.indexOf(' ');
- if (indexOfFirstSpace == NOT_FOUND){
- return commandEntered;
- }
- String commandWord = commandEntered.substring(0, indexOfFirstSpace);
- return commandWord;
- }
- private static String getCommandData(String commandEntered){
- int lengthOfCommand = commandEntered.length();
- int indexOfFirstSpace = commandEntered.indexOf(' ');
- if (indexOfFirstSpace == NOT_FOUND){
- return null;
- }
- String commandData = commandEntered.substring(indexOfFirstSpace + 1, lengthOfCommand);
- return commandData;
- }
- private static void addData(ArrayList<String> textFile, String commandEntered, String fileName){
- String commandData = getCommandData(commandEntered);
- if(commandData != null){
- textFile.add(commandData);
- linePrinter(ADD_MESSAGE, commandData, fileName);
- }
- else{
- linePrinter(ERROR_CANNOT_ADD_MESSAGE, null, null);
- }
- }
- private static void deleteData(ArrayList<String> textFile, String commandEntered, String fileName){
- String commandData = getCommandData(commandEntered);
- if(isEmptyCommandData(commandData)){
- linePrinter(ERROR_CANNOT_DELETE_NULL_MESSAGE, null, null);
- }
- else{
- try{
- int commandDataToInteger = Integer.parseInt(commandData);
- String deletedData = textFile.get(commandDataToInteger - 1);
- textFile.remove(commandDataToInteger - 1);
- linePrinter(DELETE_MESSAGE, fileName, deletedData);
- } catch(IndexOutOfBoundsException e) {
- linePrinter(ERROR_CANNOT_DELETE_MESSAGE, commandData, fileName);
- }
- }
- }
- private static boolean isEmptyCommandData(String commandData){
- if (commandData == null){
- return true;
- }
- return false;
- }
- private static void displayData(ArrayList<String> textFile, String fileName){
- if (textFile.size() > 0){
- printData(textFile);
- }
- else {
- linePrinter(ERROR_IS_EMPTY_MESSAGE, fileName, null);
- }
- }
- private static void printData(ArrayList<String> textFile){
- for (int counter = 0; counter < textFile.size(); counter++){
- System.out.println((counter + 1) + ". " + textFile.get(counter));
- }
- }
- private static void clearData(ArrayList<String> textFile, String fileName){
- if (isEmptyTextFile(textFile)){
- linePrinter(ERROR_IS_EMPTY_MESSAGE, fileName, null);
- }
- else{
- textFile.clear();
- linePrinter(CLEAR_MESSAGE, fileName, null);
- }
- }
- private static boolean isEmptyTextFile(ArrayList<String> textFile){
- if (textFile.isEmpty()){
- return true;
- }
- return false;
- }
- private static void exit(){
- System.exit(0);
- }
- private static void saveDataToFile(ArrayList<String> textFile, String fileName, File outputFile){
- try{
- FileWriter writer = new FileWriter(outputFile);
- saveData(writer, textFile, fileName, outputFile);
- writer.close();
- } catch (IOException e){
- linePrinter(ERROR_CANNOT_SAVE_MESSAGE, fileName, null);
- }
- }
- private static void saveData(FileWriter writer, ArrayList<String> textFile, String fileName, File outputFile){
- try{
- for (int counter = 0; counter < textFile.size(); counter++){
- writer.write(textFile.get(counter) + "\r\n");
- }
- } catch (IOException e){
- linePrinter(ERROR_CANNOT_SAVE_MESSAGE, fileName, null);
- }
- }
- private static void commandLinePrinter(String commandLine){
- System.out.print(commandLine);
- }
- private static void linePrinter(String textConstant, String variableText1, String variableText2){
- String text = String.format(textConstant, variableText1, variableText2);
- System.out.println(text);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement