Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. int row=Integer.parseInt(scanner.nextLine());
  9. String[][] matrix=new String[row][];
  10. for (int i = 0; i < row; i++) {
  11. String line=scanner.nextLine();
  12. matrix[i]=line.split(", ");
  13. }
  14. String[] command=scanner.nextLine().split(" ");
  15.  
  16. if("hide".equals(command[0])) {
  17. hideColumnAndPrint(matrix,command[1]);
  18. } else if("sort".equals(command[0])) {
  19. sortByColumnAndPrint(matrix,command[1]);
  20. } else if("filter".equals(command[0])) {
  21. filterByColumnAndPrint(matrix,command[1],command[2]);
  22. }
  23. }
  24. private static void filterByColumnAndPrint(String[][] matrix, String filterColumn, String filterValue) {
  25. int filterColumnIndex=getColumnIndexByName(matrix[0],filterColumn);
  26. System.out.println(String.join(" | ",matrix[0]));
  27. for (int r = 1; r < matrix.length; r++) {
  28. if(matrix[r][filterColumnIndex].equals(filterValue)) {
  29. System.out.println(String.join(" | ",matrix[r]));
  30. }
  31. }
  32. }
  33. private static void sortByColumnAndPrint(String[][] matrix, String sortColumn) {
  34. int sortColumnIndex=getColumnIndexByName(matrix[0],sortColumn);
  35. for (int i = 0; i < matrix.length; i++) {
  36. for (int r = 1; r < matrix.length-1-i; r++) {
  37. if(matrix[r][sortColumnIndex]
  38. .compareTo(matrix[r+1][sortColumnIndex])>0) {
  39. String[] temp=matrix[r];
  40. matrix[r]=matrix[r+1];
  41. matrix[r+1]=temp;
  42. }
  43. }
  44. }
  45. for (int r = 0; r < matrix.length; r++) {
  46. for (int c = 0; c < matrix[r].length; c++) {
  47. System.out.print(matrix[r][c]);
  48. if(c!=matrix[r].length-1) {
  49. System.out.print(" | ");
  50. }
  51. }
  52. System.out.println();
  53. }
  54. }
  55. private static void hideColumnAndPrint(String[][] matrix, String hiddenColumn) {
  56. int hiddenColumnIndex=getColumnIndexByName(matrix[0], hiddenColumn);
  57. for (int r = 0; r < matrix.length; r++) {
  58. for (int c = 0; c < matrix[r].length; c++) {
  59. if(c==hiddenColumnIndex){
  60. continue;
  61. }
  62. System.out.print(matrix[r][c]);
  63. if(c!=matrix[r].length-1) {
  64. if (!(c == matrix[r].length - 2 && hiddenColumnIndex == c + 1)) {
  65. System.out.print(" | ");
  66. }
  67. }
  68. }
  69. System.out.println();
  70. }
  71. }
  72. private static int getColumnIndexByName(String[] matrix, String columnName) {
  73. int hiddenColumnIndex = -1;
  74. for (int r = 0; r < matrix.length; r++) {
  75. if(matrix[r].equals(columnName)) {
  76. hiddenColumnIndex=r;
  77. break;
  78. }
  79. }
  80. return hiddenColumnIndex;
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement