You are required to develop a GUI for car show room. The program must use collection to store and process information about cars.

The class for car information must have following attributes:

1. Name of car’s manufacturer
2. Name of car’s owner
3. Registration number of car
4. NIC number of car’s owner
5.
Following is the list of core requirements.

The program should allow a user to:

1. Add a car to a collection
2. Find the registration number of all cars owned by particular person (whose NIC number is specified)
3. Remove all the cars from collection by NIC number
4. Find the total number of cars of particular manufacturer (whose name is specified)
5. Reset all data entered in text boxes

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class CarShowRoom implements ActionListener {

JFrame frame;
JLabel nameOfManufacturer;
JLabel nameOfOwnerOfCar ;
JLabel registrationNumber;
JLabel nicNumberOfOwnerOfCar ;

JTextField field1, field2,field3, field4;
JButton add, search, delete, count ,reset;

// setting Layout

public void initGUI () {
frame = new JFrame ();
nameOfManufacturer = new JLabel ("Name of Manufacturer:");
nameOfOwnerOfCar = new JLabel ("Name of Owner of Car");
registrationNumber = new JLabel ("Registration Number:");
nicNumberOfOwnerOfCar= new JLabel ("NIC Number of Owner");


field1 = new JTextField (20);
field2 = new JTextField (20);
field3 = new JTextField (20);
field4 = new JTextField (20);

add = new JButton ("add a Car");
add.setPreferredSize (new Dimension (70,25));
search = new JButton ("search");
search.setPreferredSize ( new Dimension (70,25));
delete = new JButton ("delete");
delete.setPreferredSize (new Dimension (70,25));
count = new JButton ("count");
count.setPreferredSize ( new Dimension (70,25));
reset = new JButton ("reset");
reset.setPreferredSize ( new Dimension (70,25));

Container cont = frame.getContentPane ();
cont.setLayout (new FlowLayout());
cont.add (nameOfManufacturer);
cont.add (field1);
cont.add (nameOfOwnerOfCar);
cont.add (field2);
cont.add (registrationNumber);
cont.add (field3);
cont.add (nicNumberOfOwnerOfCar);
cont.add (field3);

// creating an object of the class which is handling button events and register it

ButtonHandler bHandler = new ButtonHandler ();
add.addActionListener (bHandler);
search.addActionListener (bHandler);
delete.addActionListener ( bHandler);
count.addActionListener (bHandler);
reset.addActionListener (bHandler);

frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (300,320);
frame.setVisible (true);
}

// constructor
public CarShowRoom (){
initGUI();
}

// innerClass implementation of ActionListner

private class ButtonHandler implements ActionListener {

String string1 , string2,string3,string4;
int num1, num2;

public void actionPerformed (ActionEvent event){
if ( event.getSource () == add ) {
string1 = field1.getText();
string2 = field2.getText();
string3 = field3.getText();
num1 = Integer.parseInt (string3);
string4 = field4.getText ();
num2 = Integer.parseInt (string4);

// add new car record to arrayList after taking input
public void addCar() {
String name = JOptionPane.showMessageDialog ( "Enter Name of Manufacturer" );
String owner = JOptionPane.showMessageDialog ( "Enter Name of Owner of Car" );
String rNum = JOptionPane.showMessageDialog ( "Enter Registration number" );
String nic = JOptionPane.showMessageDialog ( "Enter NIC of Owner of car" );

//construct new car object

CarInfo c = new CarInfo ( name ,owner, rNum, nic);

// add the above carinfo object to arrayList

cars.add(c);
}

}

if ( event.getSource () == search ) {

String nic = JOptionPane.showInputDialog ( "Enter NIC of Owner of car" );

// search car record by NIC by iterating over arrayList
public void void searchCar (){
for (int i=0; i <cars.size (); i++){
CarInfo c = ( CarInfo ) cars.get (i);
if ( nic.equals (c.registNum)) {
c.print();
}
}
}
}
if ( event.getSource () == delete) {
string4 = field4.getText ();
num2 = Integer.parseInt (string4);

// delete car record by NIC of owner
public void deletCar (int num2){
for (int i=0; i <cars.size (); i ++){
CarInfo c = (CarInfo) cars.get (i);
if (num2.equals (c.registNum)){
cars.remove (i);
}
}
}

if ( event.getSource () == count ) {
string1 = field1.getText();
string2 = field2.getText();
string3 = field3.getText();
num1 = Integer.parseInt (string3);
string4 = field4.getText ();
num2 = Integer.parseInt (string4);
}

if ( event.getSource () == reset) {
string1 = field1.getText();
string2 = field2.getText();
string3 = field3.getText();
num1 = Integer.parseInt (string3);
string4 = field4.getText ();
num2 = Integer.parseInt (string4);

}
}
public static void main ( String args [] ) {
CarShowRoom cSR = new CarShowRoom ();

}
}
}

Recommended Answers

All 8 Replies

So .... where do you need our help ??? (Apart from giving you pointers on code tags usage)

So .... where do you need our help ??? (Apart from giving you pointers on code tags usage)

Dear I need Help in "ActionPerformed" section
would you pleae.

Dear I need Help in "ActionPerformed" section

Now "Help" is too generic a term here. It can be a drowning man calling out for "HELP" or financially strained person asking for "HELP" etc..

Tell us what you want the program to do and from what you have implemented what is happening and a little analysis on why you think it is happening and do not simply quote your instructor's questions, because as this announcement mentions we only help those who show effort on their part to solve the problem.

I need Help in "ActionListener" section of the assignment.
more details on assignment:
Detailed information:

1. When user presses ‘Add’ button then all the information from the text boxes must be added into collection and a label must be displayed as: “A car has been added”.

2. When user presses ‘Search’ button then a dialog box must appear that ask the user to enter NIC number to find the registration number for that NIC number. When user enters NIC number in the dialog box then it must search the Registration number corresponding to that NIC from collection and then display it to user. If it does not find any record then it displays message: “Record not found”.

3. When user presses ‘Delete’ button then a dialog box must appear that ask the user to enter NIC number to remove all the cars corresponding to that NIC number. When user enters NIC number then it must delete the record corresponding to that NIC from collection. If it does not find any record then it displays message: “Record not found”.

4. When user presses ‘Count’ button then a dialog box must appear that ask the user to enter manufacturer name to count the total number of cars. When user enters manufacturer name then it must search the total no. of cars corresponding to that manufacturer and then displays to user.

5. When user presses ‘Reset’ button then all the data in the text boxes and label must be reset.

Posting an email address still isn't going to get this code written for you.

Posting an email address still isn't going to get this code written for you.

Dear I am trying to understand what you have said?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.