forked from jayeshsadhwani99/StarDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStar.java
228 lines (204 loc) · 8.79 KB
/
Star.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.io.*;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.FileWriter;
import java.util.*;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class Star {
boolean check_if_id_exists = true;
int star_id;
String star_name;
float star_temp;
String star_class;
Scanner scannerObj = new Scanner(System.in);
public void addStar() {
// Assign a random value to the ID
while(check_if_id_exists) {
star_id = ThreadLocalRandom.current().nextInt(1000, 10000);
// Check if ID already exists
File star_file = new File("./database/"+star_id+".txt");
if(!star_file.exists()) {
check_if_id_exists = false;
}
}
// Get the info of the star
System.out.println("What's the name of the star?");
star_name = scannerObj.nextLine();
System.out.println("What's the spectral class of the star?");
star_class = scannerObj.nextLine();
System.out.println("What's the Temperature of the star(in Kelvin)?");
star_temp = scannerObj.nextFloat();
// Write to database
try {
FileWriter fileWriter = new FileWriter("./database/"+star_id+".txt");
fileWriter.write("ID: " + star_id + "\nName: " + star_name + "\nTemperature: "
+ star_temp + "\nSpectral Class: " + star_class);
fileWriter.close();
} catch (IOException e ) {
System.out.println("An error occurred.");
e.printStackTrace();
}
// Clear the console
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("\nStar Created-");
System.out.println("ID: " + star_id + "\nName: " + star_name + "\nTemperature: "
+ star_temp + "\nSpectral Clas: " + star_class);
}
public void viewStar(int id) throws InputMismatchException {
File directory = new File("./database");
int flag = 0;
String[] stars_list = directory.list();
if (stars_list.length == 0) {
System.out.println("There are no stars to show.");
} else {
// Linear search in the array
for (int i = 0; i < stars_list.length; i++) {
String filename = stars_list[i];
if (filename.equalsIgnoreCase(id + ".txt")) {
// Clear the console
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("**********STAR FOUND************");
flag = 1;
// Read the file
try {
File fileObj = new File("./database/" + filename);
Scanner fileReader = new Scanner(fileObj);
while (fileReader.hasNextLine()) {
String data = fileReader.nextLine();
System.out.println(data);
}
System.out.println("**********************************\n");
fileReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
// If file not found
if (flag == 0) {
System.out.println("Could not find any star with the ID " + id);
}
}
}
public void updateStar(int id) {
File directory = new File("./database");
int flag = 0;
String[] stars_list = directory.list();
if (stars_list.length == 0) {
System.out.println("There are no stars in the Database.");
} else {
// Linear search in the array
for (int i = 0; i < stars_list.length; i++) {
String filename = stars_list[i];
if (filename.equalsIgnoreCase(id + ".txt")) {
// Clear the console
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("**********STAR FOUND************\nCurrent Details: ");
flag = 1;
// Read the file
try {
File fileObj = new File("./database/" + filename);
Scanner fileReader = new Scanner(fileObj);
while (fileReader.hasNextLine()) {
String data = fileReader.nextLine();
System.out.println(data);
}
System.out.println("\nEdit Details:");
// Edit the file
System.out.println("What's the name of the star?");
star_name = scannerObj.nextLine();
// Write to database
try {
FileWriter fileWriter = new FileWriter("./database/"+id+".txt");
fileWriter.write("ID: " + star_id + "\nName: " + star_name + "\nTemperature: "
+ star_temp + "\nSpectral Clas: " + star_class);
fileWriter.close();
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
fileReader.close();
// Clear the console
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("Star with the ID " + id + " was updated.\n");
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
// If file not found
if (flag == 0) {
System.out.println("Could not find any star with the ID " + id);
}
}
}
public void deleteStar(int id) {
File directory = new File("./database");
int flag = 0;
String[] stars_list = directory.list();
if (stars_list.length == 0) {
System.out.println("There are no stars to show.");
} else {
// Linear search in the array
for (int i = 0; i < stars_list.length; i++) {
String filename = stars_list[i];
if (filename.equalsIgnoreCase(id + ".txt")) {
// Clear the console
System.out.print("\033[H\033[2J");
System.out.flush();
flag = 1;
// Delete the file
File fileObj = new File("./database/" + filename);
if(fileObj.delete()) {
System.out.println("Star with the id " + id + "deleted.\n");
} else {
System.out.println("Could not delete file.");
}
}
}
// If file not found
if (flag == 0) {
System.out.println("Could not find any star with the ID " + id);
}
}
}
public void viewAllStars() {
File directory = new File("./database");
String[] stars_list = directory.list();
if (stars_list.length == 0) {
System.out.println("There are no stars to show.\n");
} else {
// Clear the console
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("**********" + stars_list.length + " STARS FOUND************");
// Linear search in the array
for (int i = 0; i < stars_list.length; i++) {
String filename = stars_list[i];
// Read the files
try {
File fileObj = new File("./database/" + filename);
Scanner fileReader = new Scanner(fileObj);
while (fileReader.hasNextLine()) {
String data = fileReader.nextLine();
System.out.println(data);
}
System.out.println("**********************************\n");
fileReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
}
}