Online Auction System In Java | Java Project

by | Nov 14, 2022 | Coding, Java

Introduction of the Project

We are going to write the source code for an online auction system in java which will basically create a management system to run the Auction. To make this project more interesting, we’ll implement an interactive GUI with a SQL database to store information about the items available at the auction and information about the timer that pulls the highest bidder’s data into the database when it’s down.

 

Objectives

We have two main objectives. The first is to use graphics to create the user interface, and the second goal is to implement the addition of auction data to the database, start the auction timer, and, when the timer stops, get the details of the winning bidder.

Requirements

  • Swing (to create a graphical user interface (GUI)).

Source Code

package com.company;

public class Main {

public static void main(String[] args) {

new MainScreen();

}

}

package com.company;

import javax.swing.*;

public class MainScreen {

private JButton CUSTOMERButton;

private JButton ADMINButton;

private JPanel auctionPanel;

JFrame auctionF = new JFrame();

public MainScreen(){




auctionF.setContentPane(auctionPanel);

auctionF.pack();

auctionF.setVisible(true);

CUSTOMERButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

new Customer();

}

});

ADMINButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

new Admin();

}

});

}

}

package com.company;

import javax.swing.*;

import javax.swing.filechooser.FileNameExtensionFilter;

import javax.swing.table.DefaultTableModel;

import java.awt.*;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.sql.*;

import java.text.DateFormat;

import java.util.TimerTask;

import java.util.Vector;

public class Admin {

private JButton startButton;

private JLabel timerLabel;

private JPanel adminPanel;

private JButton ADDITEMButton;

private JTable table1;

private JTextField nameData;

private JTextField priceData;

private JTextField path;

private JButton SELECTIMAGEButton;

private JLabel imageLabel;

private JButton CLOSEButton;

public static String adminNameData="",adminPriceData="";

public static ImageIcon adminImageData;

JFrame adminF = new JFrame();

Timer timer;

public static int sec = 60;

public Admin() {




adminF.setContentPane(adminPanel);

adminF.pack();

tableData();

adminF.setVisible(true);

startButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

startTimer();

timer.start();

}

});

ADDITEMButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if(nameData.getText().equals("")|| path.getText().equals("")|| priceData.getText().equals("")){

JOptionPane.showMessageDialog(null,"Please Fill All Fields to add Record.");

}else{

String sql = "insert into auction"+"(ITEM_NAME,IMAGE,PRICE)"+"values (?,?,?)";

try {

File f = new File(path.getText());

InputStream inputStream = new FileInputStream(f);

Class.forName("com.mysql.cj.jdbc.Driver");

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");

PreparedStatement statement = connection.prepareStatement(sql);

statement.setString(1,nameData.getText());

statement.setBlob(2, inputStream);

statement.setString(3, priceData.getText());

statement.executeUpdate();

JOptionPane.showMessageDialog(null,"DETAILS ADDED SUCCESSFULLY");

nameData.setText("");

priceData.setText("");

imageLabel.setIcon(null);

path.setText("");

}catch (Exception ex){

JOptionPane.showMessageDialog(null,ex.getMessage());

}

tableData();

}

}

});

SELECTIMAGEButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

JFileChooser fileChooser = new JFileChooser();

FileNameExtensionFilter filter = new FileNameExtensionFilter("*.IMAGE","jpg","png");

fileChooser.addChoosableFileFilter(filter);

int rs = fileChooser.showSaveDialog(null);

if(rs==JFileChooser.APPROVE_OPTION){

File selectedImage = fileChooser.getSelectedFile();

path.setText(selectedImage.getAbsolutePath());

imageLabel.setIcon(resize(path.getText()));

}

}

});

table1.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

DefaultTableModel dm = (DefaultTableModel)table1.getModel();

int selectedRow = table1.getSelectedRow();

adminNameData=dm.getValueAt(selectedRow,0).toString();

