nikeza

Excel Functions

Oct 22nd, 2019
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.66 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Comparator;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Excel_Functions {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         int n = Integer.parseInt(scanner.nextLine());
  12.  
  13.         String[][] matrix = new String[n][n];
  14.         String[] header = scanner.nextLine().split(", ");
  15.         List<String> list = new ArrayList<>();
  16.         for (int i = 0; i < n-1; i++) {
  17.             list.add(scanner.nextLine());
  18.         }
  19.         String[] command = scanner.nextLine().split("\\s+");
  20.        List<String> newList = new ArrayList<>();
  21.  
  22.         switch (command[0]) {
  23.             case "hide":
  24.                 String nameH = command[1];
  25.                 int indexH = -1;
  26.                 for (int i = 0; i < header.length; i++) {
  27.                     if (header[i].equals(command[1])) {
  28.                         indexH = i;
  29.                         int finalIndex = indexH;
  30.  
  31.                     }
  32.                 }
  33.  
  34.                 for (int i = 0; i < header.length; i++) {
  35.                     if (i != indexH) {
  36.                         if (i != header.length - 1) {
  37.                             System.out.print(header[i] + " | ");
  38.                         } else {
  39.                             System.out.print(header[i]);
  40.                         }
  41.                     }
  42.                 }
  43.                 System.out.println();
  44.                 for (int i = 0; i <list.size() ; i++) {
  45.                     String[] text =list.get(i).split(", ");
  46.                     for (int j = 0; j <header.length ; j++) {
  47.                         if (j != indexH) {
  48.                             if (j != header.length - 1) {
  49.                                 System.out.print(text[j] + " | ");
  50.                             } else {
  51.                                 System.out.print(text[j]);
  52.                             }
  53.                         }
  54.                     }
  55.                     System.out.println();
  56.                 }
  57.  
  58.                 break;
  59.             case "sort":
  60.  
  61.                 for (int i = 0; i < header.length; i++) {
  62.  
  63.                     if (i != header.length - 1) {
  64.                         System.out.print(header[i] + " | ");
  65.                     } else {
  66.                         System.out.print(header[i]);
  67.                     }
  68.                 }
  69.                 System.out.println();
  70.                 int index = -1;
  71.                 for (int i = 0; i < header.length; i++) {
  72.                     if (header[i].equals(command[1])) {
  73.                         index = i;
  74.                         int finalIndex = index;
  75.                        newList=list.stream().sorted(Comparator.comparing(f -> f.split(", ")[finalIndex])).collect(Collectors.toList());
  76.                     }
  77.                 }
  78.                 for (int i = 0; i <newList.size() ; i++) {
  79.                    String[] text =newList.get(i).split(", ");
  80.                         for (int j = 0; j <header.length ; j++) {
  81.                             if (j!=header.length-1){
  82.                                 System.out.print(text[j]+" | ");
  83.                             }else   {
  84.                                 System.out.print(text[j]);
  85.                             }
  86.                         }
  87.                     System.out.println();
  88.                     }
  89.  
  90.                 break;
  91.             case "filter":
  92.  
  93.                 for (int i = 0; i < header.length; i++) {
  94.  
  95.                     if (i != header.length - 1) {
  96.                         System.out.print(header[i] + " | ");
  97.                     } else {
  98.                         System.out.print(header[i]);
  99.                     }
  100.                 }
  101.                 System.out.println();
  102.                 String name = command[2];
  103.                 int indexF = -1;
  104.                 for (int i = 0; i < header.length; i++) {
  105.                     if (header[i].equals(command[1])) {
  106.                         indexF = i;
  107.                         int finalIndex = indexF;
  108.                         newList=list.stream()
  109.                                 .filter(f->f.split(", ")[finalIndex].equals(name))
  110.                                 .collect(Collectors.toList());
  111.  
  112.                     }
  113.                 }
  114.                 for (int i = 0; i <newList.size() ; i++) {
  115.                     String[] text =newList.get(i).split(", ");
  116.                     for (int j = 0; j <header.length ; j++) {
  117.                         if (j!=header.length-1){
  118.                             System.out.print(text[j]+" | ");
  119.                         }else   {
  120.                             System.out.print(text[j]);
  121.                         }
  122.                     }
  123.                     System.out.println();
  124.                 }
  125.                 break;
  126.         }
  127.  
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment