Create a GUI interface
1. When the program starts the user needs to be asked if they want to make a new entry or to
view a previous entry
2. If the user wants to make a new entry, the first question will be how many meters they
travelled (this will then need to be converted into kilometers)
3. The second question will give the user 3 options to choose from, and each option will have a
value. The options are as follows:
a. Hatchback = 3
b. SUV = 3.5
c. Sports car = 4.
When the user has selected an option that options value needs to be multiplied by the
distance they travelled in kilometers
4. The third question will allow the user to enter a description, of where they travel to and why
did they travel there.
5. All the information above needs to then be saved into a JSON file
6. If the user says he want to view a previous entry the JSON file needs to be loaded and
displayed.
7. You need to make use of error handling to ensure that the user only types in items that are
asked and not anything else
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
import org.json.simple.JSONObject;
import java.io.FileNotFoundException;
import java.io.FileReader;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class CarsApp {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
int choice = Integer.parseInt(JOptionPane.showInputDialog("1. Make a new entry\n"+"2. View a previous entry\n"+"Your choice: "));
if (choice == 1) {
double meters = Double.parseDouble(JOptionPane.showInputDialog("How many meters they travelled? "));
double kilometers = meters / 1000.0;
String carType = JOptionPane.showInputDialog("a. Hatchback = 3\n"+"b. SUV = 3.5\n"+"c. Sports car = 4\n"+"Your choice: ");
double total = 0.0;
if (carType.compareToIgnoreCase("a") == 0) {
total = kilometers * 3;
} else if (carType.compareToIgnoreCase("b") == 0) {
total = kilometers * 3.5;
} else if (carType.compareToIgnoreCase("c") == 0) {
total = kilometers * 4;
}
String description = JOptionPane.showInputDialog(String.format("The total: %.2f\n", total)+"\n\nEnter description, of where you travel to and why did you travel there: ");
JSONObject JSON_Object = new JSONObject();
JSON_Object.put("Total", total);
JSON_Object.put("Description", description);
try {
File entryFile = new File("entry.json");
entryFile.createNewFile();
FileWriter fileWriter = new FileWriter(entryFile);
fileWriter.write(JSON_Object.toJSONString());
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
JSONParser JSON_Parser = new JSONParser();
try {
Object obj = JSON_Parser.parse(new FileReader("entry.json"));
JSONObject jsonObject = (JSONObject) obj;
double total = (double) jsonObject.get("Total");
String description = (String) jsonObject.get("Description");
JOptionPane.showMessageDialog(null,String.format("The total: %.2f\nDescription: %s", total,description));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
}
Comments
Leave a comment