Advertisement
apl-mhd

JavaOnline

Dec 28th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.22 KB | None | 0 0
  1. package finalAssignment;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8. public class Main extends JFrame {
  9.  
  10.   public   JButton buttons[] = new JButton[7];
  11.  
  12.     static Main a;
  13.  
  14.     public Main(){
  15.  
  16.         super("Assignment One");
  17.         setLayout(new FlowLayout());
  18.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
  19.  
  20.         buttons[0] = new JButton("Add");
  21.         buttons[1] = new JButton("Remove");
  22.         buttons[2] = new JButton("Save");
  23.         buttons[3] = new JButton("Sort By First Name");
  24.         buttons[4] = new JButton("Sort By Last Name");
  25.         buttons[5] = new JButton("Sort By PID");
  26.         buttons[6] = new JButton("Search");
  27.  
  28.  
  29.         for(int i=0; i<7; i++)
  30.             add(buttons[i]);
  31.  
  32.         buttons[0].addActionListener(new ActionListener() {
  33.             @Override
  34.             public void actionPerformed(ActionEvent e) {
  35.                 Update.add();
  36.             }
  37.         });
  38.  
  39.  
  40.         buttons[1].addActionListener(new ActionListener() {
  41.             @Override
  42.             public void actionPerformed(ActionEvent e) {
  43.                 Update.remove();
  44.             }
  45.         });
  46.  
  47.  
  48.  
  49.         buttons[2].addActionListener(new ActionListener() {
  50.             @Override
  51.             public void actionPerformed(ActionEvent e) {
  52.                 Update.save();
  53.             }
  54.         });
  55.         buttons[3].addActionListener(new ActionListener() {
  56.             @Override
  57.             public void actionPerformed(ActionEvent e) {
  58.                 Update.sortByFirstName();
  59.             }
  60.         });
  61.  
  62.         buttons[4].addActionListener(new ActionListener() {
  63.             @Override
  64.             public void actionPerformed(ActionEvent e) {
  65.                 Update.sortByLastName();
  66.             }
  67.         });
  68.  
  69.         buttons[5].addActionListener(new ActionListener() {
  70.             @Override
  71.             public void actionPerformed(ActionEvent e) {
  72.                 Update.sortByPID();
  73.             }
  74.         });
  75.  
  76.         buttons[6].addActionListener(new ActionListener() {
  77.             @Override
  78.             public void actionPerformed(ActionEvent e) {
  79.                 Update.serch();
  80.             }
  81.         });
  82.  
  83.  
  84.  
  85.  
  86.  
  87.         // buttons[1].addActionListener(new TestClass(buttons[1]));
  88.  
  89.  
  90.         setSize(800,400);
  91.         setVisible(true);
  92.  
  93.     }
  94.  
  95.     public static void main(String[] args) {
  96.  
  97.  
  98.      a =  new Main();
  99.  
  100.  
  101.     }
  102. }
  103.  
  104.  
  105.  
  106. .................................................................
  107. package finalAssignment;
  108.  
  109. import java.io.*;
  110. import java.util.ArrayList;
  111. import java.util.Collections;
  112. import java.util.HashMap;
  113. import java.util.Scanner;
  114.  
  115. public class Update {
  116.  
  117.  
  118.     static ArrayList<Person> arrayList = new ArrayList<>();
  119.     static HashMap<Person,Boolean> hashMap = new HashMap<>();
  120.  
  121.  
  122.    static void dataLoadFromfile() throws IOException{ /*data load from file*/
  123.  
  124.        if (1==1) {
  125.  
  126.            FileReader fileReader = new FileReader("PersonalInformation.txt");
  127.            BufferedReader bufferedReader = new BufferedReader(fileReader);
  128.  
  129.  
  130.            String s = bufferedReader.readLine();
  131.  
  132.  
  133.            String arr[] = s.split(" ");
  134.            HashMap<Person, Boolean> hashMap = new HashMap<>();
  135.  
  136.            while (true) {
  137.  
  138.                arrayList.add(new Person(arr[0], arr[1], arr[2]));
  139.                //System.out.println(s);
  140.                s = bufferedReader.readLine();
  141.                if (s == null) break;
  142.                arr = s.split(" ");
  143.            }
  144.  
  145.  
  146.            fileReader.close();
  147.            bufferedReader.close();
  148.  
  149.      /*      for (Person o:arrayList){
  150.  
  151.                System.out.println("put in hash map: "+o);
  152.                hashMap.put(o,true);
  153.            }*/
  154.  
  155.         /*   System.out.println("x "+arrayList.get(0));
  156.            hashMap.put(arrayList.get(0),true);
  157.            System.out.println(hashMap.get(arrayList.get(0)));
  158. */
  159.  
  160.        }
  161.  
  162.        else throw  new IOException("not found");
  163.  
  164.  
  165.     }
  166.  
  167.     static  {
  168.         try {
  169.             Update.dataLoadFromfile();
  170.  
  171.            // System.out.println("tostring "+arrayList.get(0)+"\n");
  172.         }
  173.         catch (IOException e){
  174.  
  175.             System.out.println("Error");
  176.         }
  177.     }
  178.  
  179.     static  void add(){
  180.  
  181.             for (Person o:arrayList){
  182.  
  183.              //  System.out.println("put in hash map: "+o);
  184.                hashMap.put(o,true);
  185.            }
  186.  
  187.         Scanner in = new Scanner(System.in);
  188.  
  189.  
  190.         System.out.println("Please Enter Your First Name Last Name and PID(same line):\n");
  191.         String info = in.nextLine();
  192.         String infoArr[] = info.split(" ");
  193.  
  194.         Person ob = new Person(infoArr[0],infoArr[1],infoArr[2]);
  195.         //System.out.println("after input: "+ob);
  196.  
  197.        // hashMap.put(ob,true);
  198.       //  System.out.println(hashMap.size());
  199.  
  200.  
  201.         if(null == hashMap.get(ob)){
  202.  
  203.             arrayList.add(ob);
  204.  
  205.            // System.out.println("arralist size "+arrayList.size());
  206.         }
  207.         else{
  208.  
  209.             System.out.println("Allready exist");
  210.         }
  211.  
  212.  
  213.         //System.out.println("tostring "+arrayList.get(0));
  214.  
  215.  
  216.  
  217.     }
  218.  
  219.  
  220. static  void writeTofile() throws IOException{
  221.  
  222.    // System.out.println("write time arraylist size "+arrayList.size());
  223.  
  224.         if(1==1) {
  225.             FileWriter fileWriter = new FileWriter("PersonalInformation.txt");
  226.             BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
  227.  
  228.             for (Person i : arrayList) {
  229.  
  230.                 String line = i.firstName + " " + i.lastName + " " + i.pid + "\n";
  231.  
  232.  
  233.  
  234.                 bufferedWriter.write(line);
  235.      //           System.out.println("Write :"+line);
  236.  
  237.  
  238.             }
  239.  
  240.             bufferedWriter.close();
  241.             fileWriter.close();
  242.         }
  243.  
  244.         else throw new IOException("Error");
  245.     }
  246.  
  247.  
  248.  
  249.     static  void remove(){
  250.  
  251.         Scanner in = new Scanner(System.in);
  252.         System.out.println("Enter PID for remove");
  253.  
  254.         String pidStr = in.nextLine();
  255.  
  256.         for(Person i:arrayList){
  257.  
  258.  
  259.             if(i.pid.equals(in)){
  260.  
  261.                 arrayList.remove(i);
  262.  
  263.                 hashMap.remove(i);
  264.             }
  265.  
  266.         }
  267.  
  268.  
  269.     }
  270.  
  271.     static  void save(){
  272.  
  273.        try {
  274.  
  275.            writeTofile();
  276.        }
  277.        catch (IOException e){
  278.  
  279.  
  280.        }
  281.  
  282.  
  283.     }
  284.  
  285.     static  void sortByFirstName(){
  286.  
  287.         Collections.sort(arrayList, new SortByFirstName());
  288.         System.out.println("Sor By FirstName:\n");
  289.  
  290.         for(Person i:arrayList){
  291.             System.out.println(i);
  292.         }
  293.  
  294.  
  295.  
  296.     }
  297.  
  298.     static  void sortByLastName(){
  299.  
  300.         Collections.sort(arrayList, new SortByLastName());
  301.         System.out.println("Sort By Last Name:\n");
  302.         for(Person i:arrayList){
  303.  
  304.             System.out.println(i);
  305.         }
  306.  
  307.     }
  308.  
  309.     static  void sortByPID(){
  310.  
  311.         Collections.sort(arrayList, new SortByPID());
  312.         System.out.println("Sort By PID:\n");
  313.         for(Person i:arrayList){
  314.  
  315.             System.out.println(i);
  316.         }
  317.  
  318.     }
  319.  
  320.     static  void serch(){
  321.  
  322.         Scanner in = new Scanner(System.in);
  323.         System.out.println("Enter PID For Search: ");
  324.  
  325.         String pidStr = in.next();
  326.  
  327.         for(Person i:arrayList){
  328.  
  329.             if(i.pid.equals(pidStr)){
  330.  
  331.                 System.out.println(i);
  332.             }
  333.  
  334.             else
  335.                 System.out.println("PID not Found:");
  336.         }
  337.  
  338.     }
  339.  
  340.  
  341. }
  342. ..............................................
  343. package finalAssignment;
  344.  
  345. import java.util.Comparator;
  346.  
  347. public class SortByLastName implements Comparator<Person> {
  348.  
  349.     @Override
  350.     public int compare(Person o1, Person o2) {
  351.         return o1.lastName.compareTo(o2.lastName);
  352.     }
  353. }
  354.  
  355. .........................................
  356.  
  357. package finalAssignment;
  358.  
  359. import java.util.Comparator;
  360.  
  361. public class SortByPID implements Comparator<Person> {
  362.  
  363.     @Override
  364.     public int compare(Person o1, Person o2) {
  365.         return o1.pid.compareTo(o2.pid);
  366.     }
  367. }
  368.  
  369.  
  370.  
  371.  
  372. package finalAssignment;
  373.  
  374. import java.util.Comparator;
  375.  
  376. public class SortByFirstName implements Comparator<Person> {
  377.  
  378.     @Override
  379.     public int compare(Person o1, Person o2) {
  380.         return o1.firstName.compareTo(o2.firstName);
  381.     }
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement