Java program for Prof.Huda

profilebshabrbq.o
assignment.docx

Description: sh3ar2

JUBAIL UNIVERSITY COLLEGE

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

SEMESTER 362

Course Code

CS 205

Course Title

Data Structures

Section

201

Date

(Assignment given to students)

28/02/2016

Date of Submission

10/04/2016

PART I

TO BE FILLED BY THE STUDENT

STUDENT’S NAME

Rowida AlFalah

ID. No.

33120255

STUDENT’S NAME

Ashwaq Al-Dhafiri

ID. No.

32120298

STUDENT’S NAME

Nojoud AlQubaili

ID. No.

32120036

PART II

1st Marker

Area of Assessment

Max Marks

Actual Marks

Comments/Remarks

Project Documentation

8

System Functionality

13

Presentation

4

Total

25

Name: Ms. C. VIJAYALAKSHMI

Signature:

TO BE FILLED BY THE CONCERNED DEPARTMENT

TABLE OF CONTENT

_________________________________________________________________________________________________________________________________________________________

· Introduction ……………………………………………………. 3

· Software Resources .…………………………………………… 3

· Hardware Resources …………………………………………… 3

· Program Codes …………………………………………….…… 4

· Output Sample ..………………………..……………………… 15

· Reference …………………………………………...………….. 16

Introduction:

This is a program that applies the concepts that were learned during the course CS205 (Data Structure and Algorithm in Java). The application "Car Plates Information" is designed and programed to read the following inputs: Plate owner's name, Social Security number, plate's information, condition and price. It also can perform the following operations: add a new record, show the list of information, print the list and finally, clear the fields of input. The program also can perform a specific operations using the owner's social security number, such as: update, delete, or search a record. The main purpose of this program is to store all the information related to the sold car plates and who owns it.

The application contains three classes. The First class "Node Class" contains the variable initialization. The second class "Process Class" contains the singly linked list methods needed to perform the operations, such as: add to head, printing, searchByOss (Search By Social Security Number), delete, and updateByOss (Update By Social Security Number). The last class, "Test Class", contains 4 panels, the first panel has the logo image. The second panel has 5 labels (Owner's name, Owner's Social Security Number, Plate Information, Plate price and Plate Condition), and 4 text fields and 1 menu bar for the plate condition. The third panel has 4 buttons: 1. Add, to add the new records. 2. Clear, to clear the fields. 3. Print, to print the records. 4. Show list, to display the list. The last panel has one text area to display the list.

The application includes four exceptions with an error message displayed:

1- User-Defined exception "CheckInput" to check the quantity if it's more than 10000, print an error message.

2- Predefined exception "IOException" to handle negative inputs.

3- Predefined exception "NumberFormatException" to handle wrong data type.

4- Predefined exception “PrinterException”

Hardware Resources:

· Manufacture: Microsoft Corporation.

· Processor: Intel(R) Core(TM) i3-4020Y CPU @ 1.50GHz.

· Installed Memory(RAM): 4 GB.

· System Type: 64-bit Operating System, x64-based processor.

Software Resources:

· Adobe Photoshop CS6.

· JCreator LE version 5.10

· Microsoft Word, 2013.

Program Codes:

1. Node Class:

public class Node

{

public String OName;

public String cond;

public String plate;

public double Pprice;

public int Oss;

public Node next;

public Node(String a,String b,String c, int d, double e)

{

OName=a;

cond=b;

plate=c;

Oss=d;

Pprice=e;

next=null;

}

public Node(String a,String b,String c, int d, double e, Node n)

{

OName=a;

cond=b;

plate=c;

Oss=d;

Pprice=e;

next=n;

}

}

2. Process Class:

public class process

