Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class MuddledMusic{
  5. public static class Song{
  6. //Instance variables for the name and artist
  7. public String name;
  8. public String artist;
  9. //Constructor
  10. public Song(String name, String artist){
  11. this.name = name;
  12. this.artist = artist;
  13. }
  14.  
  15. //Remove the "The "
  16. public String effectiveArtist(){
  17. return artist.replace("The ", "");
  18. }
  19.  
  20. //String that would show the same as the input file(IE: "Hello - Adele")
  21. public String toString(){
  22. return name + " - " + artist;
  23. }
  24. }
  25. //Comparator for Artist. Used to sort artists alphabetically
  26. public static class ArtistComparator implements Comparator<Song>{
  27. public int compare(Song a, Song b){
  28. return a.effectiveArtist().compareTo(b.effectiveArtist());
  29. }
  30. }
  31. //Comparator for Songs. Uses to sort songs alphabetically
  32. public static class SongComparator implements Comparator<Song>{
  33. public int compare(Song a, Song b){
  34. return a.name.compareTo(b.name);
  35. }
  36. }
  37. //Used for debugging
  38. public static void printPairs(Song[] input){
  39. for(Song s : input){
  40. System.out.println(s.toString());
  41. }
  42. }
  43. //Sort with certain indexes
  44. public static Song[] sortBySongNameWithIndexes(Song[] arr, int a, int b){
  45. Song[] tempArray = new Song[(b - a) + 1];
  46. System.arraycopy(arr, a, tempArray, 0, ((b-a) + 1));
  47. Arrays.sort(tempArray, new SongComparator());
  48. return tempArray;
  49. }
  50. public static void main(String[] args) throws FileNotFoundException,IOException{
  51. //Path to text file
  52. String filePath = "Prob08.in.txt";
  53. //new bufferedreader
  54. BufferedReader br = new BufferedReader(new FileReader(filePath));
  55. //Read first line
  56. String testCasesStr = br.readLine();
  57. //Parse to int
  58. int testCases = Integer.parseInt(testCasesStr);
  59. //loop through test cases
  60. for(int i = 0; i < testCases; i++){
  61. //Read amount of song-artist pairs
  62. String songArtistPairsStr = br.readLine();
  63. //Parse to int
  64. int songArtistPairs = Integer.parseInt(songArtistPairsStr);
  65. //Create Song objects array
  66. Song[] songObjectsOrig = new Song[songArtistPairs];
  67. //loop through song artist pairs
  68. for(int j = 0; j < songArtistPairs; j++){
  69. //Read line
  70. String line = br.readLine();
  71. //Split into song and artist
  72. String[] lineSplit = line.split(" - ");
  73. //make them seperate strings so it is easier
  74. String song = lineSplit[0];
  75. String artist = lineSplit[1];
  76. //Create song instance
  77. Song curSong = new Song(song, artist);
  78. //Store into array
  79. songObjectsOrig[j] = curSong;
  80. }
  81. //Now we have all of the data, lets sort by artists
  82. //But first lets copy the array into a copy
  83. Song[] songObjects = new Song[songArtistPairs];
  84. System.arraycopy(songObjectsOrig, 0, songObjects, 0, songObjectsOrig.length);
  85. //Now copy is made, sort copy
  86. Arrays.sort(songObjects, new ArtistComparator());
  87. //debug
  88. //printPairs(songObjects);
  89. //Now that we have sorted by artists, we much sort the names of the songs. We should only do this for songs with the same artist
  90. //To do this, we find the amount of artists that have multiple songs. Then make a for loop with the conditional of i < (multipleSongArtistCount)
  91. //Then we fill it with the songs they have.
  92. sortBySongNameWithIndexes(songObjects, 1, 2);
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement