Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class Excel_Functions {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- String[][] matrix = new String[n][n];
- String[] header = scanner.nextLine().split(", ");
- List<String> list = new ArrayList<>();
- for (int i = 0; i < n-1; i++) {
- list.add(scanner.nextLine());
- }
- String[] command = scanner.nextLine().split("\\s+");
- List<String> newList = new ArrayList<>();
- switch (command[0]) {
- case "hide":
- String nameH = command[1];
- int indexH = -1;
- for (int i = 0; i < header.length; i++) {
- if (header[i].equals(command[1])) {
- indexH = i;
- int finalIndex = indexH;
- }
- }
- for (int i = 0; i < header.length; i++) {
- if (i != indexH) {
- if (i != header.length - 1) {
- System.out.print(header[i] + " | ");
- } else {
- System.out.print(header[i]);
- }
- }
- }
- System.out.println();
- for (int i = 0; i <list.size() ; i++) {
- String[] text =list.get(i).split(", ");
- for (int j = 0; j <header.length ; j++) {
- if (j != indexH) {
- if (j != header.length - 1) {
- System.out.print(text[j] + " | ");
- } else {
- System.out.print(text[j]);
- }
- }
- }
- System.out.println();
- }
- break;
- case "sort":
- for (int i = 0; i < header.length; i++) {
- if (i != header.length - 1) {
- System.out.print(header[i] + " | ");
- } else {
- System.out.print(header[i]);
- }
- }
- System.out.println();
- int index = -1;
- for (int i = 0; i < header.length; i++) {
- if (header[i].equals(command[1])) {
- index = i;
- int finalIndex = index;
- newList=list.stream().sorted(Comparator.comparing(f -> f.split(", ")[finalIndex])).collect(Collectors.toList());
- }
- }
- for (int i = 0; i <newList.size() ; i++) {
- String[] text =newList.get(i).split(", ");
- for (int j = 0; j <header.length ; j++) {
- if (j!=header.length-1){
- System.out.print(text[j]+" | ");
- }else {
- System.out.print(text[j]);
- }
- }
- System.out.println();
- }
- break;
- case "filter":
- for (int i = 0; i < header.length; i++) {
- if (i != header.length - 1) {
- System.out.print(header[i] + " | ");
- } else {
- System.out.print(header[i]);
- }
- }
- System.out.println();
- String name = command[2];
- int indexF = -1;
- for (int i = 0; i < header.length; i++) {
- if (header[i].equals(command[1])) {
- indexF = i;
- int finalIndex = indexF;
- newList=list.stream()
- .filter(f->f.split(", ")[finalIndex].equals(name))
- .collect(Collectors.toList());
- }
- }
- for (int i = 0; i <newList.size() ; i++) {
- String[] text =newList.get(i).split(", ");
- for (int j = 0; j <header.length ; j++) {
- if (j!=header.length-1){
- System.out.print(text[j]+" | ");
- }else {
- System.out.print(text[j]);
- }
- }
- System.out.println();
- }
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment