Word Problem 2 (25 points) You\'ve taked a part-time job as a used car salespers
ID: 3596317 • Letter: W
Question
Word Problem 2 (25 points) You've taked a part-time job as a used car salesperson. Your employer, Discount Dave's Used Cars, has the following cars on his lot YEAR MAKE MODEL MILEAGE LIST PRICE 2015 Chevy Nova 40000 $18950.00 2013 Chevy Astro 80000 $17950.00 2017 Toyota Rav4 50000 $22950.00 2014 Toyota Supra 110000 $16950.00 Write a class called UsedCarlookup that asks the customer to select a car manufacturer, a maximum mileage, and a maximum list price. The program then checks if a car matching that description is available using the table above, then prints ether the matching carts), or the message "No cars matching that description are available" For example, if a customer asks for a Chevy with 50000 miles or less for a list price of $20000 or less, your program would report that a 2015 Chevy Nova with 40000 miles is available for $18950.00. However, if a customer asks for a Toyota, with 100000 miles or less for a list price of $20000 or less, your program would report the message "No cars matching that description are available" You may use either a Scanner or JOptionPane to read user input. The program performs no input validation. Write the code to minimize the number of paths in the conditional logic. The message "No cars matching that description are available" should be printed at only one place in your codeExplanation / Answer
This program will work only you have the mysql table with the details inserted into that you have to replace the databasename,username ,and password .
import java.sql.*;
import java.util.Scanner;
class UsedCarLookup{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/databasename";
// Database credentials
static final String USER = "your username";
static final String PASS = "your password";
public static void main(String[] args) {
String manufacturer = null;
long maxMile = 0;
long maxPrice = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the manufacterer ... ");
manufacturer = sc.next();
System.out.println("Enter The maximum miles ... ");
maxMile = (long)sc.nextInt();
System.out.println("Enter The Maximum List price ... ");
maxPrice = (long)sc.nextInt();
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM cardetails where make ="+manufacturer+" AND price < "+maxPrice+" AND mileage <"+maxMile;
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
if(!rs.hasNext()){
System.out.println("No cars matching description is found .... ");
}
while(rs.next()){
//Retrieve by column name
System.out.println("a "+rs.getInt("year")+" "+rs.getString("make")+" "+rs.getString("model")+" with "+rs.getInt("mileage")+" miles is available for $"+rs.getInt("price"));
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.