Question
**THIS IS A JAVA PROBLEM, NEEDS TO BE DONE WITH ECLIPSE!** Write a Java program that does the following jobs. Step1. Creates a Car table
**THIS IS A JAVA PROBLEM, NEEDS TO BE DONE WITH ECLIPSE!**
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. (I WILL POST THIS AT THE BOTTOM OF THIS PAGE)
Four files are provided with the assignment.
**THESE FILES ARE LISTED BELOW, AND MUST MUST MUST BE INCLUDED IN THE CODE, NO EXCEPTIONS**
1. CarDB.java: the main class of this assignment.
2. Database.properties: file for Apache Derby
3. SimpleDataSource.java: file for Apache Derby
4. Carmpg.txt: sample text file for Step 2.
Provide one source file (CarDB.java). In other words, Database.properties, SimpleDataSource.java, or Carmpg.txt are not submitted. In modifying the source file,
1. Do not change the file (class) names. Points will be deducted if you have different names.
2. Fill in your name in the @author section of the comment in each of the files. If you do not, points will be deducted.
***CarDB.java file***
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.IOException;
/**
* Code for HW5
* @author
*/
/**
Creates the Car table, insert some data into the table,
and drop the table from a database.
*/
//ARGS database.properties
public class CarDB
{
public static void main(String[] args) throws IOException, SQLException,
ClassNotFoundException
{
if (args.length == 0)
{
System.out.println("Usage: java CarDB propertiesFile");
System.exit(0);
}
SimpleDataSource.init(args[0]);
...
}
}
***Database.properties***
jdbc.url=jdbc:derby:BigJavaDB;create=true # With other databases, you may need to add entries such as these # jdbc.username=admin # jdbc.password=secret # jdbc.driver=org.apache.derby.jdbc.EmbeddedDriver
SimpleDataSource.java
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***
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
*****REMINDER!! ALL OF THESE FILES MUST BE ABLE TO BE USED IN THE ASSIGNMENT, NO EXCEPTIONS*****
**APPENDIX A: SAMPLE RUN (FROM ABOVE)**
Appendix A: sample run Select from the following options (Q) Quit (A) Add a car (C) Calculate 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 C Average fuel efficiency: 29.1176 Select from the following options (Q) Quit (A) Add a car (C) Calculate 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 A Manufacturer name: Toyota Model name: Corolla Year: 2017 MPG: 32 Select from the following options (Q) Quit (A) Add a car (C) Calculate 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 C Average fuel efficiency: 29.2777 Select from the following options (Q) Quit (A) Add a car (C) Calculate 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 W Output file name: cartable.txt Select from the following options (Q) Quit (A) Add a car (C) Calculate 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 P Manufacturer Model ModelYear Efficiency Toyota Prius 2017 52 Kia Optima 2017 31 Hyundai Sonata 2017 31 Nissan Altima 2017 31 Chevrolet Malibu 2017 30 Honda Accord 2017 30 Mazda 6 2017 29 Subaru Legacy 2017 29 Toyota Camry 2017 27 Chrysler 200 2017 27 Ford Fusion 2017 27 Volkswagen Passat 2017 27 Volkswagen CC 2017 25 Chevrolet Impala 2017 25 Buick LaCrosse 2017 25 Nissan Maxima 2017 25 Buick Regal 2017 24 Toyota Corolla 2017 32 Select from the following options (Q) Quit (A) Add a car (C) Calculate 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 M Upper bound on efficiency (MPG):25 Manufacturer Model ModelYear Efficiency Volkswagen CC 2017 25 Chevrolet Impala 2017 25 Buick LaCrosse 2017 25 Nissan Maxima 2017 25 Buick Regal 2017 24 Select from the following options (Q) Quit (A) Add a car (C) Calculate 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
THANK YOU!!
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