Answered step by step
Verified Expert Solution
Question
1 Approved Answer
hi i need help im new to java im trying to run my code that will show the output of my sort price button and
hi i need help im new to java im trying to run my code that will show the output of my sort price button and search button from eclipse that save to my database table vehicle but it will show car not found for example im trying to search KIA but the output show car not found this my code: public class AddCarFormJava extends JFrame
private JTextField makeField;
private JTextField modelField;
private JTextField yearField;
private JTextField priceField;
private List vehicleList new ArrayList;
public AddCarFormJava
setTitleAdd Car";
setSize;
setDefaultCloseOperationJFrameDISPOSEONCLOSE;
JPanel panel new JPanel;
panel.setLayoutnew GridLayout;
JLabel makeLabel new JLabelMake:;
makeField new JTextField;
JLabel modelLabel new JLabelModel:;
modelField new JTextField;
JLabel yearLabel new JLabelYear: ;
yearField new JTextField;
JLabel priceLabel new JLabelPrice:;
priceField new JTextField;
JButton addButton new JButtonAdd Car";
addButton.addActionListenernew ActionListener
public void actionPerformedActionEvent e
your existing action listener code
try
String make makeField.getText;
String model modelField.getText;
int year Integer.parseIntyearFieldgetText;
double price Double.parseDoublepriceFieldgetText;
Assert that the price is nonnegative
assert price : "Price should be nonnegative";
Get the database connection
Connection connection getDatabaseConnection;
Insert the new car into the database
if connection null
try
database insertion code here
String sql "INSERT INTO Vehicle make model, year, price VALUES ;
try PreparedStatement statement connection.prepareStatementsql
statement.setString make;
statement.setString model;
statement.setInt year;
statement.setDouble price;
statement.executeUpdate;
System.out.printlnCar added successfully.";
catch SQLException ex
exprintStackTrace;
JOptionPane.showMessageDialognull "Error adding car to the database", "Database Error", JOptionPane.ERRORMESSAGE;
finally
Close the database connection
try
connection.close;
catch SQLException ex
exprintStackTrace;
catch NumberFormatException ex
exprintStackTrace;
JOptionPane.showMessageDialognull "Invalid number format for year or price", "Input Error", JOptionPane.ERRORMESSAGE;
;
JButton sortButton new JButtonSort by Price";
sortButton.addActionListenernew ActionListener
public void actionPerformedActionEvent e
Collections.sortvehicleList;
Refresh the display or update a JTable
;
JButton searchButton new JButtonSearch;
searchButton.addActionListenernew ActionListener
public void actionPerformedActionEvent e
String searchMake JOptionPane.showInputDialogEnter make to search:";
Perform linear search for simplicity, consider using a more efficient search algorithm
for Vehicle vehicle : vehicleList
if vehiclegetMakeequalsIgnoreCasesearchMake
JOptionPane.showMessageDialognull "Found: vehicle.toString;
return;
JOptionPane.showMessageDialognull "Car not found.";
;
panel.addmakeLabel;
panel.addmakeField;
panel.addmodelLabel;
panel.addmodelField;
panel.addyearLabel;
panel.addyearField;
panel.addpriceLabel;
panel.addpriceField;
panel.addaddButton;
panel.addsortButton;
panel.addsearchButton;
addpanel;
setVisibletrue;
private Connection getDatabaseConnection
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