Ejemplo n.º 1
0
 public static void parse(String command, Spreadsheet spreadsheet) { // method to parse user input
   if (command.length() == 2 || command.length() == 3) {
     System.out.println(command + " = " + spreadsheet.getValue(command) + "\n");
   } else if (command.substring(0, 4).equals("exit")) { // allows user to terminate program
     System.out.println("\nFarewell!");
     running = false;
   } else if (command.substring(0, 5).equals("print")) { // prints spreadsheet
     System.out.println();
     spreadsheet.drawSpreadsheet();
   } else if (command.contains(" = ")) { // set cell values
     spreadsheet.setValue(
         command.substring(0, command.indexOf(" = ")),
         command.substring(command.indexOf(" = ") + 3));
   } else if (command.substring(0, 5).equals("clear")) { // clears cells or spreadsheet
     if (command.length() > 5) { // clear specific cell
       spreadsheet.clearCell(command.substring(6));
     } else { // clears entire spreadsheet
       spreadsheet.clear();
     }
   } else if (command.subSequence(0, 4).equals("save")) { // saves spreadsheet to file
     try {
       PersistenceHelper.save(command.substring(5), spreadsheet);
     } catch (Exception e) { // catches file error
       System.out.println("Unable to save to path '" + command.substring(5) + "'\n");
     }
   } else if (command.subSequence(0, 4).equals("load")) { // loads spreadsheet from file
     try {
       PersistenceHelper.load(command.substring(5), spreadsheet);
     } catch (Exception e) { // catches file error
       System.out.println("Unable to load from path '" + command.substring(5) + "'\n");
     }
   } else if (command.substring(0, 5).equals("clash")) {
     try {
       spreadsheet.clash(command.substring(6));
     } catch (IOException | ClashException e) {
       System.out.println(
           "'" + command.substring(6) + "' is not recognized as a valid clan tag.\n");
     }
   } else if (command.substring(0, 7).equals("quizlet")) {
     try {
       spreadsheet.quizlet(command.substring(8));
     } catch (IOException e) {
       System.out.println(
           "Something went wrong when reading Quizlet set #" + command.substring(8) + "\n");
     }
   } else if (command.substring(0, 4).equals("sort")) {
     spreadsheet.sort(command);
   } else if (command.substring(0, 6).equals("resize")) {
     spreadsheet.resize(command.substring(7));
   } else { // notifies user of invalid command
     System.out.println("'" + command + "' is not recognized as a valid command.\n");
   }
 }