This repository was archived by the owner on Nov 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTextExcel.java
More file actions
70 lines (68 loc) · 2.55 KB
/
Copy pathTextExcel.java
File metadata and controls
70 lines (68 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//Written by Ryan Chan
//AP Computer Science 2016
import java.util.*;
import persistence.PersistenceHelper;
public class TextExcel {
public static int TABLE_X_SIZE=10; //Class constants for table size
public static int TABLE_Y_SIZE=10;
public static boolean running=true;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Table grid=new Table(TABLE_X_SIZE,TABLE_Y_SIZE); //Initialize table object
System.out.println("Welcome to TextExcel, by Ryan Chan");
while (running){ //Gets user input
System.out.print("Enter a command: ");
parseInput(sc.nextLine(),grid);
}
}
public static void parseInput(String input,Table grid){ //Acts on the user input
char number='.';
char firstLetter='.';
if (input.length()>1){ //Takes the first 2 characters of the string
firstLetter = input.charAt(0);
number = input.charAt(1);
}
if (input.length()<5){
for(int i = input.length();i<5;i++){
input+=" ";
}
}
if(input.substring(0,5).equals(("print"))){ //Prints table
grid.printTable();
} else if(input.substring(0,4).equals(("quit"))){ //Quits program
System.out.println("Thank you for using TextExcel!");
running=false;
} else if(input.contains(" = ")){ //Setting cells
grid.setCell(input.substring(0,input.indexOf(" = ")), input.substring(input.indexOf(" = ")+3));
} else if('A'<=firstLetter&&firstLetter<='Z'&&'0'<=number&&number<='9'&&input.indexOf("=")==-1){ //Displaying cells
System.out.println(input.substring(0,input.indexOf(" "))+"= "+grid.outputCell(input.substring(0,input.indexOf(" "))));
} else if(input.substring(0,5).equals("clear")){ //Clearing cells
if (input.contains(" ")){ //If a cell is specified, clears that cell
grid.clearCell(input.substring(6));
} else { //If no cell is specified, clears the entire table
grid.clear();
}
} else if (input.substring(0,4).equals("save")){ //Saving the table
try {
PersistenceHelper.save(input.substring(5), grid);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error while saving "+input.substring(5));
}
} else if (input.substring(0,4).equals("load")){ //Loading the table
try {
PersistenceHelper.load(input.substring(5), grid);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error while loading "+input.substring(5));
}
} else if (input.substring(0,4).equals("sort")){ //sorting the table
grid.sort(input);
} else if (input.substring(0,4).equals("help")){ //Help menu
Table.help(input);
}
else {
System.out.println("Command not recognized");
}
}
}