+1 (315) 557-6473 

Create A Program to Create Car Warm Up System in Java Assignment Solution.


Instructions

Objective
Write a java assignment program to create car warm up system.

Requirements and Specifications

program to create car warm up system in java program to create car warm up system in java 1

Source Code

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

public class App {

// Method to ask user for model

public static String getModel(Scanner sc)

{

System.out.print("Enter model: ");

return sc.nextLine();

}

// Method to ask for car's release year

public static int getReleaseYear(Scanner sc)

{

// We want to validate that the user enters an integer and not a string, so we use a try-catch block

// We also use a while-loop so if user enters an invalid input, the function will ask again

while(true)

{

try{

System.out.print("Enter release year: ");

int year = Integer.valueOf(sc.nextLine());

if(year > 0) // is positive

{

return year;

}

else {

System.out.println("The release year must be positive.");

}

}

catch(Exception ex)

{

// The only way to get an exception is if the Integer.valueOf function fails, and that happens

// when the user enters an non-numeric input

System.out.println("Please enter a valid release year.");

}

}

}

// Method to ask for car's trim

public static String getTrim(Scanner sc)

{

System.out.print("Enter car's trim: ");

return sc.nextLine().strip();

}

// Method to ask for type of car

public static String getType(Scanner sc)

{

// Define an Array with the valid types

String[] valid_types = {"truck", "sedan", "coupe", "station wagon", "van"};

// We use a while-loop so if user enters an invalid input, the function will ask again

while(true)

{

System.out.print("Enter car type: ");

String car_type = sc.nextLine().strip().toLowerCase();

// Check if the car_type is in the arra valid_types

boolean valid = false;

for(String s: valid_types) {

if(s.compareTo(car_type) == 0) {

valid = true;

break;

}

}

if(valid) {

return car_type;

}

else {

System.out.println("Please enter a valid car type.");

}

}

}

// Method to get MPG

public static int getMPG(Scanner sc)

{

// We want to validate that the user enters an integer and not a string, so we use a try-catch block

// We also use a while-loop so if user enters an invalid input, the function will ask again

while(true)

{

try{

System.out.print("Enter MPG: ");

int mpg = Integer.valueOf(sc.nextLine().strip());

if(mpg > 0) // is positive

{

return mpg;

}

else {

System.out.println("The miles-per-gallon value must be positive.");

}

}

catch(Exception ex)

{

// The only way to get an exception is if the Integer.valueOf function fails, and that happens

// when the user enters an non-numeric input

System.out.println("Please enter a valid release MPG value.");

}

}

}

/*

************************

****** PART 2 **********

************************

*/

public static String summarizeInfo(String model, int release_year, String trim, String car_type, int mpg)

{

/*

This function takes all car's info and returns it all in a single string

*/

return String.format("Model: %s\nRelease Year: %d\nTrim: %s\nType: %s\nMiles-per-gallon: %d\n", model, release_year, trim, car_type, mpg);

}

public static void saveCarInfo(String info) throws IOException

{

/*

This function saves the car info into a file named 'car.txt'

*/

// use a try-catch block

try

{

BufferedWriter writer = new BufferedWriter(new FileWriter("car.txt"));

writer.write(info);

writer.close();

}

catch(IOException ex) {

System.out.println("Could not write info to file car.txt");

}

}

public static void readCarInfo()

{

/*

This function reads the car info into from file named 'car.txt'

*/

try

{

BufferedReader reader = new BufferedReader(new FileReader("car.txt"));

String line;

while((line = reader.readLine()) != null) // while there is a line to be read

{

System.out.println(line);

}

reader.close();

}

catch(IOException ex) {

System.out.println("Could not read info from file car.txt");

}

}

public static void main(String[] args) throws Exception {

// First, create Scanner

Scanner sc = new Scanner(System.in);

// Ask for model

String model = getModel(sc);

// Ask for release year

int year = getReleaseYear(sc);

// Ask for trim

String trim = getTrim(sc);

// Type

String car_type = getType(sc);

//get MPG

int mpg = getMPG(sc);

// Now, print info

String info = summarizeInfo(model, year, trim, car_type, mpg);

// Save to file

saveCarInfo(info);

// read from file

readCarInfo();

// Close scanner

sc.close();

}

}