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 ();
}
}
}