-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrontendImplementation.java
More file actions
160 lines (143 loc) · 4.65 KB
/
Copy pathFrontendImplementation.java
File metadata and controls
160 lines (143 loc) · 4.65 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
// --== CS400 Fall 2023 File Header Information ==--
// Name: Cole Movsessian
// Email: movsessian@wisc.edu
// Group: C35
// TA: Alexander Peseckis
// Lecturer: Florian Heimerl
import java.io.IOException;
import java.util.Scanner;
import java.util.List;
public class FrontendImplementation implements FrontendInterface {
private Scanner scanner;
private BackendInterface backend;
/**
* Constructor for frontend interface
* @param backend interface with data
* @param scanner object
*/
public FrontendImplementation(BackendInterface backend, Scanner scanner) {
this.backend = backend;
this.scanner = scanner;
}
/**
* Begins command loop
*/
public void startCommandLoop(){
String cmd;
while(true){
displayMenu();
cmd = this.scanner.nextLine();
if(cmd.equals("4")){
exitApp();
break;
}else if(cmd.equals("1")){
loadFile();
break;
}else if(cmd.equals("2")){
listHighMeteorites();
break;
}else if(cmd.equals("3")){
listAllRange();
break;
}else{
System.out.println("Invalid command: try again\n");
}
}
}
/**
* Outputs readable menu
*/
public void displayMenu(){
System.out.println("Select Command");
System.out.println("==========================");
System.out.println("1) Load file");
System.out.println("2) List highest meteorites");
System.out.println("3) List within range");
System.out.println("4) Exit application");
}
/**
* Specifies and loads given file
*/
public void loadFile(){
try {
backend.insertFromCSV("meteorites.csv");
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("Enter file name:");
String fileName = scanner.nextLine();
System.out.println("Searching for file...");
try{
backend.readDataFromFile(fileName);
System.out.println(fileName + " loaded");
}catch(Exception e){
System.out.println("File not found");
}
startCommandLoop();
}
/**
* Lists meteorites with highest mass
*/
public void listHighMeteorites(){
try {
backend.insertFromCSV("meteorites.csv");
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("Printing meteorites with highest mass...");
displayResults(backend.getMeteoritesWithMaxMass());
}
/**
* Takes input parameters and lists meteorites within given range
* @throws IllegalArgumentException if given range is invalid
*/
public void listAllRange() throws IllegalArgumentException{
try {
backend.insertFromCSV("meteorites.csv");
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("Enter threshold in grams(low):");
double low = scanner.nextDouble();
System.out.println("Enter threshold in grams(high):");
double high = scanner.nextDouble();
while(low >= high){
System.out.println("Invalid range: try again");
System.out.println("Enter threshold in grams(low):");
low = scanner.nextDouble();
System.out.println("Enter threshold in grams(high):");
high = scanner.nextDouble();
}
System.out.println("Printing meteorites within range...");
displayResults(backend.getMeteoritesWithinMassRange(low,high));
}
/**
* Iterates through list and displays results following given command
*/
public void displayResults(List<Meteorite> data){
if(data == null){
System.out.println("No results in range");
}else {
for(int i = 0; i < data.size(); i++){
System.out.println(data.get(i).getName());
}
}
System.out.println("==========================");
startCommandLoop();
}
/**
* Terminates application processes and runs any desired closing operations
*/
public void exitApp() {
System.out.println("Exiting application...");
}
/**
* Initializes and runs command loop
*/
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
BackendImplementation back= new BackendImplementation();
FrontendImplementation front = new FrontendImplementation(back, scan);
front.startCommandLoop();
}
}