nameData.setText(adminNameData);

byte[] img = (byte[]) dm.getValueAt(selectedRow,1);

ImageIcon imageIcon = new ImageIcon(img);

Image im = imageIcon.getImage();

Image newimg = im.getScaledInstance(200,200,Image.SCALE_SMOOTH);

ImageIcon finalPic = new ImageIcon(newimg);

adminImageData = finalPic;

imageLabel.setIcon(adminImageData);

adminPriceData=dm.getValueAt(selectedRow,2).toString();

priceData.setText(adminPriceData);

}

});

CLOSEButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

adminF.dispose();

}

});

}

public ImageIcon resize(String path){

ImageIcon myImg = new ImageIcon(path);

Image image = myImg.getImage();

Image newImage = image.getScaledInstance(200,200,Image.SCALE_SMOOTH);

ImageIcon finalImage = new ImageIcon(newImage);

return finalImage;

}

public void startTimer(){

timer = new Timer(1000, new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

sec--;

if(sec==-1){

timer.stop();

tableData();

}

else if(sec>=0&&sec<10) timerLabel.setText("00:0"+sec);

else timerLabel.setText("00:"+ sec);

}

});

}

public void tableData() {

try{

String a= "Select* from auction";

Class.forName("com.mysql.cj.jdbc.Driver");

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");

Statement statement = connection.createStatement();

ResultSet rs = statement.executeQuery(a);

table1.setModel(buildTableModel(rs));

}catch (Exception ex1){

JOptionPane.showMessageDialog(null,ex1.getMessage());

}

}

public static DefaultTableModel buildTableModel(ResultSet rs)

throws SQLException {

ResultSetMetaData metaData = rs.getMetaData();

// names of columns

Vector<String> columnNames = new Vector<String>();

int columnCount = metaData.getColumnCount();

for (int column = 1; column <= columnCount; column++) {

columnNames.add(metaData.getColumnName(column));

}

// data of the table

Vector<Vector<Object>> data = new Vector<Vector<Object>>();

while (rs.next()) {

Vector<Object> vector = new Vector<Object>();

for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {

vector.add(rs.getObject(columnIndex));

}

data.add(vector);

}

return new DefaultTableModel(data, columnNames);

}

}

package com.company;

import javax.swing.*;

import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.*;

import java.util.Vector;

public class Customer extends Admin {

private JLabel timerLabel;

private JPanel customPanel;

private JTable bidDetails;

private JTextField bidName;

private JTextField bidPrice;

private JButton ADDBIDButton;

private JLabel itemName;

private JLabel price;

private JLabel image;

private JButton close;

private String priceS="",name = "",bidder="";

private ImageIcon imageS;

private int bid;

Timer timer;

private static int sec;

JFrame customerF = new JFrame();

public Customer(){

customerF.setContentPane(customPanel);

customerF.pack();

customerF.setVisible(true);

sec = Admin.sec;

startTimer();

timer.start();

name = Admin.adminNameData;

priceS = Admin.adminPriceData;

imageS = Admin.adminImageData;

itemName.setText(name);

price.setText(priceS);

image.setIcon(imageS);

ADDBIDButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if(bidName.getText().equals("")|| bidPrice.getText().equals("")){

JOptionPane.showMessageDialog(null,"Please Fill All Fields to add Record.");

}else{

try {

String sql = "insert into bid"+"(Bidder_Name,Bid)"+"values (?,?)";

Class.forName("com.mysql.cj.jdbc.Driver");

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");

PreparedStatement statement = connection.prepareStatement(sql);

statement.setString(1,bidName.getText());

statement.setInt(2, Integer.parseInt(bidPrice.getText()));

statement.executeUpdate();

JOptionPane.showMessageDialog(null,"ITEM ADDED SUCCESSFULLY");

bidName.setText("");

bidPrice.setText("");

}catch (Exception ex){

JOptionPane.showMessageDialog(null,ex.getMessage());

}

tableData();

}

}

});

