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.
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started