-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject1.java
134 lines (130 loc) · 4.52 KB
/
Project1.java
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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Scanner;
class Project1 {
// should read from input file line by line
// write to output file
// add input and output to the program arguments
public static void main(String[] args) {
FactoryImpl myFactory = new FactoryImpl();
try {
FileWriter myWriter = new FileWriter(args[1]);
try {
File myObj = new File(args[0]);
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
String[] input = data.split(" ");
String func = input[0];
if (func.equals("AF")) {
Product product = new Product(Integer.parseInt(input[1]), Integer.parseInt(input[2]));
myFactory.addFirst(product);
} else if (func.equals("AL")) {
Product product = new Product(Integer.parseInt(input[1]), Integer.parseInt(input[2]));
myFactory.addLast(product);
} else if (func.equals("A")) {
try {
Product product = new Product(Integer.parseInt(input[2]), Integer.parseInt(input[3]));
myFactory.add(Integer.parseInt(input[1]), product);
} catch (IndexOutOfBoundsException e) {
myWriter.write("Index out of bounds.");
myWriter.write("\n");
}
} else if (func.equals("RF")) {
try {
myWriter.write(myFactory.removeFirst().toString());
myWriter.write("\n");
} catch (NoSuchElementException e) {
myWriter.write("Factory is empty.");
myWriter.write("\n");
}
} else if (func.equals("RL")) {
try {
myWriter.write(myFactory.removeLast().toString());
myWriter.write("\n");
} catch (NoSuchElementException e) {
myWriter.write("Factory is empty.");
myWriter.write("\n");
}
} else if (func.equals("RI")) {
try {
myWriter.write(myFactory.removeIndex(Integer.parseInt(input[1])).toString());
myWriter.write("\n");
} catch (IndexOutOfBoundsException e) {
myWriter.write("Index out of bounds.");
myWriter.write("\n");
}
} else if (func.equals("RP")) {
try {
myWriter.write(myFactory.removeProduct(Integer.parseInt(input[1])).toString());
myWriter.write("\n");
} catch (NoSuchElementException e) {
myWriter.write("Product not found.");
myWriter.write("\n");
}
} else if (func.equals("F")) {
try {
myWriter.write(myFactory.find(Integer.parseInt(input[1])).toString());
myWriter.write("\n");
} catch (NoSuchElementException e) {
myWriter.write("Product not found.");
myWriter.write("\n");
}
} else if (func.equals("G")) {
try {
myWriter.write(myFactory.get(Integer.parseInt(input[1])).toString());
myWriter.write("\n");
} catch (IndexOutOfBoundsException e) {
myWriter.write("Index out of bounds.");
myWriter.write("\n");
}
} else if (func.equals("U")) {
try {
myWriter.write(myFactory.update(Integer.parseInt(input[1]), Integer.parseInt(input[2]))
.toString());
myWriter.write("\n");
} catch (NoSuchElementException e) {
myWriter.write("Product not found.");
myWriter.write("\n");
}
} else if (func.equals("FD")) {
myWriter.write(String.valueOf(myFactory.filterDuplicates()));
myWriter.write("\n");
} else if (func.equals("R")) {
myFactory.reverse();
myWriter.write(myFactory.print());
myWriter.write("\n");
} else if (func.equals("P")) {
myWriter.write(myFactory.print());
myWriter.write("\n");
}
// myWriter.write("\n");
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
myWriter.close();
// System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
// FactoryImpl myFactory2 = new FactoryImpl();
// Product a = new Product(1, 1);
// Product b = new Product(2, 2);
// Product c = new Product(3, 3);
// Product z = new Product(100, 100);
// myFactory2.addLast(a);
// myFactory2.addLast(b);
// myFactory2.addLast(c);
// myFactory2.addLast(z);
// System.out.println(myFactory2.print());
// myFactory2.reverse2();
// System.out.println(myFactory2.print());
}
}