close.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

customerF.dispose();

}

});

}

public void startTimer(){

timer = new Timer(1000, new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if(sec==60) timerLabel.setText("AUCTION NOT STARTED!");

else sec--;

if(sec==-1){

timer.stop();

String sql = "select * from bid where bid =(select max(bid) from bid)";

try{

Class.forName("com.mysql.cj.jdbc.Driver");

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");

Statement statement = connection.createStatement();

ResultSet rs = statement.executeQuery(sql);

while (rs.next()){

bidder = rs.getString(1);

bid = rs.getInt(2);

}

String sql1 = "UPDATE auction " +

"SET BIDDER_NAME = '"+bidder+"'"+

",SOLD_AT= "+bid+

" WHERE ITEM_NAME= '"+name+"'";

String sql2= "delete from bid";

PreparedStatement preparedStatement = connection.prepareStatement(sql1);

preparedStatement.executeUpdate();

PreparedStatement preparedStatement1 = connection.prepareStatement(sql2);

preparedStatement1.executeUpdate();

JOptionPane.showMessageDialog(null,"ITEM:"+name+" SOLD TO "+bidder+" AT "+bid);

}catch (Exception e2){

JOptionPane.showMessageDialog(null,"some error");

}

tableData();

}

else if(sec>=0&&sec<10) timerLabel.setText("00:0"+sec);

else if(sec>10&&sec<60) timerLabel.setText("00:"+ sec);

}

});

}

public void tableData() {

try{

String a= "Select* from bid";

Class.forName("com.mysql.cj.jdbc.Driver");

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");

Statement statement = connection.createStatement();

ResultSet rs = statement.executeQuery(a);

bidDetails.setModel(buildTableModel(rs));

}catch (Exception ex1){

JOptionPane.showMessageDialog(null,ex1.getMessage());

}

}

public static DefaultTableModel buildTableModel(ResultSet rs)

throws SQLException {

ResultSetMetaData metaData = rs.getMetaData();

// names of columns

Vector<String> columnNames = new Vector<String>();

int columnCount = metaData.getColumnCount();

for (int column = 1; column <= columnCount; column++) {

columnNames.add(metaData.getColumnName(column));

}

// data of the table

Vector<Vector<Object>> data = new Vector<Vector<Object>>();

while (rs.next()) {

Vector<Object> vector = new Vector<Object>();

for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {

vector.add(rs.getObject(columnIndex));

}

data.add(vector);

}

return new DefaultTableModel(data, columnNames);

}

}

 

Explanation of the Code

The code can be divided into two sections. One is to create a GUI, the other is to add information about the items available at the auction, start the one-minute timer for the auction, and store the bidder details in a database.

1. The main screen consists of two buttons to open the customer screen or the management screen.
2. The customer screen has a timer, auction item information, two text fields, and a button.
3 The admin screen consists of a timer, three buttons, and a Jtable for storing item information.

Moving to the next part, we will apply the following:

1. First, use the Connection object to establish a connection to the database.
2. Paste the query that stores the table data into the ResultSet.
3. Finally, send the data to Jtable.
4. The Start Auction button on the admin panel starts the timer. Since the same timer is managed in the client area, it will continue from the moment you leave the management area.
5 The Add Bid button gets the data from the bidder and adds it to the table. At the end of the timer, there is a task to send the highest bidder details to the table that manages the items.
6. Both panels have a close button that discards the image when pressed.

Output

Main Interface

Online Auction System In Java | Java Project

Admin Panel

Online Auction System In Java | Java Project

Customer Panel

Online Auction System In Java | Java Project

Conclusion

We have successfully created an online auction system in java that will help you manage auction events. We used Swing for the GUI. This java project is a very efficient and easy way to run an auction event, add items to the auction list, start a 1 minute auction time and save the winning bidder’s details.

More Java Projects >>>>

You May Also Like To Create…

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *