Question & Answer: evy Impala Purchases Joe | Chevy Impala | S25,678.00 Sue l Ford F1501 $36,879.00 nce 24789 Submit Aveage Purcha…..

Final Exam Customers Name Beth Car Make/Model Chevy Impala Purchases Joe | Chevy Impala | S25,678.00 Sue l Ford F1501 $36,879.00 nce 24789 Submit Aveage Purchase Price $31,278.50Final Exam Customers Name Beth Car Make/Model Chevy Impala Purchases Joe | Chevy Impala | S25,678.00 Sue l Ford F1501 $36,879.00 nce 24789 Submit Aveage Purchase Price $31,278.50

The program allows a user to add a customer name, a car make/model and a purchase price into a database. Once entered, the list of customers, cards and purchase prices as well as the average purchase price must be displayed.

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: evy Impala Purchases Joe | Chevy Impala | S25,678.00 Sue l Ford F1501 $36,879.00 nce 24789 Submit Aveage Purcha…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

A purchase price must be a positive number. Should the user enter an invalid value, an appropriate error message must be displayed.

When the program opens, should there be any data in the database, it must be displayed first. This is to ensure the user may pick up where they left off next time they open the software.

The list of Car Make/Models must be setup so that the user may type within the Comobox and it must display a unique list of car makes and models. As the user adds new cars, the list must automatically update based on the data in the database.

Show transcribed image text

Expert Answer

 

Below is the program . You need to create a database (chegg according to my program) and a table named customer.

create table customer(name varchar(40),model varchar(40),price varchar(40));

You can rename the driver, url, password, username according to your database.

************************************************************************************************************************************

package com.ibm.dao;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class view extends JApplet implements Runnable {
JButton button;
JTextArea text;
JTextField myTextField;
JComboBox cb;
JTextField myTextField1;
view me;
Connection con;

public void init() {
//WindowUtilities.setNativeLookAndFeel();
Container content = getContentPane();
content.setBackground(Color.gray);
content.setLayout(new FlowLayout());
JLabel namee = new JLabel(“Customer’s name”);
myTextField = new JTextField(“Enter name”);
content.add(namee,BorderLayout.WEST);
content.add(myTextField,BorderLayout.CENTER);
JLabel mod = new JLabel(“Car make/model”);
content.add(mod,BorderLayout.WEST);
String model[]={“Chevy imphala”,”Ford”,”BMW”,”Audi”,”Ferrari”};
cb=new JComboBox(model);
cb.setBounds(50, 50,90,20);
content.add(cb);
JLabel pri = new JLabel(“Purchase price”);
myTextField1 = new JTextField(“enter price”);
content.add(pri,BorderLayout.WEST);
content.add(myTextField1,BorderLayout.CENTER);
button = new JButton(“Submit”);
content.add(button);
button.addActionListener(new ButtonListener());

text = new JTextArea(10, 60);
text.setLineWrap(true);
content.add(text);

me = this;
me.getResults();
}

public void run() {

}

public void getResults() {
text.setText(“”);

try {
//System.out.println(“I am here”);
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
}
catch (Exception e) {
System.out.println(
“Unable to register the JDBC Driver.n” +
“Make sure the JDBC driver is in then” +
“classpath.n”);
}

// This URL specifies we are connecting with a database server
// on localhost.
String url = “jdbc:mysql://localhost:3306/chegg”;

// The username / password to connect under.
String username = “root”;
String password = “jass”;

// Make a connection with the database.

try {
con = DriverManager.getConnection(url, username, password);
}
catch (SQLException e) {
System.out.println(
“Unable to make a connection to the database.n” +
“The reason: ” + e.getMessage());
return;
}

try {

Statement stmt = con.createStatement();

// using executeQuery():
ResultSet rs = stmt.executeQuery(
“SELECT * FROM customer “);

// moving forward in the result set:
while (rs.next()) {

String name = rs.getString(“name”);
String model = rs.getString(“model”);
String price = rs.getString(“price”);
text.append(name + ” ” + model + ” ” + price + “n”);

}

// Close the connection when finished
}
catch (SQLException e) {
System.out.println(
“An error occuredn” +
“The SQLException message is: ” + e.getMessage());
return;
}
}

class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {

try {
PreparedStatement pstmt;
pstmt=con.prepareStatement(“insert into customer values(?,?,?)”);
pstmt.setString(1,myTextField.getText());
pstmt.setString(2,cb.getItemAt(cb.getSelectedIndex()).toString());
pstmt.setString(3,myTextField1.getText());
pstmt.executeUpdate();
System.out.println(“saved”);
me.getResults();
con.close();
} catch(Exception c) {
System.out.println(“insertion:”+c.getMessage());
}

}

}

}

************************************************************************************************************************************

Still stressed from student homework?
Get quality assistance from academic writers!