Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that does the following jobs. Step1. Creates a Car table wi

ID: 3590141 • Letter: W

Question

Write a Java program that does the following jobs.

Step1. Creates a Car table with car manufacturers, models, model years, and fuel efficiency ratings. Step2. Reads data from a text file and insert the cars to the table created in Step 1.
Step3. Interact with the user, where the user can select one of the following actions.

- (Q) Quit: quit the program

- (A) Add a car: insert a car to the table

- (C) Calculate avg: calculate the average

- (W) Write the entire table to a text file

- (P) Print the entire table

- (M) Print a subset of the rows based on efficiency

The user can choose one of these actions with typing Q,A,C, W, P, or M in the console. When adding a car, the user must provide manufacturer, model, year, and mpg through the console.

When writing the table to a text file, the user must provide output text file name. When selecting a subset of the rows based on efficiency an upper bound value must be provided.

To help you understand how the program works, a sample run is available in Appendix A. Four files are provided with the assignment.

CarDB.java: the main class of this assignment.

Database.properties: file for Apache Derby

SimpleDataSource.java: file for Apache Derby

Carmpg.txt: sample text file for Step 2.

SimpleDataSource.java is:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Properties;

/**

A simple data source for getting database connections.

*/

public class SimpleDataSource

{

private static String url;

private static String username;

private static String password;

/**

Initializes the data source.

@param fileName the name of the property file that

contains the database driver, URL, username, and password

*/

public static void init(String fileName) throws IOException,

ClassNotFoundException

{

//complete this method

}

/**

Gets a connection to the database.

@return the database connection

*/

public static Connection getConnection() throws SQLException

{

//complete this method

}

}

carmpg.txt is:

2017 Toyota Prius 52

2017 Kia Optima 31

2017 Hyundai Sonata 31

2017 Nissan Altima 31

2017 Chevrolet Malibu 30

2017 Honda Accord 30

2017 Mazda 6 29

2017 Subaru Legacy 29

2017 Toyota Camry 27

2017 Chrysler 200 27

2017 Ford Fusion 27

2017 Volkswagen Passat 27

2017 Volkswagen CC 25

2017 Chevrolet Impala 25

2017 Buick LaCrosse 25

2017 Nissan Maxima 25

2017 Buick Regal 24

Explanation / Answer

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.io.*;

public class MyClass {
public static void main(String args[]) {
try{
Connection con = DriverManager.getConnection( host, username, password );//making connecton to database
Statement stmt = con.createStatement( );
String sql = "CREATE TABLE CAR " +//create the table
"(MANUFACTURES VARCHAR(50) not NULL, " +
" MODELS VARCHAR(50) PRIMARY KEY, " +
" MODELYEAR INTEGER, " +
" FUELRATING INTEGER";

stmt.executeUpdate(sql);
do
{
//giving user options.
System.out.println("(Q) Quit: quit the program");
System.out.println("(A) Add a car: insert a car to the table");
System.out.println("(C) Calculate avg: calculate the average");
System.out.println("(W) Write the entire table to a text file");
System.out.println("(P) Print the entire table");
System.out.println("(M) Print a subset of the rows based on efficiency");
Scanner scanner = new Scanner(System.in);
String option = scanner.nextLine();
if(option == "Q")
System.exit(0);
if else(option == "A")
{
System.out.println("Enter car manufacturee name, model no., model year and fuel effficiency rating");
String manu = scanner.nextLine();
String model_no = scanner.nextLine();
int year = scanner.nextLine();
int rate = scanner.nextLine();
Statement s=c.createStatement();
s.executeUpdate("INSERT INTO CAR VALUE ('"+manu+"','"+model_no+"','"+year+"','"+rate+"')");
}
if else(option == "P")
{
String sql2 = "SELECT MANUFACTURES, MODELS, MODELYEAR, FUELRATING FROM CAR";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
String manu1 = rs.getString("MANUFACTURES");
String model1 = rs.getString("MODELS");
int year1 = rs.getInt("MODELYEAR");
int rate1 = rs.getInt("FUELRATING");

System.out.print(manu1);
System.out.print(" " + model1);
System.out.print(" " + year1);
System.out.println(" " + rate1);
}
rs.close();
}
if else(option == "W")
{
System.out.println("Enter file name");
String fileName = scanner.nextLine();
String sql2 = "SELECT MANUFACTURES, MODELS, MODELYEAR, FUELRATING FROM CAR";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
String manu1 = rs.getString("MANUFACTURES");
String model1 = rs.getString("MODELS");
int year1 = rs.getInt("MODELYEAR");
int rate1 = rs.getInt("FUELRATING");

String row = manu1+" "+model1+" "+Integer.toString(year1)+" "+Integer.toString(rate1)+" ";
}
InputStream istream;  
OutputStream ostream=null;
istream = System.in;  
File outFile = new File(fileName);   
ostream = new FileOutputStream(outFile);  
ostream.write(row);  
  
}
}while(option ! = "Q")
}
catch ( SQLException err ) {
System.out.println( err.getMessage( ) );
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote