Answer to Question #232319 in Java | JSP | JSF for Siya

Question #232319

You will need to complete the following objectives as a Java Application: 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.


1
Expert's answer
2021-09-02T02:36:29-0400
Download JSON.simple jar from https://code.google.com/archive/p/json-simple/downloads.

Java code:

import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
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) {


		Scanner input = new Scanner(System.in);
		// 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
		System.out.println("1. Make a new entry");
		System.out.println("2. View a previous entry");
		System.out.print("Your choice: ");
		int ch = input.nextInt();
		// 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
		if (ch == 1) {
			System.out.print("How many meters they travelled? ");
			double meters = input.nextDouble();
			double kilometers = meters / 1000.0;
			// 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
			System.out.println("a. Hatchback = 3");
			System.out.println("b. SUV = 3.5");
			System.out.println("c. Sports car = 4");
			System.out.print("Your choice: ");
			input.nextLine();
			String carType = input.nextLine();
			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;
			}
			// 4. The third question will allow the user to enter a description, of where
			// they travel to and why did they travel there.
			System.out.printf("The total: %.2f\n", total);
			System.out.print("Enter description, of where you travel to and why did you travel there: ");
			String description = input.nextLine();
			// 5. All the information above needs to then be saved into a JSON file
			JSONObject JSON_Object = new JSONObject();
			JSON_Object.put("Total", total);
			JSON_Object.put("Description", description);


			try {


				// Writing to a file
				File file = new File("entry.json");
				file.createNewFile();
				FileWriter fileWriter = new FileWriter(file);
				fileWriter.write(JSON_Object.toJSONString());
				fileWriter.flush();
				fileWriter.close();


			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			// 6. If the user says he want to view a previous entry the JSON file needs to
			// be loaded and displayed.


			JSONParser parser = new JSONParser();
			try {
				Object obj = parser.parse(new FileReader("entry.json"));
				JSONObject jsonObject = (JSONObject) obj;
				double total = (double) jsonObject.get("Total");
				String description = (String) jsonObject.get("Description");
				System.out.printf("The total: %.2f\n", total);
				System.out.println("Description: " + description);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}


		// close file
		input.close();
	}
}







Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS