-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextBuddy.java
More file actions
191 lines (156 loc) · 4.47 KB
/
Copy pathTextBuddy.java
File metadata and controls
191 lines (156 loc) · 4.47 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* author: zhang chenwei
* This is to help user write, delete, clear and display content in file
* File will be saved when user exit the program.Sample format is as below:
*
* c:>java TextBuddy TestInput.txt
* Welcome to TextBuddy. TestInput.txt is ready for use
* command: add jumped over the moon
* added to TestInput.txt : jumped over the moon
* command: display
* 1. jumped over the moon
* command:delete 1
* deleted from TestInput.txt: jumped over the moon
* command:display
* TestInput.txt is empty
* command: clear
* all content deleted from TestInput.txt
* command: exit
*
* c:>
* */
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class TextBuddy {
public static String fileName;
public static ArrayList<String> content;
//assume input format is correct eg: delete 1
//Content in input file will be kept instead of being overwrite
public static void main(String[] argv) throws Exception{
if(argv.length != 1) {
System.err.println("Invalid command line, exactly one argument required");
System.exit(1);
}
fileName = argv[0];
System.out.println("Welcome to TextBuddy."+fileName+" is ready for use. ");
content = readFileToList();
BufferedReader readFromKeyboard = new BufferedReader(new InputStreamReader(System.in));//read from keyboard;
while (true) {
System.out.print("command: ");
StringTokenizer st = new StringTokenizer(readFromKeyboard.readLine());
String command = st.nextToken();
executeCommand(st, command);
}
}
public static void executeCommand(StringTokenizer st, String command)
throws Exception {
switch(command.toLowerCase()){
case "add": {
doAdd(st);
break;
}
case "display": {
doDisplay();
break;
}
case "delete": {
doDelete(st);
break;
}
case "clear": {
doClear();
break;
}
case "sort": {
/*doSort();*/
break;
}
case "search":{
/*doSearch(st); */
break;
}
case "exit": {
doExit();
}
default :
System.out.println("WTF IS THIS COMMAND?!");
}
}
/**************************************** level 2**********************************************************************************************************/
public static void doExit() throws Exception {
writeIntoFile();
System.exit(0);
}
public static void doAdd(
StringTokenizer st) {
String whatToAdd = st.nextToken();
while(st.hasMoreElements())
whatToAdd+=" "+st.nextToken();
content.add(whatToAdd);
System.out.println("added to "+fileName+": "+whatToAdd);
}
public static void doDisplay() {
if(content.size() ==0)
System.out.println(fileName+" is empty. ");
else{
int j = 1;
for(String line: content){
System.out.println(j+". "+line);
j++;
}
}
}
public static void doDelete(
StringTokenizer st) {
String lineToDelete = st.nextToken();
//there is no line to delete
if(lineToDelete.length()!=0){
String whatRemoved = content.remove(Integer.parseInt(lineToDelete)-1);
System.out.println("deleted from "+fileName+": "+whatRemoved);
}
else
System.out.println("Which line to delete?");
}
public static void doClear() {
content.clear();
System.out.println("all content deleted from "+fileName);
}
public static void doSort() {
Collections.sort(content);
System.out.println(fileName+" sorted.");
}
public static void doSearch(StringTokenizer st) {
int j = 1;
String searchingFor = st.nextToken();
for(String line: content){
if(line.contains(searchingFor)){
System.out.println(j + ". "+line);
j++;
}
}
}
/**************************************** level 3**********************************************************************************************************/
//Read the content of the input file into an arraylist of string
public static ArrayList<String> readFileToList() throws Exception{
String line = null;
ArrayList<String> content = new ArrayList<>();
BufferedReader readFromFile = new BufferedReader(new FileReader(fileName));
while ((line = readFromFile.readLine()) != null)
content.add(line);
readFromFile.close();
return content;
}
//Write from the ArrayList into the inputfile
public static void writeIntoFile() throws Exception {
PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
for(String line: content)
pw.println(line);
pw.close();
}
}