{

public Node head,tail;

public process()

{

head=tail=null;

}

public void addToHead(String e1,String e2, String e3, int e4,double e5)

{

head=new Node (e1,e2,e3,e4,e5,head);

if(tail == null)

tail=head;

}

public String SearchByOss(int e1)

{

String s;

Node tmp;

for(tmp=head;tmp!=null && tmp.Oss != e1; tmp=tmp.next);

if(tmp!=null)

s="The " + e1 + " is found in the list\n"+

tmp.OName+"\t"+tmp.Oss+"\t"+tmp.Pprice+"\t"+tmp.cond+"\t"+tmp.plate;

else

s="The " + e1 + " is NOT found in the list\n";

return s;

}

public String printing()

{

String s="";

Node tmp;

for( tmp=head;tmp!=null;tmp=tmp.next)

s=tmp.OName+"\t "+tmp.Oss+"\t\t "+tmp.cond+"\t\t "+tmp.Pprice+"\t "+tmp.plate+"\n"+s;

return s;

}

public String updateByOss(int a1,String e1,String e2, String e3, int e4,double e5)

{

Node tmp;

for(tmp=head;tmp!=null && tmp.Oss != a1; tmp=tmp.next);

if(tmp!=null)

{

tmp.OName=e1;

tmp.cond=e2;

tmp.plate=e3;

tmp.Oss=e4;

tmp.Pprice=e5;

return("\n The record updated successfully.\n"+

tmp.OName+"\t"+tmp.Oss+"\t"+tmp.cond+"\t"+tmp.Pprice+"\t"+tmp.plate);

}

else

return( a1 + " is not found\n");

}

public String delete(int e1)

{

if(head!=null)

{

if(head == tail && head.Oss==e1)

{

head=tail=null;

return e1+"\n is deleted\n";}

else if (head.Oss==e1){

head=head.next;

return e1+" is deleted\n";

}

else

{

Node pred,tmp;

for(pred=head,tmp=head.next; tmp!=null && tmp.Oss != e1;

pred=pred.next,tmp=tmp.next);

if (tmp!=null)

{

pred.next=tmp.next;

if(tmp==tail)

tail=pred;

return e1+"\n is deleted\n";

}

}

}

return e1+" is not found\n";

}

}

3. Test Class:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.text.*;

import java.awt.print.*;

public class test extends JFrame implements ActionListener

{

public String Oname,cond, plate;

public int Oss,Ossupdate;

public double Pprice;

JPanel p1,p2,p3,p4;

JScrollPane scrollpane;

JTextField t1,t2,t3,t4,t5;

JTextArea ta1;

JButton b1,b2,b3,b4;

JLabel l1,l2,l3,l4,j1,l11;

Color c1,c2,c3,c4;

Font f1,f2,f3;

JMenuBar menuBar = new JMenuBar();

JMenu ProductCodeMenu = new JMenu("Choose");

JMenuItem Product1 = new JMenuItem("New");

JMenuItem Product2 = new JMenuItem("Used");

JMenuBar menuBar1 = new JMenuBar();

JMenu CodeMenu = new JMenu("Choose an operation");

JMenuItem P1 = new JMenuItem("Update");

JMenuItem P2 = new JMenuItem("Delete");

JMenuItem P3 = new JMenuItem("Search");

public process P= new process();

public test()

{

f1=new Font("Times New Roman",Font.BOLD,20);

f2=new Font("Times New Roman",Font.BOLD,18);

f3=new Font("Times New Roman",Font.BOLD,20);

c1=new Color(20,161,149); //Drop down color

c4=new Color(20,161,149); //Buttons color

setSize(500,500);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new BorderLayout());

p4=new JPanel();

j1=new JLabel();

j1.setIcon(new ImageIcon("logo.png"));

p4.add(j1);

p4.setBackground(c3);

add(p4,BorderLayout.NORTH);

validate();

p1=new JPanel();

p1.setSize(100,200);

p1.setLayout(new GridLayout(6,2));

l1=new JLabel("Plate Owner Name");

l1.setForeground(c2);

l1.setFont(f1);

p1.add(l1);

t3=new JTextField();

t3.setForeground(c1);

t3.setFont(f2);

p1.add(t3);

l2=new JLabel("Owner Socia Security Number ");

l2.setFont(f1);

p1.add(l2);

t2=new JTextField();

t2.setForeground(c1);

t2.setFont(f2);

p1.add(t2);

l4=new JLabel("Plate Information");

l4.setForeground(c2);

l4.setFont(f1);

p1.add(l4);

t5=new JTextField();

t5.setForeground(c1);

t5.setFont(f2);

p1.add(t5);

l11=new JLabel("Plate Price");

l11.setFont(f1);

p1.add(l11);

t4=new JTextField();

t4.setForeground(c1);

t4.setFont(f2);

p1.add(t4);

l3=new JLabel("Condition ");

l3.setForeground(c2);

l3.setFont(f1);

p1.add(l3);

menuBar.add(ProductCodeMenu);

ProductCodeMenu.setForeground(Color.WHITE);

ProductCodeMenu.setFont(f2);

menuBar.setBackground(c1);

ProductCodeMenu.add(Product1);

Product1.setForeground(Color.WHITE);

Product1.setFont(f2);

Product1.setBackground(c1);

ProductCodeMenu.add(Product2);

Product2.setForeground(Color.WHITE);

Product2.setFont(f2);

Product2.setBackground(c1);

setJMenuBar(menuBar);

Product1.addActionListener(this);

Product2.addActionListener(this);

p1.add(menuBar);

t1=new JTextField("Write the Social Security Number to do an operation");

p1.add(t1);

menuBar1.add(CodeMenu);

CodeMenu.setForeground(Color.WHITE);

CodeMenu.setFont(f2);

menuBar1.setBackground(c1);

CodeMenu.add(P1);

P1.setForeground(Color.WHITE);

P1.setFont(f2);

P1.setBackground(c1);

CodeMenu.add(P2);

P2.setForeground(Color.WHITE);

P2.setFont(f2);

P2.setBackground(c1);

CodeMenu.add(P3);

P3.setForeground(Color.WHITE);

P3.setFont(f2);

P3.setBackground(c1);

setJMenuBar(menuBar1);

P1.addActionListener(this);

P2.addActionListener(this);

P3.addActionListener(this);

p1.add(menuBar1);

add(p1,BorderLayout.CENTER);

p2=new JPanel();

p2.setLayout(new FlowLayout());

b1=new JButton("Add");

b1.setBackground(c4);

b1.setForeground(Color.WHITE);

b1.setFont(f2);

b1.addActionListener(this);

b1.setToolTipText("To add a record to the list");

p2.add(b1);

b2=new JButton("Clear");

b2.setBackground(c4);

b2.setForeground(Color.WHITE);

b2.setFont(f2);

b2.addActionListener(this);

b2.setToolTipText("Reset fields");

p2.add(b2);

b3=new JButton("Print");

b3.setBackground(c4);

b3.setForeground(Color.WHITE);

b3.setFont(f2);

b3.addActionListener(this);

b3.setToolTipText("Click to print the List");

p2.add(b3);

b4=new JButton("Show list");

b4.setBackground(c4);

b4.setForeground(Color.WHITE);

b4.setFont(f2);

b4.addActionListener(this);

b4.setToolTipText("Click to show the List");

p2.add(b4);

p2.setBackground(c3);

add(p2,BorderLayout.SOUTH);

p3=new JPanel();

p3.setLayout(new FlowLayout());

p3.setBackground(c3);

ta1=new JTextArea(10,100);

ta1.setText("Owner's name\t Social Security Number\t Plate's Information\t Price\t Condition\n");

p3.add(ta1);

add(p3,BorderLayout.EAST);

scrollpane = new JScrollPane(ta1);

add(scrollpane, BorderLayout.EAST);

pack();

scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

}

public void actionPerformed(ActionEvent e)

