-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSong.java
More file actions
202 lines (183 loc) · 8 KB
/
Copy pathTestSong.java
File metadata and controls
202 lines (183 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package GenderMag2;
import java.util.LinkedList;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Random;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedWriter;
public class TestSong {
public static void main(String[] args) {
// Testing the Song class
Song<String> test1 = new Song<>("Bohemian Rhapsody", "Queen", "1975", "Arrival", "0000000");
Song<String> test2 = new Song<>("Imagine", "John Lennon", "1971", "Imagine", "11111111");
Song<String> test3 = new Song<>("Stairway to Heaven", "Led Zeppelin", "1971", "Led Zeppelin IV", "22222222");
System.out.println(test1.toString());
System.out.println(test2.toString());
System.out.println(test3.toString());
System.out.println();
// Testing readSongs method
String[] songsArray = readSongs();
for (String song : songsArray) {
System.out.println(song);
}
System.out.println();
// Testing readSongs2 method
LinkedList<Song<String>> songsLinkedList = readSongs2();
for (Song<String> song : songsLinkedList) {
System.out.println(song.toString());
}
System.out.println();
// Testing searchSong method
String searchedSong = searchSong(songsArray, "Imagine");
System.out.println(searchedSong);
String searchedSong2 = searchSong(songsArray, "1965", "My Girl");
System.out.println(searchedSong2);
// Testing comparator method
int compare = comparator(songsArray, "My Girl", "Imagine");
if (compare == 0) {
System.out.println("The songs are the same");
} else if (compare == -1) {
System.out.println("The second song goes after the first");
} else if (compare == 1) {
System.out.println("The first song goes after the second");
}
int compare2 = comparator(songsArray, "1965", "My Girl", "1965", "Yesterday");
if (compare2 == 0) {
System.out.println("The songs are the same");
} else if (compare2 == -1) {
System.out.println("The second song goes after the first");
} else if (compare2 == 1) {
System.out.println("The first song goes after the second");
}
// made by dahana
// Write the songs to a new file
writeToFile(songsArray, "C:\\Users\\dahan\\Downloads\\new_songs.txt");
}
///////////////////////// Modification made by Dahana
// Write modified songs array to a new text file
public static void writeToFile(String[] songsArray, String filePath) {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(filePath));
for (String song : songsArray) {
writer.write(song);
writer.newLine();
}
} catch (IOException e) {
System.err.println("Error writing to file: " + e.getMessage());
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
System.err.println("Error closing file: " + e.getMessage());
}
}
}
/////////////////////////
public static String[] readSongs() {
// Create an array to hold the songs
File songs = new File("C:\\Users\\dahan\\Downloads\\new_songs.txt");
String[] songsArray = new String[25];
// Read the file and store the lines in an array
try {
Scanner scanner = new Scanner(songs);
Random random = new Random();
int i = 0;
while (scanner.hasNextLine()) {
// Create a random number between 1000000 and 9999999
random.setSeed(i); // Set's the seed so each song has the same ID each time
Integer randomNumber = random.nextInt(1000000) + 1000000;
String line = scanner.nextLine();
String[] lineArray = line.split(",");
// Create a new song
Song<String> song = new Song<>(lineArray[0], lineArray[1], lineArray[2], lineArray[3], randomNumber.toString());
// Add the song to the list
songsArray[i] = song.toString();
i++;
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
// Return the array
return songsArray;
}
public static LinkedList<Song<String>> readSongs2() {
// Create an array to hold the songs
File songs = new File("C:\\Users\\dahan\\Downloads\\songs.txt");
LinkedList<Song<String>> songsLinkedList = new LinkedList<>();
// Read the file and store the lines in an array
try {
Scanner scanner = new Scanner(songs);
Random random = new Random();
int i = 0;
while (scanner.hasNextLine()) {
random.setSeed(i); // Set's the seed so each song has the same ID each time
i++;
// Create a random number between 1000000 and 9999999
Integer randomNumber = random.nextInt(1000000) + 1000000;
String line = scanner.nextLine();
String[] lineArray = line.split(",");
// Create a new song
Song<String> song = new Song<>(lineArray[0], lineArray[1], lineArray[2], lineArray[3], randomNumber.toString());
// Add the song to the list
songsLinkedList.add(song);
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
// Return the array
return songsLinkedList;
}
public static String searchSong(String[] songsArray, String name) {
for (int i = 0; i < songsArray.length; i++) {
if (songsArray[i].contains(name)) {
return songsArray[i];
}
}
return "Song not found";
}
public static String searchSong(String[] songsArray, String year, String name) {
for (int i = 0; i < songsArray.length; i++) {
if (songsArray[i].contains(year) && songsArray[i].contains(name)) {
return songsArray[i];
}
}
return "Song not found";
}
public static int comparator(String[] songsArray, String name1, String name2) {
if (name1.compareTo(name2) > 0 && searchSong(songsArray, name1) != "Song not found" && searchSong(songsArray, name2) != "Song not found") {
return 1;
}
else if (name1.compareTo(name2) < 0 && searchSong(songsArray, name1) != "Song not found" && searchSong(songsArray, name2) != "Song not found") {
return -1;
}
else if (name1.compareTo(name2) == 0 && searchSong(songsArray, name1) != "Song not found" && searchSong(songsArray, name2) != "Song not found") {
return 0;
}
else {
System.out.println("One or more songs are not found");
return -2;
}
}
public static int comparator(String[] songsArray, String year1, String name1, String year2, String name2) {
if (year1.compareTo(year2) > 0 && searchSong(songsArray, year1, name1) != "Song not found" && searchSong(songsArray, year2, name2) != "Song not found") {
return -1;
}
else if (year1.compareTo(year2) < 0 && searchSong(songsArray, year1, name1) != "Song not found" && searchSong(songsArray, year2, name2) != "Song not found") {
return 1;
}
else if (year1.compareTo(year2) == 0 && searchSong(songsArray, year1, name1) != "Song not found" && searchSong(songsArray, year2, name2) != "Song not found") {
return comparator(songsArray, name1, name2);
}
else {
System.out.println("One or more songs are not found");
return -2;
}
}
}