You are on page 1of 10

// Class fully inheritance and Person immutable GUI nun and Gridbag Layout

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Date;
import java.text.*;
class Person
{
private String Name;
private int nId;
private Date bDay;
Person(String Name,int nId)
{
bDay=new Date();
this.Name=Name;
this.nId=nId;
}
public String getName()
{
return Name;
}
public String toString()
{
String st="";
SimpleDateFormat ft = new SimpleDateFormat ("E dd / MM / yyyy");
st+=("Student Name "+Name+"\nNational ID "+nId+"\nDate "+ ft.format(bDay));
return st;
}
}
class Subjects
{
private int subId;
private double marks[];
private String sName;
private double GPA;
boolean paid;
Subjects(){}
public void setSubject(String sName,int subId,int mNum)
{
this.sName=sName;
this.subId=subId;
marks=new double[mNum];
}
public void setMarks(double [] marks)
{
for(int i=0;i<this.marks.length;i++)
this.marks[i]=marks[i];
}
public void setPaid(boolean paid)
{
this.paid=paid;
}
public boolean getPaid()
{
return this.paid;
}
private void setGPA()
{
GPA=0;
for(int i=0;i<marks.length;i++)
GPA+=marks[i];
}
public double getGPA()
{
this.setGPA();
return GPA;
}
public String toString()
{
String st="";
if(paid)
{
st+="\nSubject Name "+sName+"\nSubject Number "+subId;
st+="\nNumber of Marks: "+marks.length+'\n';
for(int i=0;i<marks.length;i++)
st+=marks[i]+" ";
st+="\nGPA= "+getGPA();
}
else
st="Tuitions Not Paid No Records";
return st;
}
}
class Student extends Person
{
private int stId;
private Subjects []m;
private double gGPA;
Student(String Name,int nId,int stId,int nSubj)
{
super(Name,nId);
this.stId=stId;
m=new Subjects[nSubj];
for(int i=0;i<m.length;i++)
m[i]=new Subjects();
}
public void addSubject(String sName,int subId,int mNum,int index, boolean isPaid)
{
m[index].setSubject(sName,subId,mNum);
m[index].setPaid(isPaid);
}
public void setMarks(int index,double[]marks)
{
m[index].setMarks(marks);
}
public int numOfMarks()
{
return m.length;
}
public String toString()
{
String ta="";
boolean tu=false;
ta+=super.toString();
ta+="\nStudent ID "+stId;
for(int j=0;j<m.length;j++)
ta+='\n'+m[j].toString();
for(int j=0;j<m.length;j++)
if(m[j].getPaid()) tu=true;
if(tu)
ta+=("\nGeneral GPA = "+getgGPA()+"\n");
else
ta+=("\nGeneral GPA = Tuitions Not Paid\n");
ta+=("----------------------------------------------------------------------\n");
return ta;
}
private double getgGPA()
{
gGPA=0;
for(int j=0;j<m.length;j++)
gGPA+=m[j].getGPA();
return gGPA/m.length;
}
}
class TestStudentGread extends JFrame implements ActionListener
{
private JLabel L=new JLabel("Number of Students ");
private JLabel L1=new JLabel();
private JLabel L2=new JLabel("1 Student Name");
private JLabel L3=new JLabel("Student National ID");
private JLabel L4=new JLabel("Student ID");
private JLabel L5=new JLabel("Number of enrolled subjects");
private JLabel L6=new JLabel("Student Name");
private JLabel L7=new JLabel("Subject Name");
private JLabel L8=new JLabel("Subject ID");
private JLabel L9=new JLabel("Marks Number");
private JTextField name=new JTextField(15);
private JTextField sName=new JTextField(15);
private JTextField nId=new JTextField(10);
private JTextField sId=new JTextField(10);
private JTextField subId=new JTextField(10);
private JTextField subjN=new JTextField(10);
private JTextField stNum=new JTextField(10);
private JTextField mNum=new JTextField(4);
private JTextField Mark[];
private JRadioButton rb=new JRadioButton("Tuitions Paid");
private JButton addSt=new JButton("ADD");
private JButton addSu=new JButton("ADD Student");
private JButton addSubj=new JButton("ADD Marks");
private JTextArea ta=new JTextArea();
private JTabbedPane taPane = new JTabbedPane();
private JPanel p0,p1,p2,p3,p4,p5,p6;
private JScrollPane scrollPane;
private Student[]st;
private double marks[];
private static int click;
private static int click1;
private static int subClick;
TestStudentGread()
{
setLayout(null);
p0=new JPanel();
p0.setLayout(null);
Border b=BorderFactory.createLineBorder(Color.BLACK,2);
p0.setBorder(BorderFactory.createTitledBorder(b,"Number of Students P0"));
p0.setSize(330,50);
p0.setLocation(60,10);
L.setSize(120,20);
L.setLocation(10,18);
stNum.setSize(80,20);
stNum.setLocation(140,18);
addSt.setSize(80,20);
addSt.setLocation(230,18);
p0.add(L);p0.add(stNum);
p0.add(addSt);
addSt.addActionListener(this);
add(p0);
p1=new JPanel();
p1.setLayout(null);
Border b1=BorderFactory.createLineBorder(Color.BLUE,2);
p1.setBorder(BorderFactory.createTitledBorder(b1,"Students Data P1"));
p1.setSize(250,130);
p1.setLocation(180,60);
L2.setSize(120,20);
L3.setSize(120,20);
L4.setSize(120,20);
L5.setSize(190,20);
L2.setLocation(15,20);
L3.setLocation(15,40);
L4.setLocation(15,60);
L5.setLocation(15,80);
name.setSize(60,20);
name.setLocation(180,20);
nId.setSize(60,20);
nId.setLocation(180,40);
sId.setSize(60,20);
sId.setLocation(180,60);
subjN.setSize(60,20);
subjN.setLocation(180,80);
p1.add(L2);p1.add(name);
p1.add(L3);p1.add(nId);
p1.add(L4);p1.add(sId);
p1.add(L5);p1.add(subjN);
addSu.setSize(120,20);
addSu.setLocation(60,100);
p1.setVisible(false);
p1.add(addSu);
addSu.addActionListener(this);
add(p1);
p2=new JPanel();
Border b2=BorderFactory.createLineBorder(Color.GRAY,2);
p2.setBorder(BorderFactory.createTitledBorder(b2,"Subject Data P2"));
p2.setLayout(null);
p2.setSize(170,120);
p2.setLocation(10,60);
L1.setSize(120,20);
L6.setSize(100,20);
L7.setSize(120,20);
L8.setSize(120,20);
L9.setSize(190,20);
L1.setLocation(90,20);
L6.setLocation(5,20);
L7.setLocation(10,40);
L8.setLocation(10,60);
L9.setLocation(10,80);
sName.setSize(60,20);
sName.setLocation(100,40);
subId.setSize(60,20);
subId.setLocation(100,60);
mNum.setSize(60,20);
mNum.setLocation(100,80);
p2.add(L6);p2.add(L1);
p2.add(L7);p2.add(sName);
p2.add(L8);p2.add(subId);
p2.add(L9);p2.add(mNum);
mNum.addActionListener(this);
addSubj.addActionListener(this);
p2.setVisible(false);
add(p2);
p3=new JPanel();
Border b3=BorderFactory.createLineBorder(Color.GREEN,2);
p3.setBorder(BorderFactory.createTitledBorder(b3,"The Results P3"));
p3.setLayout(null);
p3.setSize(400,100);
p3.setLocation(10,300);
ta.setEditable(false);
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
JScrollPane areaScrollPane = new JScrollPane(ta);
areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALW
AYS);
areaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR
_AS_
NEEDED);
areaScrollPane.setSize(380,70);
areaScrollPane.setLocation(10,20);
p3.add(areaScrollPane);
p3.setVisible(false);
add(p3);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==addSt)
{
int ist=0;
try
{
ist=Integer.parseInt(stNum.getText());
}
catch(NumberFormatException q)
{
JOptionPane.showMessageDialog(this,q.getMessage()+"\nYou must enter integers.
Please try again.\n",
"Input Error",JOptionPane.ERROR_MESSAGE);
stNum.setText("");
}
if(ist!=0){
st=new Student[ist];
p1.setVisible(true);
stNum.setEditable(false);}
}
if(e.getSource()==addSu)
{
int inId=0;
int isId=0;
int isubjN=0;
try
{
inId=Integer.parseInt(nId.getText());
isId=Integer.parseInt(sId.getText());
isubjN=Integer.parseInt(subjN.getText());
}
catch(NumberFormatException q)
{
JOptionPane.showMessageDialog(this,q.getMessage()+"\nYou must enter integers.
Please try again.\n",
"Input Error",JOptionPane.ERROR_MESSAGE);
nId.setText("");sId.setText("");subjN.setText("");
}
if(isubjN!=0){
if(click<st.length)
{
st[click]=new Student(name.getText(),inId,isId,isubjN);
name.setText("");nId.setText("");sId.setText("");subjN.setText("");
}
if(click== st.length-1)
{
L1.setText(st[0].getName());
click=0;
name.setEditable(false);
nId.setEditable(false);
sId.setEditable(false);
subjN.setEditable(false);
p2.setVisible(true);
}
click++;
L2.setText((click+1)+" Student Name");}
}
if(e.getSource()==mNum)
{
int intmNum=0;
int isubId=0;
try
{
intmNum=Integer.parseInt(mNum.getText());
isubId=Integer.parseInt(subId.getText());
}
catch(NumberFormatException q)
{
JOptionPane.showMessageDialog(this,q.getMessage()+"\nYou must enter integers.
Please try again.\n",
"Input Error",JOptionPane.ERROR_MESSAGE);
mNum.setText("");subId.setText("");
}
if(intmNum!=0){
Mark=new JTextField[Integer.parseInt(mNum.getText())];
JLabel[]la=new JLabel[Integer.parseInt(mNum.getText())];
p4=new JPanel(new GridLayout((Integer.parseInt(mNum.getText())+1),2,10,0));
Border b4=BorderFactory.createLineBorder(Color.YELLOW,2);
p4.setBorder(BorderFactory.createTitledBorder(b4,"Enter the Marks p4"));
for(int y=0,i=0;i<Integer.parseInt(mNum.getText());i++,y+=20)
{
la[i]=new JLabel((i+1)+" Mark");
Mark[i]=new JTextField(6);
la[i].setSize(70,20);
la[i].setLocation(15,20+y);
Mark[i].setSize(70,20);
Mark[i].setLocation(90,20+y);
p4.add(la[i]);
p4.add(Mark[i]);
}
addSubj.setLocation(200,20);
addSubj.setSize(100,20);
rb.setLocation(200,50);
rb.setSize(100,20);
p4.add(rb);
p4.add(addSubj);
scrollPane = new JScrollPane(p4);
scrollPane.setSize(350,100);
scrollPane.setLocation(10,200);
add(scrollPane);
revalidate();
repaint();}
}
if(e.getSource()==addSubj)
{
if(subClick<st[click1].numOfMarks()&&click1<st.length)
{
st[click1].addSubject(sName.getText(),Integer.parseInt(subId.getText()),Integer.parseInt
(mNum.
getText()),
subClick,rb.isSelected());
marks=new double[Integer.parseInt(mNum.getText())];
for(int i=0;i<Integer.parseInt(mNum.getText());i++)
{
marks[i]=Double.parseDouble(Mark[i].getText());
Mark[i].setText("");
}
st[click1].setMarks(subClick,marks);
subClick++;
sName.setText("");subId.setText("");mNum.setText("");rb.setSelected(false);
for(JTextField f:Mark)
f.setEditable(false);
remove(scrollPane);
revalidate();
repaint();
}
if(subClick==st[click1].numOfMarks()&&click1<st.length)
{
click1++;
subClick=0;
if(click1<=st.length-1)
L1.setText(st[click1].getName());
}
if(click1==st.length)
{
add(scrollPane);
revalidate();
repaint();
p3.setVisible(true);
ta.setText("");
sName.setEditable(false);subId.setEditable(false);mNum.setEditable(false);
click1=0;
subClick=0;
for(int i=0;i<st.length;i++)
ta.append(st[i].toString());
}
}
}
public static void main(String[] args)
{
JFrame frame = new TestStudentGread();
frame.setTitle("Records");
frame.setSize(450, 450);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

You might also like