{

String k=e.getActionCommand();

if(k.equals("New"))

{

ProductCodeMenu.setText("New");

}

if(k.equals("Used"))

{

ProductCodeMenu.setText("Used");

}

if(k.equals("Add"))

{

try{

Oname=t3.getText();

plate=t5.getText();

Oss=Integer.parseInt(t2.getText());

cond=ProductCodeMenu.getText();

Pprice=Double.parseDouble(t4.getText());

if((Oss<0)||(Pprice<0)) throw new IOException("Error: No negative values allowed") ;

if(t5.getText().equals("")||t3.getText().equals("")||t4.getText().equals("")||t2.getText().equals("")||ProductCodeMenu.getText().equals("Choose"))

{

String messageF= "Addition failed";

JOptionPane.showMessageDialog(null,messageF);

}

else{

P.addToHead(Oname,plate,cond,Oss,Pprice);

String messageS= "Record is added";

JOptionPane.showMessageDialog(null,messageS);

ta1.append(P.printing()+ "\n");

}

}

catch (IOException f)

{

JOptionPane.showMessageDialog(null,f.getMessage(),null,JOptionPane.ERROR_MESSAGE);

}

catch(NumberFormatException g)

{

JOptionPane.showMessageDialog(null,"Error: Wrong data type",null,JOptionPane.ERROR_MESSAGE);

}

}

if(k.equals("Print"))

{

MessageFormat header=new MessageFormat("Plate list");

MessageFormat footer=new MessageFormat("page{0,number,integer}");

try{

boolean complete=ta1.print();

if(complete)

{

JOptionPane.showMessageDialog(null,"Done printing","Information",JOptionPane.INFORMATION_MESSAGE);

}

else

{

JOptionPane.showMessageDialog(null,"Printing!","Printer",JOptionPane.ERROR_MESSAGE);

}

}

catch(PrinterException c)

{

JOptionPane.showMessageDialog(null,c);

}

}

if(k.equals("Clear"))

{

CodeMenu.setText("Choose an operation");

ProductCodeMenu.setText("Choose");

t2.setText("");

t3.setText("");

t4.setText("");

t5.setText("");

t1.setText("Write the Social Security Number to do an operation");

ta1.setText("Owner's name\t Social Security Number\t Plate's Information\t Price\t Condition\n");

}

if(k.equals("Update"))

{ try{

Ossupdate =Integer.parseInt(t1.getText());

Oname=t3.getText();

plate=t5.getText();

Oss=Integer.parseInt(t2.getText());

cond=ProductCodeMenu.getText();

Pprice=Double.parseDouble(t4.getText());

if((Oss<0)||(Pprice<0)||(Ossupdate<0)) throw new IOException("Error: No negative values allowed") ;

CodeMenu.setText("Update");

ta1.setText(P.updateByOss(Ossupdate,Oname,cond,plate,Oss,Pprice));}

catch (IOException f)

{

JOptionPane.showMessageDialog(null,f.getMessage(),null,JOptionPane.ERROR_MESSAGE);

}

catch(NumberFormatException g)

{JOptionPane.showMessageDialog(null,"Error: Wrong data type",null,JOptionPane.ERROR_MESSAGE); }

}

if(k.equals("Delete"))

{

try{

Ossupdate =Integer.parseInt(t1.getText());

if((Ossupdate<0)) throw new IOException("Error: No negative values allowed") ;

CodeMenu.setText("Delete");

ta1.setText(P.delete(Ossupdate));

}

catch (IOException f)

{

JOptionPane.showMessageDialog(null,f.getMessage(),null,JOptionPane.ERROR_MESSAGE);

}

catch(NumberFormatException g)

{JOptionPane.showMessageDialog(null,"Error: Wrong data type",null,JOptionPane.ERROR_MESSAGE); }

}

if(k.equals("Search"))

{try{

Ossupdate =Integer.parseInt(t1.getText());

if((Ossupdate<0)) throw new IOException("Error: No negative values allowed") ;

CodeMenu.setText("Search");

ta1.setText(P.SearchByOss(Ossupdate));}

catch (IOException f)

{

JOptionPane.showMessageDialog(null,f.getMessage(),null,JOptionPane.ERROR_MESSAGE);

}

catch(NumberFormatException g)

{JOptionPane.showMessageDialog(null,"Error: Wrong data type",null,JOptionPane.ERROR_MESSAGE); }

}

if(k.equals("Show list"))

{

ta1.append(P.printing()+ "\n");

}

}

public static void main(String []args)

{

test ob=new test();

ob.setVisible(true); }

}

Output Samples:

____________________________________________________________________________________________________________________________________________________________________________________________________

References:

· Harvey, R. (2009). What is a Null Pointer Exception, and how do I fix it. Retrieved from: http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it

· Oracle. How to use Panels. Retrieved from: https://docs.oracle.com/javase/tutorial/uiswing/components/panel.html

· No Author. How to print a page when a JButton is pressed in java swing using PrinterJob. Retrieved from: http://stackoverflow.com/questions/4529316/how-to-print-a-page-when-a-jbutton-is-pressed-in-java-swing-using-printerjob

· Savitch, Walter. Absolute Java™. 5th ed. Pearson.

3 | Page