-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackendImplementation.java
More file actions
162 lines (140 loc) · 5.46 KB
/
Copy pathBackendImplementation.java
File metadata and controls
162 lines (140 loc) · 5.46 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
// --== CS400 Fall 2023 File Header Information ==--
// Name: Arnav Srivastav
// Email: asrivastav32@wisc.edu
// Group: C35
// TA: Alexander Peseckis
// Lecturer: Florian Heimerl
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
public class BackendImplementation implements BackendInterface {
private IterableMultiKeyRBT<Meteorite> meteoriteTree = new IterableMultiKeyRBT<>();
public IterableMultiKeyRBT<Meteorite> getMeteoriteTree() {
return meteoriteTree;
}
/**
* Read meteorite data from a file and populate the meteorite tree.
*
* @param filePath The path to the data file.
* @throws FileNotFoundException If the file is not found.
* @throws IOException If an I/O error occurs.
*/
@Override
public void readDataFromFile(String filePath) throws FileNotFoundException, IOException {
// Check if the file exists
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException("File not found: " + filePath);
}
// Clear the existing data
getMeteoriteTree().clear();
// Read data from the file and populate meteoriteTree
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
// Skip the header line
reader.readLine();
while ((line = reader.readLine()) != null) {
// Split the line into fields
String[] datas = line.split("\"");
String[] data = datas[0].split(",");
String name = data[0];
// Parse latitude and longitude
double latitude = 0.0;
double longitude = 0.0;
try {
String[] latLon = datas[1].substring(1, datas[1].length() - 1).split(",");
latitude = Double.parseDouble(latLon[0]);
longitude = Double.parseDouble(latLon[1]);
} catch (NumberFormatException | StringIndexOutOfBoundsException e) {
continue; // Skip invalid entries
}
// Parse fall
boolean fall = data[5].equals("\"Fell\"");
// Parse mass
double mass = 0.0;
try {
mass = Double.parseDouble(data[4]);
} catch (NumberFormatException e) {
continue; // Skip invalid entries
}
// Insert the meteorite into the tree
this.meteoriteTree.insertSingleKey(new MeteoriteImpl(name, latitude, longitude, fall, mass));
}
reader.close();
}
/**
* Insert meteorite data from a CSV file into the meteorite tree.
*
* @param fileName The name of the CSV file.
* @return True if insertion is successful, false otherwise.
* @throws IOException If an I/O error occurs.
*/
@Override
public boolean insertFromCSV(String fileName) throws IOException {
// Implement inserting data from a CSV file into the meteoriteTree
// You can reuse the readDataFromFile method.
try {
readDataFromFile(fileName);
return true;
} catch (FileNotFoundException e) {
System.err.println("File not found: " + e.getMessage());
e.printStackTrace();
return false;
} catch (IOException e) {
System.err.println("An error occurred during insertion: " + e.getMessage());
e.printStackTrace();
return false;
}
}
/**
* Get meteorites with the maximum mass.
*
* @return A list of meteorites with the maximum mass.
*/
@Override
public List<Meteorite> getMeteoritesWithMaxMass() {
if (getMeteoriteTree().isEmpty()) {
return new ArrayList<>();
}
double maxMass = Double.MIN_VALUE; // Initialize maxMass to the smallest possible value
List<Meteorite> meteorites = new ArrayList<>();
for (Meteorite meteorite : getMeteoriteTree()) {
double mass = meteorite.getMass();
if (mass > maxMass) {
maxMass = mass;
meteorites.clear(); // Clear the list because a new maximum is found
meteorites.add(meteorite);
} else if (mass == maxMass) {
meteorites.add(meteorite); // Add to the list if mass is equal to the current maximum
}
}
return meteorites;
}
/**
* Get meteorites within a specified mass range.
*
* @param minMass The lower bound of the mass range.
* @param maxMass The upper bound of the mass range.
* @return A list of meteorites within the specified mass range.
*/
@Override
public List<Meteorite> getMeteoritesWithinMassRange(double minMass, double maxMass) {
// Implement getting meteorites within a specific mass range from the meteoriteTree
List<Meteorite> meteorites = new ArrayList();
for (Meteorite meteorite : this.meteoriteTree) {
double mass = meteorite.getMass();
if (mass >= minMass && mass <= maxMass) {
meteorites.add(meteorite);
}
}
return meteorites;
}
public static void main(String[] args) {
BackendImplementation backend = new BackendImplementation();
Scanner scnr = new Scanner(System.in);
FrontendImplementation frontend = new FrontendImplementation(backend,scnr);
// Start the main command loop of the frontend
frontend.startCommandLoop();
}
}