Advertisement
vencinachev

StudentGabi

Jun 10th, 2020
1,245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.89 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class Apl extends JFrame {
  6.     protected JButton ad;
  7.     protected JButton avgBtn;
  8.     protected JButton sortBtn;
  9.     protected JTextField tf[] = new JTextField[3];
  10.     protected List lst;
  11.  
  12.     protected JPanel contr, plst, footer;
  13.     protected JLabel avgLbl;
  14.     protected AdSt adSt;
  15.     protected Student prs[] = new Student[0];
  16.  
  17.     Apl(int x, int y, int ln, int ht) {
  18.         this.setLayout(new BorderLayout());
  19.         this.setBounds(x, y, ln, ht);
  20.         this.setVisible(true);
  21.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.         this.setTitle("Student's directory - version 1");
  23.  
  24.         contr = new JPanel(new FlowLayout());
  25.         plst = new JPanel(new FlowLayout());
  26.         footer = new JPanel(new FlowLayout());
  27.        
  28.         ad = new JButton("add");
  29.         contr.add(ad);
  30.        
  31.         avgBtn = new JButton("Average");
  32.         footer.add(avgBtn);
  33.         avgBtn.addActionListener(new AvgEvent());
  34.        
  35.         avgLbl = new JLabel("....");
  36.         footer.add(avgLbl);
  37.        
  38.         sortBtn = new JButton("Sort");
  39.         plst.add(sortBtn);
  40.         sortBtn.addActionListener(new SortEvent());
  41.        
  42.         tf[0] = new JTextField("name?", 10);
  43.         tf[1] = new JTextField("note?", 3);
  44.         tf[2] = new JTextField("ECN?", 10);
  45.         contr.add(tf[0]);
  46.         contr.add(tf[1]);
  47.         contr.add(tf[2]);
  48.        
  49.         ad.addActionListener(adSt = new AdSt());
  50.  
  51.         lst = new List(20);
  52.         plst.add(lst);
  53.        
  54.         add("North", contr);
  55.         add("Center", plst);
  56.         add("South", footer);
  57.        
  58.         revalidate();
  59.     }
  60.    
  61.     class SortEvent implements ActionListener {
  62.         public void actionPerformed(ActionEvent e) {
  63.             for (int i = 0; i < prs.length; i++)
  64.             {
  65.                 for (int j = i; j < prs.length; j++)
  66.                 {
  67.                     if (prs[i].compareTo(prs[j]) > 0)
  68.                     {
  69.                         Student temp = prs[i];
  70.                         prs[i] = prs[j];
  71.                         prs[j] = temp;
  72.                     }
  73.                 }
  74.             }
  75.             lst.removeAll();
  76.             for (int i = 0; i < prs.length; i++)
  77.             {
  78.                 lst.add("" + prs[i]);
  79.             }
  80.             revalidate();
  81.         }
  82.     }
  83.    
  84.     class AdSt implements ActionListener {
  85.         public void actionPerformed(ActionEvent e) {
  86.             Student s;
  87.             int nt;
  88.             String n = tf[0].getText();
  89.             String egn = tf[2].getText();
  90.             try {
  91.                 nt = Integer.parseInt(tf[1].getText());
  92.             } catch (NumberFormatException ex) {
  93.                 tf[0].setText("name?");
  94.                 tf[1].setText("note?");
  95.                 tf[2].setText("ECN?");
  96.                 return;
  97.             }
  98.             if (!egnCheck(egn))
  99.             {
  100.                 tf[0].setText("name?");
  101.                 tf[1].setText("note?");
  102.                 tf[2].setText("ECN?");
  103.                 return;
  104.             }
  105.             Student help[] = new Student[prs.length + 1];
  106.             System.arraycopy(prs, 0, help, 0, prs.length);
  107.             help[help.length - 1] = s = new Student(n, nt, egn);
  108.             prs = help;
  109.             tf[0].setText("name?");
  110.             tf[1].setText("note?");
  111.             tf[2].setText("ECN?");
  112.             lst.add("" + s);
  113.             revalidate();
  114.         }
  115.     }
  116.    
  117.     class AvgEvent implements ActionListener {
  118.         public void actionPerformed(ActionEvent e) {
  119.             int sum = 0;
  120.             for (int i = 0; i < prs.length; i++) {
  121.                 sum += prs[i].note;
  122.             }
  123.             double average = (float)sum / prs.length;
  124.             avgLbl.setText(String.format("Average %.2f", average));
  125.             revalidate();
  126.         }
  127.     }
  128.    
  129.     public boolean egnCheck(String egn)
  130.     {
  131.         if (egn.length() != 10)
  132.         {
  133.             return false;
  134.         }
  135.         for (int i = 0; i < egn.length(); i++)
  136.         {
  137.             if (!Character.isDigit(egn.charAt(i)))
  138.             {
  139.                 return false;
  140.             }
  141.         }
  142.         int[] digits = new int[10];
  143.         for (int i = 0; i < egn.length(); i++)
  144.         {
  145.             digits[i] = Character.getNumericValue(egn.charAt(i));
  146.         }
  147.         int month = digits[2]*10 + digits[3];
  148.         if (month > 12)
  149.         {
  150.             return false;
  151.         }
  152.         int day = digits[4]*10 + digits[5];
  153.         if (month > 31)
  154.         {
  155.             return false;
  156.         }
  157.         int control = 0;
  158.         for (int i = 1; i <= 9; i++)
  159.         {
  160.             control += digits[i-1] * (Math.pow(2, i) % 11);
  161.         }
  162.         control = control % 11;
  163.         if (control == 10)
  164.         {
  165.             control = 0;
  166.         }
  167.         if (control != digits[9])
  168.         {
  169.             return false;
  170.         }
  171.         return true;
  172.     }
  173.    
  174.     public static void main(String[] arg) {
  175.         Apl apl = new Apl(20, 20, 400, 300);
  176.     }
  177.    
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement