Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Apl extends JFrame {
- protected JButton ad;
- protected JButton avgBtn;
- protected JButton sortBtn;
- protected JTextField tf[] = new JTextField[3];
- protected List lst;
- protected JPanel contr, plst, footer;
- protected JLabel avgLbl;
- protected AdSt adSt;
- protected Student prs[] = new Student[0];
- Apl(int x, int y, int ln, int ht) {
- this.setLayout(new BorderLayout());
- this.setBounds(x, y, ln, ht);
- this.setVisible(true);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setTitle("Student's directory - version 1");
- contr = new JPanel(new FlowLayout());
- plst = new JPanel(new FlowLayout());
- footer = new JPanel(new FlowLayout());
- ad = new JButton("add");
- contr.add(ad);
- avgBtn = new JButton("Average");
- footer.add(avgBtn);
- avgBtn.addActionListener(new AvgEvent());
- avgLbl = new JLabel("....");
- footer.add(avgLbl);
- sortBtn = new JButton("Sort");
- plst.add(sortBtn);
- sortBtn.addActionListener(new SortEvent());
- tf[0] = new JTextField("name?", 10);
- tf[1] = new JTextField("note?", 3);
- tf[2] = new JTextField("ECN?", 10);
- contr.add(tf[0]);
- contr.add(tf[1]);
- contr.add(tf[2]);
- ad.addActionListener(adSt = new AdSt());
- lst = new List(20);
- plst.add(lst);
- add("North", contr);
- add("Center", plst);
- add("South", footer);
- revalidate();
- }
- class SortEvent implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- for (int i = 0; i < prs.length; i++)
- {
- for (int j = i; j < prs.length; j++)
- {
- if (prs[i].compareTo(prs[j]) > 0)
- {
- Student temp = prs[i];
- prs[i] = prs[j];
- prs[j] = temp;
- }
- }
- }
- lst.removeAll();
- for (int i = 0; i < prs.length; i++)
- {
- lst.add("" + prs[i]);
- }
- revalidate();
- }
- }
- class AdSt implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- Student s;
- int nt;
- String n = tf[0].getText();
- String egn = tf[2].getText();
- try {
- nt = Integer.parseInt(tf[1].getText());
- } catch (NumberFormatException ex) {
- tf[0].setText("name?");
- tf[1].setText("note?");
- tf[2].setText("ECN?");
- return;
- }
- if (!egnCheck(egn))
- {
- tf[0].setText("name?");
- tf[1].setText("note?");
- tf[2].setText("ECN?");
- return;
- }
- Student help[] = new Student[prs.length + 1];
- System.arraycopy(prs, 0, help, 0, prs.length);
- help[help.length - 1] = s = new Student(n, nt, egn);
- prs = help;
- tf[0].setText("name?");
- tf[1].setText("note?");
- tf[2].setText("ECN?");
- lst.add("" + s);
- revalidate();
- }
- }
- class AvgEvent implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- int sum = 0;
- for (int i = 0; i < prs.length; i++) {
- sum += prs[i].note;
- }
- double average = (float)sum / prs.length;
- avgLbl.setText(String.format("Average %.2f", average));
- revalidate();
- }
- }
- public boolean egnCheck(String egn)
- {
- if (egn.length() != 10)
- {
- return false;
- }
- for (int i = 0; i < egn.length(); i++)
- {
- if (!Character.isDigit(egn.charAt(i)))
- {
- return false;
- }
- }
- int[] digits = new int[10];
- for (int i = 0; i < egn.length(); i++)
- {
- digits[i] = Character.getNumericValue(egn.charAt(i));
- }
- int month = digits[2]*10 + digits[3];
- if (month > 12)
- {
- return false;
- }
- int day = digits[4]*10 + digits[5];
- if (month > 31)
- {
- return false;
- }
- int control = 0;
- for (int i = 1; i <= 9; i++)
- {
- control += digits[i-1] * (Math.pow(2, i) % 11);
- }
- control = control % 11;
- if (control == 10)
- {
- control = 0;
- }
- if (control != digits[9])
- {
- return false;
- }
- return true;
- }
- public static void main(String[] arg) {
- Apl apl = new Apl(20, 20, 400, 300);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement