定义三个新类,分别名为Song、PlayList和MusicCollection , Song对象包含着关于特定歌曲的信息,比如歌曲名、艺术家,专辑、歌曲长度等,PlayList对象包含播放列表名称和一个歌曲的集合:MusicCollection对象包含播放列表集合。它包括一个名为Library的主播放列表,这个列表包含该集合中的所有歌曲。定义上述的三个类,并编写方法实现下列任务:
创建一个Song 对象,并设置其信息。
创建一个PlayList对象,并对播放列表添加和删除歌曲。如果一首新歌不在主列表中,那么将其添加进去。确保从主播放列表中删除一首歌时,也要从音乐集合中的其他播放列表中删除此歌曲。
创建一个MusicCollection 对象,并对该集合添加和删除播放列表搜索并显示关于所有歌曲,播放列表或整个音乐集合的信息。
确保所有你定义的类都不产生内存漏洞。
import java.util.Arrays;
public class TestDemo {
public static void main(String[] args) {
Song song = new Song();
song.songName = "庐州月";
song.author = "许嵩";
song.album = "寻雾启示";
song.time = 253;
System.out.println("建立好了一首歌:"+song.toString());
PlayList playList = new PlayList();
// 定义播放列表的名称
playList.playListName = "播放列表一";
// 定义播放列表的长度
playList.songs = new Song[0];
// 创建对象
MusicCollection mc = new MusicCollection();
//创建主播放列表
mc.Library = new PlayList();
mc.Library.playListName = "Library";
mc.Library.songs = new Song[0];
// 其他播放列表中有一个播放列表,就是播放列表一
mc.otherLibrary = new PlayList[1];
mc.otherLibrary[0] = playList;
PlayList.add(playList, song,mc.Library);
PlayList.add(playList,new Song("清明雨上","许嵩","自定义",283),mc.Library);
PlayList.add(playList,new Song("庐州月", "许嵩", "寻雾启示", 253),mc.Library);
PlayList.delete(playList,"庐州月");
PlayList.delete(playList,"舅舅家");
PlayList.delete(playList,"清明雨上");
PlayList.delete(playList,"庐州月");
PlayList.showSongs(mc.Library.songs, "Library");
PlayList.deletezhu(mc.Library, "庐州月", mc.otherLibrary);
//PlayList.showSongs(mc.Library.songs, "Library");
PlayList.showSongs(playList.songs, playList.playListName);
PlayList.add(playList,new Song("清明雨上","许嵩","自定义",283),mc.Library);
PlayList.showSongs(mc.Library.songs, "Library");
PlayList.delete(playList,"清明雨上");
PlayList.add(playList,new Song("庐州月", "许嵩", "寻雾启示", 253),mc.Library);
PlayList.showSongs(playList.songs, playList.playListName);
PlayList.showSongs(mc.Library.songs, "Library");
PlayList.deletezhu(mc.Library, "庐州月", mc.otherLibrary);
PlayList.deletezhu(mc.Library, "清明雨上", mc.otherLibrary);
PlayList.showSongs(playList.songs, playList.playListName);
}
}
//
/**
定义一个歌曲类
*/
class Song {
public String songName;// 歌曲名称
public String author;// 艺术家
public String album;// 专辑
public long time;// 歌曲时长
// 定义一个有参数构造方法
public Song(String songName, String author, String album, long time) {
this.songName = songName;
this.author = author;
this.album = album;
this.time = time;
}
public String toString() {
return songName + "-" + author + "-" + album + "-" + time;
}
// 一个无参数构造方法
public Song() {
}
}
/**
* 播放列表类
* * 播放列表名称
* * 当前播放列表中的歌曲集合
*/
class PlayList {
//播放列表的名称
public String playListName;
//当前播放列表的所有歌曲的集合
public Song[] songs;
public static void add(PlayList playList ,Song song ,PlayList Library) {
int index = 0;
for(int i = 0;i<playList.songs.length;i++){
if(song.songName==playList.songs[i].songName){
index = -1;
}
}
if(index==0){
Song[] newsongs = Arrays.copyOf(playList.songs,playList.songs.length + 1);
newsongs[newsongs.length - 1] = song;
playList.songs = newsongs;// 把新的数组赋值给播放列表一,想当于被播放列表一中添加歌曲
newsongs = null;// 回收垃圾
showSongs(playList.songs, playList.playListName);// 打印数组
}
else
{
System.out.println(song.songName+"已在您的播放列表中,请查看。");
}
//同步到主列表
add2library(Library, song);
}
/**
* 输出打印数组中的内容
*/
public static void showSongs(Song[] songs, String b) {
String name;
if (b == "Library") {
name = "主播放列表";
} else {
name = "播放列表一";
}
System.out.println(name + "中内容为:");
System.out.println(Arrays.toString(songs));// 输出数组内容
}
// 添加歌曲到主列表的数组中
public static void add2library(PlayList Library , Song song) {
int index = 0;
for(int i = 0;i<Library.songs.length;i++){
if(song.songName==Library.songs[i].songName){
index = -1;
}
}
if(index==0){
Song[] newsongs = Arrays.copyOf(Library.songs,Library.songs.length + 1);
newsongs[newsongs.length - 1] = song;
Library.songs = newsongs;// 把新的数组赋值给播放列表一,想当于被播放列表一中添加歌曲
newsongs = null;// 回收垃圾
}
}
public static void delete(PlayList playList , String name) {
if(playList.songs.length==0){
System.out.println("该播放列表已经没有歌曲了");
return;
}
if (name == null || "".equals(name)) {
System.out.println("你传递的歌曲名称为空,请检查");
return;
}
int index = -1;
System.out.println(playList.songs.length);
for (int i = 0; i < playList.songs.length; i++) {
// 如果集合中有和要删除的名称相同的歌曲
if (name.equals(playList.songs[i].songName)) {
index = i;
break;
}
}
// 如果标记不是-1,就有要删除的了
if (index != -1) {
Song[] songs = new Song[playList.songs.length - 1];
for (int i = 0; i < songs.length; i++) {
if (i == index) {
songs[i] = playList.songs[++i];
break;
}
songs[i] = playList.songs[i];
}
// 新的数组赋值给旧的,相当于从旧的中删除了
playList.songs = songs;
songs = null;// 回收垃圾
System.out.println("删除:" + name + "成功");
showSongs(playList.songs, playList.playListName);
} else {
System.out.println("您要删除的歌曲:" + name + "不存在");
}
}
public static void deletezhu(PlayList playList , String name,PlayList arr []) {
if(playList.songs.length==0){
System.out.println("该播放列表已经没有歌曲了");
return;
}
if (name == null || "".equals(name)) {
System.out.println("你传递的歌曲名称为空,请检查");
return;
}
int index = -1;
System.out.println(playList.songs.length);
for (int i = 0; i < playList.songs.length; i++) {
// 如果集合中有和要删除的名称相同的歌曲
if (name.equals(playList.songs[i].songName)) {
index = i;
break;
}
}
// 如果标记不是-1,就有要删除的了
if (index != -1) {
Song[] songs = new Song[playList.songs.length - 1];
for (int i = 0; i < songs.length; i++) {
if (i == index) {
songs[i] = playList.songs[++i];
break;
}
songs[i] = playList.songs[i];
}
// 新的数组赋值给旧的,相当于从旧的中删除了
playList.songs = songs;
songs = null;// 回收垃圾
System.out.println("删除:" + name + "成功");
showSongs(playList.songs, playList.playListName);
} else {
System.out.println("您要删除的歌曲:" + name + "不存在");
}
for(int i = 0;i<arr.length;i++){
PlayList.delete(arr[i], name);
}
}
}
/**
* 播放列表集合类,存放播放列表
*
*
*/
class MusicCollection {
//主播放列表
public PlayList Library;
//其他播放列表
public PlayList [] otherLibrary;
}
|
|