Here's a simple Java application using a console-based approach to track the music you've listened to. You can expand upon this to include a database or a more sophisticated user interface.
java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Song {
private String title;
private String artist;
private String album;
public Song(String title, String artist, String album) {
this.title = title;
this.artist = artist;
this.album = album;
}
public String getTitle() {
return title;
}
public String getArtist() {
return artist;
}
public String getAlbum() {
return album;
}
@Override
public String toString() {
return "Song{" +
"title='" + title + '\'' +
", artist='" + artist + '\'' +
", album='" + album + '\'' +
'}';
}
}
public class MusicTracker {
private List<Song> songList;
public MusicTracker() {
this.songList = new ArrayList<>();
}
public void addSong(Song song) {
songList.add(song);
}
public void showSongs() {
if (songList.isEmpty()) {
System.out.println("Your music list is empty!");
} else {
for (Song song : songList) {
System.out.println(song);
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
MusicTracker tracker = new MusicTracker();
while (true) {
System.out.println("Welcome to MusicTracker! What would you like to do?");
System.out.println("1. Add a song");
System.out.println("2. Show all songs");
System.out.println("3. Exit");
System.out.print("Enter your choice (1-3): ");
int choice = scanner.nextInt();
scanner.nextLine(); // consume the newline
switch (choice) {
case 1:
System.out.print("Enter song title: ");
String title = scanner.nextLine();
System.out.print("Enter artist name: ");
String artist = scanner.nextLine();
System.out.print("Enter album name: ");
String album = scanner.nextLine();
tracker.addSong(new Song(title, artist, album));
System.out.println("Song added successfully!");
break;
case 2:
tracker.showSongs();
break;
case 3:
System.out.println("Thank you for using MusicTracker. Goodbye!");
return;
default:
System.out.println("Invalid choice. Please enter 1, 2, or 3.");
break;
}
}
}
}
This application allows you to add songs to your music list and display all the songs you've added. Each song has a title, artist, and album. The MusicTracker class has an ArrayList to store songs, and the main method provides a simple menu to interact with the application.
To run the application, you must compile the code using a Java compiler (if you're using javac, the command is javac MusicTracker.java) and then run it using a Java runtime environment (with the command java MusicTracker).
Feel free to enhance the application with more features, such as removing a song, searching for a song by various attributes, saving the list to a file, reading from a file, or integrating with a database for persistent storage.