Advertisement
CR7CR7

Concert

Apr 10th, 2023
757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Concert {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         // A map to store the bands and their members
  8.         Map<String, List<String>> bands = new LinkedHashMap<>();
  9.         // A map to store the bands and their time on stage
  10.         Map<String, Integer> times = new TreeMap<>(Comparator.reverseOrder());
  11.         // A variable to store the total time
  12.         int totalTime = 0;
  13.         // A variable to store the first band on stage
  14.         String firstBand = "";
  15.        
  16.         // Read commands until "Start!" is entered
  17.         String command = scanner.nextLine();
  18.         while (!command.equals("Start!")) {
  19.             // Split the command by space
  20.             String[] tokens = command.split("\\s+");
  21.             // If the command starts with "Add"
  22.             if (tokens[0].equals("Add")) {
  23.                 // Get the band name and the members
  24.                 String bandName = tokens[1];
  25.                 List<String> members = new ArrayList<>(Arrays.asList(tokens).subList(2, tokens.length));
  26.                 // If the band does not exist in the map, add it with its members
  27.                 if (!bands.containsKey(bandName)) {
  28.                     bands.put(bandName, members);
  29.                 } else {
  30.                     // Otherwise, add only the members that are not already in the list
  31.                     List<String> existingMembers = bands.get(bandName);
  32.                     for (String member : members) {
  33.                         if (!existingMembers.contains(member)) {
  34.                             existingMembers.add(member);
  35.                         }
  36.                     }
  37.                 }
  38.             }
  39.             // If the command starts with "Play"
  40.             if (tokens[0].equals("Play")) {
  41.                 // Get the band name and the time
  42.                 String bandName = tokens[1];
  43.                 int time = Integer.parseInt(tokens[2]);
  44.                 // If the band does not exist in the map, add it with its time
  45.                 if (!times.containsKey(bandName)) {
  46.                     times.put(bandName, time);
  47.                     // If this is the first band on stage, set it as such
  48.                     if (firstBand.isEmpty()) {
  49.                         firstBand = bandName;
  50.                     }
  51.                 } else {
  52.                     // Otherwise, increase the time by the given amount
  53.                     times.put(bandName, times.get(bandName) + time);
  54.                 }
  55.                 // Increase the total time by the given amount
  56.                 totalTime += time;
  57.             }
  58.             // Read the next command
  59.             command = scanner.nextLine();
  60.         }
  61.        
  62.         // Print the total time
  63.         System.out.println("Total time: " + totalTime);
  64.         // Print the bands and their times in descending order
  65.         for (Map.Entry<String, Integer> entry : times.entrySet()) {
  66.             System.out.println(entry.getKey() + " -> " + entry.getValue());
  67.         }
  68.         // Print the first band on stage and its members in insertion order
  69.         System.out.println(firstBand);
  70.         for (String member : bands.get(firstBand)) {
  71.             System.out.println("=>" + member);
  72.         }
  73.     }
  74. }
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement