Advertisement
SensaBG

EditExam

Dec 1st, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class newTwo {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         Map<String, StringBuilder> book = new HashMap<>();
  11.         String[] notes = scanner.nextLine().split(" \\| ");
  12.  
  13.         for (String note : notes) {
  14.             String[] data = note.split(": ");
  15.             String word = data[0];
  16.             String def = data[1];
  17.  
  18.             book.putIfAbsent(word, new StringBuilder());
  19.             book.get(word).append(" -").append(def).append(System.lineSeparator());
  20.         }
  21.  
  22.         notes = scanner.nextLine().split(" \\| ");
  23.         String command = scanner.nextLine();
  24.  
  25. //        boolean isExamTime = "Test".equals(command);
  26.  
  27.         for (String currentTopic : notes) {
  28.             String topicFromBook = "\n" + book.get(currentTopic);
  29.            
  30.             if (book.containsKey(currentTopic)) {
  31.                
  32.                 if("Test".equals(command)){
  33.                     System.out.println(currentTopic + ":" + topicFromBook);
  34.                 } else if ("Hand Over".equals(command)){
  35.                     System.out.println(currentTopic + " ");
  36.                 }
  37.             }
  38.         }
  39.     }
  40. }
  41.  
  42.                 // renamed to topicFromBook as its more accurate
  43.                 // replaced the System.lineSeparator() with "\n"
  44.                 // although System.lineSeparator() is better
  45.                 // since it runs on all systems, whereas the "\n"
  46.                 // might interfere with some systems like Linux
  47.                 // ->  moved it on line 28
  48. //                String topicFromBook = System.lineSeparator() + book.get(currentTopic);
  49.                
  50.                 // split the ternary operator into an if else if statement to be more precise,
  51.                 // although the task doesn't require it.
  52.                 // the if else if statement is nested in another if statement
  53.                 // as we first have to check if our book with past notes contains
  54.                 // the particular topics our teacher is asking us
  55.                 // print only the topics saved in your book depending on
  56.                 //    1.  whether is an exam -> print the
  57.                 //
  58.                 //        -> print  everything "Title(s)" ( word ) and Paragraph(s) ( def )
  59.                 //
  60.                 //    2.  whether is a just a brief check if you pay attention in Class
  61.                 //
  62.                 //        -> print  the "Title(s)" ( word ) and Paragraph ( def )
  63.                 //
  64.                 // -> across lines 30 & 36
  65. //                System.out.print(isExamTime ? currentTopic + ":" + topicFromBook : currentTopic + " ");
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement