oenkser komentar til kode men paa eneglsk
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;
class Programming2Student implements Serializable
{
private String firstname;
private String surname;
private String stdidno;
private float labmark;
private float asg1mark;
private float asg2mark;
private float finalmark;
Programming2Student(EntryDlg obj)
{
firstname=obj.txtname.getText();;
surname=obj.txtsurname.getText();
stdidno=obj.txtID.getText();
labmark=0;
asg1mark=0;
asg2mark=0;
finalmark=0;
}
public void writeToFile()
{
try{
File f=new File ("filename.ser");
if(!f.exists())
{
f.createNewFile() ;
}
DataInputStream din=new DataInputStream(new FileInputStream(f));
// fin=new FileInputStream(f);
int counter=0;
while(din.available()>0)
{
String ss=din.readLine();
counter++;
}
DataOutputStream dout=new DataOutputStream(new FileOutputStream(f,true));
String str=Integer.toString(counter)+";;"+stdidno+";;"+firstname+" "+surname+";;"+String.valueOf(labmark)+";;"+String.valueOf(asg1mark)+";;"+String.valueOf(asg2mark)+";;"+String.valueOf(finalmark)+";;\n";
dout.writeBytes(str);
}catch(Exception e){System.out.println(e);}
}
public static void main(String s[])
{
StdInfoViewer x=new StdInfoViewer();
x.intComponent();
}
public void print()
{
System.out.println(firstname);
}
}
class StdInfoViewer extends JFrame implements ActionListener,ListSelectionListener
{
private JList viewList;
private JScrollPane jScroll;
private javax.swing.JButton cmdClose;
private javax.swing.JButton cmdDelete;
private javax.swing.JButton cmdEditMarks;
private javax.swing.JButton cmdEntry;
private javax.swing.JButton cmdUndoDelete;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JList jList1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JRadioButton radioSortByName;
private javax.swing.JRadioButton radioTotalMark;
private javax.swing.JTextField txtASg1;
private javax.swing.JTextField txtAsg2;
private javax.swing.JTextField txtFinal;
private javax.swing.JTextField txtGrade;
private javax.swing.JTextField txtID;
private javax.swing.JTextField txtLabMark;
private javax.swing.JTextField txtName;
private javax.swing.JTextField txtSurname;
private javax.swing.JTextField txtTotal;
private javax.swing.ButtonGroup btnGrp;
EntryDlg obj=new EntryDlg(this);
String arr[]=new String[1000];
String arrAll[]=new String[1000];
int undoPos=-1;
String undoStr="";
public void intComponent()
{
viewList=new JList();
jScroll=new JScrollPane();
cmdDelete = new javax.swing.JButton();
cmdUndoDelete = new javax.swing.JButton();
cmdEntry = new javax.swing.JButton();
cmdEditMarks = new javax.swing.JButton();
cmdClose = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
txtName = new javax.swing.JTextField();
txtSurname = new javax.swing.JTextField();
txtID = new javax.swing.JTextField();
txtLabMark = new javax.swing.JTextField();
txtASg1 = new javax.swing.JTextField();
txtAsg2 = new javax.swing.JTextField();
txtFinal = new javax.swing.JTextField();
txtTotal = new javax.swing.JTextField();
txtGrade = new javax.swing.JTextField();
radioSortByName = new javax.swing.JRadioButton();
radioTotalMark = new javax.swing.JRadioButton();
btnGrp = new javax.swing.ButtonGroup();
getContentPane().setLayout(null);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jScroll.setViewportView(viewList);
getContentPane().add(jScroll);
jScroll.setBounds(10, 10, 230, 230);
cmdDelete.setText("Delete");
getContentPane().add(cmdDelete);
cmdDelete.setBounds(403, 10, 100, 23);
cmdUndoDelete.setText("Undo Delete");
getContentPane().add(cmdUndoDelete);
cmdUndoDelete.setBounds(403, 40, 100, 23);
cmdEntry.setText("Create Entry");
getContentPane().add(cmdEntry);
cmdEntry.setBounds(403, 70, 100, 23);
cmdEditMarks.setText("Edit Marks");
getContentPane().add(cmdEditMarks);
cmdEditMarks.setBounds(403, 100, 100, 23);
cmdClose.setText("Close");
getContentPane().add(cmdClose);
cmdClose.setBounds(403, 130, 100, 23);
jPanel1.setLayout(new java.awt.GridLayout(9, 0));
jLabel1.setText("Name");
jPanel1.add(jLabel1);
jLabel2.setText("Surname");
jPanel1.add(jLabel2);
jLabel3.setText("ID");
jPanel1.add(jLabel3);
jLabel4.setText("Lab Mark");
jPanel1.add(jLabel4);
jLabel5.setText("Asg1 Mark");
jPanel1.add(jLabel5);
jLabel6.setText("Asg2 Mark");
jPanel1.add(jLabel6);
jLabel7.setText("Final Exam Mark");
jPanel1.add(jLabel7);
jLabel8.setText("Total Mark");
jPanel1.add(jLabel8);
jLabel9.setText("Grade");
jPanel1.add(jLabel9);
getContentPane().add(jPanel1);
jPanel1.setBounds(40, 270, 150, 230);
jPanel2.setLayout(new java.awt.GridLayout(9, 0));
jPanel2.add(txtName);
jPanel2.add(txtSurname);
jPanel2.add(txtID);
jPanel2.add(txtLabMark);
jPanel2.add(txtASg1);
jPanel2.add(txtAsg2);
jPanel2.add(txtFinal);
jPanel2.add(txtTotal);
jPanel2.add(txtGrade);
txtName.setEnabled(false);
txtSurname.setEnabled(false);
txtID.setEnabled(false);
txtLabMark.setEnabled(false);
txtASg1.setEnabled(false);
txtAsg2.setEnabled(false);
txtFinal.setEnabled(false);
txtTotal.setEnabled(false);
txtGrade.setEnabled(false);
getContentPane().add(jPanel2);
jPanel2.setBounds(220, 270, 140, 200);
btnGrp.add(radioSortByName);
radioSortByName.setText("Sort by Full Name");
radioSortByName.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
radioSortByName.setMargin(new java.awt.Insets(0, 0, 0, 0));
getContentPane().add(radioSortByName);
radioSortByName.setBounds(380, 330, 150, 15);
btnGrp.add(radioTotalMark);
radioTotalMark.setText("Sort by the Total Mark");
radioTotalMark.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
radioTotalMark.setMargin(new java.awt.Insets(0, 0, 0, 0));
getContentPane().add(radioTotalMark);
radioTotalMark.setBounds(380, 370, 160, 15);
setSize(600,600);
setVisible(true);
cmdDelete.addActionListener(this);
cmdUndoDelete.addActionListener(this);
cmdEntry.addActionListener(this);
cmdEditMarks.addActionListener(this);
cmdClose .addActionListener(this);
radioSortByName.addActionListener(this);
radioTotalMark.addActionListener(this);
viewList.addListSelectionListener(this);
addWindowListener(new WindowAdapter(){
public void windowOpened(WindowEvent e){
/*try{
File f=new File("E://filename.ser");
ObjectInput in=new ObjectInputStream(new FileInputStream(f));
Object o;
o=in.readObject();
o=in.readObject();
Programming2Student ob=(Programming2Student)o;
ob.print();
DataInputStream din=new DataInputStream(new FileInputStream(f));
String arr[]=new String[1000];
int i=0;
while(din.available()>0)
{
String str=din.readLine();
String ss[]=str.split("##");
arr[i]="IdNo:"+ss[1];
i++;
}
viewList.setListData(arr);
}catch(Exception ex){System.out.println(ex);}*/
updateDetails();
}});
}
public boolean passed()
{
float result,lbm,asg1m,asg2m,finalm;
lbm=Float.parseFloat(txtLabMark.getText());
asg1m=Float.parseFloat(txtASg1.getText());
asg2m=Float.parseFloat(txtAsg2.getText());
finalm=Float.parseFloat(txtFinal.getText());
if((lbm+asg1m+asg2m)>=25 && finalm>=25)
return true;
else
return false;
}
public String getGrade()
{
float result,lbm,asg1m,asg2m,finalm;
lbm=Float.parseFloat(txtLabMark.getText());
asg1m=Float.parseFloat(txtASg1.getText());
asg2m=Float.parseFloat(txtAsg2.getText());
finalm=Float.parseFloat(txtFinal.getText());
result=lbm+asg1m+asg2m+finalm;
txtTotal.setText(String.valueOf(result));
String ans="";
if (passed())
{
if(result>=50 && result<=60)
ans ="P";
else if(result>=60 && result<=70)
ans ="C";
else if(result>=70 && result<=80)
ans ="D";
else if (result>=80)
{
ans ="HD";
}
}
else
{
if(result>=40 && result<=50)
ans ="MN";
else
ans ="NN";
}
return ans;
}
public void valueChanged(ListSelectionEvent e)
{
try{
String value=(String)viewList.getSelectedValue();
//System.out.println(value);
String arrId[]=value.split(";");
for (int i=0;i<arrAll.length ;i++ )
{
String arrAllSpl[]=arrAll[i].split(";;");
//System.out.println("xxx"+arrAllSpl[1]);
String chk=arrId[0].substring(5);
//System.out.println("ppp"+chk);
// arrAllSpl
if ((arrAllSpl[1].trim()).equals(chk))
{
txtName.setText(arrAllSpl[2]);
txtSurname.setText(arrAllSpl[2]);
txtID.setText(arrAllSpl[1]);
txtLabMark.setText(arrAllSpl[3]);
txtASg1.setText(arrAllSpl[4]);
txtAsg2.setText(arrAllSpl[5]);
float result,lbm,asg1m,asg2m,finalm;
lbm=Float.parseFloat(arrAllSpl[3]);
asg1m=Float.parseFloat(arrAllSpl[4]);
asg2m=Float.parseFloat(arrAllSpl[5]);
finalm=Float.parseFloat(arrAllSpl[6]);
result=lbm+asg1m+asg2m+finalm;
txtFinal.setText(arrAllSpl[6]);
txtTotal.setText(String.valueOf(result));
String rd="";
if(result<40)
{
rd="NN";
}
else if((result>=40 && result<=50) || (lbm+asg1m+asg2m)<25 || (finalm<25))
{
rd="MN";
}
else if((result>=50 && result<=60) && (lbm+asg1m+asg2m)>=25 && (finalm>=25))
{
rd="P";
}
else if ((result>=60 && result<=70) && (lbm+asg1m+asg2m)>=25 && (finalm>=25))
{
rd="C";
}
else if ((result>=70 && result<=80) && (lbm+asg1m+asg2m)>=25 && (finalm>=25))
{
rd="D";
}
else if(result>=80)
{
rd="HD";
}
txtGrade.setText(rd);
}
}
}catch(Exception ee){System.out.println(ee);}
}
public void updateDetails()
{
try{
File f=new File("filename.ser");
/*ObjectInput in=new ObjectInputStream(new FileInputStream(f));
Object o;
o=in.readObject();
o=in.readObject();
Programming2Student ob=(Programming2Student)o;
ob.print();*/
DataInputStream din=new DataInputStream(new FileInputStream(f));
arrAll=new String[1000];
arr=new String[1000];
int i=0;
while(din.available()>0)
{
String str=din.readLine();
arrAll[i]=str;
String ss[]=str.split(";;");
arr[i]="IdNo:"+ss[1]+";"+ss[2];
i++;
}
viewList.setListData(arr);
}catch(Exception ex){System.out.println(ex);}
}
public void actionPerformed(ActionEvent e)
{
System.out.println(e.getActionCommand());
if (e.getActionCommand().equals("Create Entry"))
{
obj=new EntryDlg(this);
obj.intComponent();
updateDetails();
}
if(e.getActionCommand().equals("Edit Marks"))
{
String value=(String)viewList.getSelectedValue();
if(value!=null)
{
cmdEditMarks.setText("Done");
txtLabMark.setEnabled(true);
txtASg1.setEnabled(true);
txtAsg2.setEnabled(true);
txtFinal.setEnabled(true);
viewList.setEnabled(false);
cmdDelete.setEnabled(false);
cmdUndoDelete.setEnabled(false);
cmdEntry.setEnabled(false);
cmdClose.setEnabled(false);
}
else
JOptionPane.showMessageDialog(null, "Please Select a Student","alert", JOptionPane.ERROR_MESSAGE);
}
else if(e.getActionCommand().equals("Done"))
{
int x=-1;
try{
if(Integer.parseInt(txtLabMark.getText())<0 || Float.parseFloat(txtLabMark.getText())>10)
{
x=1;
JOptionPane.showMessageDialog(null, "Enter a value between 0 to 10","alert", JOptionPane.ERROR_MESSAGE);
}
if(Integer.parseInt(txtASg1.getText())<0 || Integer.parseInt(txtASg1.getText())>15)
{x=1;
JOptionPane.showMessageDialog(null, "Enter a value between 0 to 15","alert", JOptionPane.ERROR_MESSAGE);
}
if(Integer.parseInt(txtAsg2.getText())<0 || Integer.parseInt(txtAsg2.getText())>25)
{x=1;
JOptionPane.showMessageDialog(null, "Enter a value between 0 to 25","alert", JOptionPane.ERROR_MESSAGE);
}
if(Integer.parseInt(txtFinal.getText())<0 || Integer.parseInt(txtFinal.getText())>50)
{x=1;
JOptionPane.showMessageDialog(null, "Enter a value between 0 to 50","alert", JOptionPane.ERROR_MESSAGE);
}
if(x!=1)
{
File f=new File("filename.ser");
String idno=txtID.getText().trim();
int j=-1;
for(j=0;j<arrAll.length;j++)
{
String arrVal[]=arrAll[j].split(";;");
if(arrVal[1].equals(idno))
{
break;
}
}
String linetoWrite=Integer.toString(j)+";;"+txtID.getText()+";;"+txtSurname.getText()+";;"+txtLabMark.getText()+";;"+txtASg1.getText()+";;"+txtAsg2.getText()+";;"+txtFinal.getText()+";;";
arrAll[j]=linetoWrite;
DataOutputStream dout=new DataOutputStream(new FileOutputStream(f));
for(int k=0;k<arrAll.length;k++)
{
if(arrAll[k]!=null)
dout.writeBytes(arrAll[k]+"\n");
else
break;
}
cmdEditMarks.setText("Edit Marks");
txtLabMark.setEnabled(false);
txtASg1.setEnabled(false);
txtAsg2.setEnabled(false);
txtFinal.setEnabled(false);
viewList.setEnabled(true);
cmdDelete.setEnabled(true);
cmdUndoDelete.setEnabled(true);
cmdEntry.setEnabled(true);
cmdClose.setEnabled(true);
}
String grd=getGrade();
txtGrade.setText(grd);
}
catch(NumberFormatException ex){JOptionPane.showMessageDialog(null, "Enter a valid Number","alert", JOptionPane.ERROR_MESSAGE); }
catch(Exception ex){System.out.println(ex);}
}
else if(e.getActionCommand().equals("Delete"))
{
try{
File f=new File("filename.ser");
String idno=txtID.getText().trim();
int j=-1;
for(j=0;j<arrAll.length;j++)
{
String arrVal[]=arrAll[j].split(";;");
if(arrVal[1].equals(idno))
{
break;
}
}
undoPos=j;
undoStr=arrAll[j];
int del=-1;
for(del=j;del<arrAll.length;del++)
{
if(arrAll[del]!=null)
arrAll[j]=arrAll[j+1];
else
break;
}
arrAll[del-1]=null;
DataOutputStream dout=new DataOutputStream(new FileOutputStream(f));
for(int k=0;k<arrAll.length;k++)
{
if(arrAll[k]!=null)
dout.writeBytes(arrAll[k]+"\n");
else
break;
}
dout.close();
}catch(Exception edel){}
updateDetails();
}
else if(e.getActionCommand().equals("Undo Delete"))
{
try{
File f=new File("filename.ser");
int k=-1;
for(k=0;k<arrAll.length;k++)
{
if(arrAll[k]==null)
break;
}
if (undoPos!=-1)
{
for (k=k;k>undoPos;k-- )
{
arrAll[k]=arrAll[k-1];
}
arrAll[undoPos]=undoStr;
undoPos=-1;
}
DataOutputStream dout=new DataOutputStream(new FileOutputStream(f));
for(k=0;k<arrAll.length;k++)
{
if(arrAll[k]!=null)
dout.writeBytes(arrAll[k]+"\n");
else
break;
}
dout.close();
updateDetails();
}catch(Exception eundo){}
}
else if (e.getActionCommand().equals("Close"))
{
System.exit(0);
}
}
public boolean checkID(String id)
{
updateDetails();
int x=-1;
for (int i=0;i<arrAll.length ;i++ )
{
String arrAllSpl[]=arrAll[i].split(";;");
//System.out.println("xxx"+arrAllSpl[1]);
//System.out.println("ppp"+chk);
// arrAllSpl
if ((arrAllSpl[1].trim()).equals(id))
{
JOptionPane.showMessageDialog(null, "Student Id exists","alert", JOptionPane.ERROR_MESSAGE);
x=0;
break;
}
else
x=1;
}
if(x==1)
return true;
else
return false;
}
}
class EntryDlg extends JDialog implements ActionListener
{
javax.swing.JTextField txtID;
javax.swing.JTextField txtname;
javax.swing.JTextField txtsurname;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private JButton cmdSave;
private JPanel p;
StdInfoViewer obj;
EntryDlg(StdInfoViewer x)
{
obj=x;
}
public void intComponent()
{
txtID=new javax.swing.JTextField();
txtname=new javax.swing.JTextField();
txtsurname=new javax.swing.JTextField();
jLabel1=new javax.swing.JLabel("ID");
jLabel2=new javax.swing.JLabel("Name");
jLabel3=new javax.swing.JLabel("Surname");
cmdSave=new JButton("Save");
p=new JPanel();
GridLayout g=new GridLayout(3,2);
// p.setBounds(0,0,100,200);
p.setLayout(g);
p.add(jLabel1);
p.add(txtID);
p.add(jLabel2);
p.add(txtname);
p.add(jLabel3);
p.add(txtsurname);
getContentPane().setLayout(null);
getContentPane().add(p);
p.setBounds(0,0,250,100);
getContentPane().add(cmdSave);
cmdSave.setBounds(20,120,80,30);
setSize(300,300);
setVisible(true);
cmdSave.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
obj.updateDetails();
}});
}
public void actionPerformed(ActionEvent e)
{Programming2Student objToWrite=new Programming2Student(this);
if(e.getActionCommand().equals("Save"))
{
try {
if(obj.checkID(txtID.getText()))
{
objToWrite.writeToFile();
obj.updateDetails();
txtID.setText("");
txtname.setText("");
txtsurname.setText("");
}
} catch (Exception ex) {
objToWrite.writeToFile();
obj.updateDetails();
txtID.setText("");
txtname.setText("");
txtsurname.setText("");
// System.out.println(e);
}
}
}
}