Toxotsist

T9

Nov 30th, 2021 (edited)
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 KB | None | 0 0
  1. public class InnException extends Exception {
  2.     public String ExceptionMessage() {
  3.         System.err.println("Введен некорректный инн!");
  4.         return "Введен некорректный инн!";
  5.     }
  6. }
  7. /////////////////////////////////////////////////////
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.util.*;
  13. import java.util.List;
  14.  
  15. public class LabClassUI extends JFrame {
  16.     List<Long> arr = new ArrayList<>();
  17.     private JLabel info = new JLabel("Список: " + arr);
  18.     private JButton sortBut = new JButton("Сортировать");
  19.     private JTextField number = new JTextField(3);
  20.     private JButton searchBut = new JButton("Найти ИНН");
  21.     public List<Long> getArr() {
  22.         return arr;
  23.     }
  24.  
  25.     public LabClassUI() {
  26.         super("T9");
  27.         this.setBounds(200,200,640,640);
  28.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  29.         info.setHorizontalAlignment(info.CENTER);
  30.         number.setHorizontalAlignment(number.CENTER);
  31.         sortBut.setHorizontalAlignment(sortBut.CENTER);
  32.         searchBut.setHorizontalAlignment(searchBut.CENTER);
  33.         sortBut.addActionListener(new ActionListener() {
  34.             @Override
  35.             public void actionPerformed(ActionEvent e) {
  36.                 long x = Long.parseLong(number.getText());
  37.                 try {
  38.                     if (checkInn(x)) arr.add(x);
  39.                 }
  40.                 catch (InnException e1) {
  41.                    // e1.ExceptionMessage();
  42.                     JOptionPane.showMessageDialog(LabClassUI.this, e1.ExceptionMessage());
  43.                 }
  44.                 //arr.add(x);
  45.                 Collections.sort(arr, new Comparator<Long>() {
  46.                     @Override
  47.                     public int compare(Long o1, Long o2) {
  48.                         return (int) (o2 - o1);
  49.                     }
  50.                 });
  51.                 info.setText("Список:" + arr);
  52.             }
  53.         });
  54.         searchBut.addActionListener(new ActionListener() {
  55.             @Override
  56.             public void actionPerformed(ActionEvent e) {
  57.                 try {
  58.                     if (checkUser(Long.parseLong(number.getText()))) JOptionPane.showMessageDialog(LabClassUI.this, "ИНН найден!");
  59.                 } catch (Exception e2) {
  60.                     JOptionPane.showMessageDialog(LabClassUI.this, "ИНН не найден!");
  61.                 }
  62.             }
  63.         });
  64.         JPanel panel = new JPanel();
  65.         GridLayout lay = new GridLayout(2,2,5,5);
  66.         panel.setLayout(lay);
  67.         panel.add(info);
  68.         panel.add(number);
  69.         panel.add(sortBut);
  70.         panel.add(searchBut);
  71.         getContentPane().add(panel);
  72.  
  73.         setVisible(true);
  74.     }
  75.  
  76.     private boolean checkInn(long inn) throws InnException {
  77.         if(inn/100000000000L < 1 | inn/100000000000L > 9 | inn<0) {
  78.             throw new InnException(); }
  79.         else return true;
  80.     }
  81.  
  82.     public boolean checkUser(long inn) throws InnNotFoundException{
  83.         if (!this.arr.contains(inn)) throw new InnNotFoundException();
  84.         else return true;
  85.     }
  86.  
  87.     public static void main(String[] args) {
  88.         LabClassUI gui = new LabClassUI();
  89.     }
  90. }
  91. //////////////////////////////////////
  92. public class InnNotFoundException extends Exception {
  93.     public String ExceptionMessage() {
  94.         System.err.println("Введен некорректный инн!");
  95.         return "Введен некорректный инн!";
  96.     }
  97. }
  98.  
Add Comment
Please, Sign In to add comment