Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Concert {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // A map to store the bands and their members
- Map<String, List<String>> bands = new LinkedHashMap<>();
- // A map to store the bands and their time on stage
- Map<String, Integer> times = new TreeMap<>(Comparator.reverseOrder());
- // A variable to store the total time
- int totalTime = 0;
- // A variable to store the first band on stage
- String firstBand = "";
- // Read commands until "Start!" is entered
- String command = scanner.nextLine();
- while (!command.equals("Start!")) {
- // Split the command by space
- String[] tokens = command.split("\\s+");
- // If the command starts with "Add"
- if (tokens[0].equals("Add")) {
- // Get the band name and the members
- String bandName = tokens[1];
- List<String> members = new ArrayList<>(Arrays.asList(tokens).subList(2, tokens.length));
- // If the band does not exist in the map, add it with its members
- if (!bands.containsKey(bandName)) {
- bands.put(bandName, members);
- } else {
- // Otherwise, add only the members that are not already in the list
- List<String> existingMembers = bands.get(bandName);
- for (String member : members) {
- if (!existingMembers.contains(member)) {
- existingMembers.add(member);
- }
- }
- }
- }
- // If the command starts with "Play"
- if (tokens[0].equals("Play")) {
- // Get the band name and the time
- String bandName = tokens[1];
- int time = Integer.parseInt(tokens[2]);
- // If the band does not exist in the map, add it with its time
- if (!times.containsKey(bandName)) {
- times.put(bandName, time);
- // If this is the first band on stage, set it as such
- if (firstBand.isEmpty()) {
- firstBand = bandName;
- }
- } else {
- // Otherwise, increase the time by the given amount
- times.put(bandName, times.get(bandName) + time);
- }
- // Increase the total time by the given amount
- totalTime += time;
- }
- // Read the next command
- command = scanner.nextLine();
- }
- // Print the total time
- System.out.println("Total time: " + totalTime);
- // Print the bands and their times in descending order
- for (Map.Entry<String, Integer> entry : times.entrySet()) {
- System.out.println(entry.getKey() + " -> " + entry.getValue());
- }
- // Print the first band on stage and its members in insertion order
- // Add a null check for the first band variable and a containsKey check for the bands map
- if (firstBand != null && bands.containsKey(firstBand)) {
- System.out.println(firstBand);
- for (String member : bands.get(firstBand)) {
- System.out.println("=>" + member);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement