播放列表管理中添加歌曲会同时添加到主播放列表和普通播放列表中,请老师纠正

播放列表管理中添加歌曲会同时添加到主播放列表和普通播放列表中,请老师纠正

package com.imooc.Music;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Map.Entry;

//Test类
public class TestDemo {

    public void mainMenu() {
        System.out.println("****************************************");
        System.out.println("              **主菜单**");
        System.out.println("              1--播放列表管理");
        System.out.println("              2--播放器管理");
        System.out.println("              0--退出");
        System.out.println("****************************************");
        System.out.println("请输入对应数字进行操作:");
    }

    public void playListMenu() {
        System.out.println("****************************************");
        System.out.println("           **播放列表管理**");
        System.out.println("      1--将歌曲添加到主播放列表");
        System.out.println("      2--将歌曲添加到普通播放列表");
        System.out.println("      3--通过歌曲id查询播放列表中的歌曲");
        System.out.println("      4--通过歌曲名称查询播放列表中的歌曲");
        System.out.println("      5--修改播放列表中的歌曲");
        System.out.println("      6--删除播放列表中的歌曲");
        System.out.println("      7--显示播放列表中的所有歌曲");
        System.out.println("      8--导出歌单");
        System.out.println("      9--返回上一级菜单");
        System.out.println("****************************************");
        System.out.println("请输入对应数字对播放列表进行管理:");
    }

    public void playerMenu() {
        System.out.println("****************************************");
        System.out.println("              **播放器管理**");
        System.out.println("          1--向播放器添加播放列表");
        System.out.println("          2--从播放器删除播放列表");
        System.out.println("          3--通过名字查询播放列表信息");
        System.out.println("          4--显示所有播放列表名称");
        System.out.println("          9--返回上一级菜单");
        System.out.println("****************************************");
        System.out.println("请输入对应数字对播放器进行操作:");
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        List<Song> musicList = new ArrayList<Song>();
        PlayList p1 = new PlayList("主播放列表", musicList);

        Map<String, PlayList> playListMap = new HashMap<String, PlayList>();
        playListMap.put("主播放列表", p1);
        Set<Entry<String, PlayList>> entrySet = playListMap.entrySet();
        PlayListCollection player = new PlayListCollection(playListMap);

        TestDemo t = new TestDemo();

        while (true) {
            t.mainMenu();
            int x = sc.nextInt();
            switch (x) {
            case 1: {
                while (true) {
                    t.playListMenu();
                    int y = sc.nextInt();
                    switch (y) {
                    case 1: {
                        System.out.println("请输入要添加的歌曲数量");
                        int num = sc.nextInt();
                        for (int i = 0; i < num; i++) {
                            System.out.println("请输入第" + (i + 1) + "首歌曲:");
                            System.out.println("请输入第" + (i + 1) + "首歌曲的id:");
                            String id = sc.next();
                            System.out.println("请输入第" + (i + 1) + "首歌曲的名称:");
                            String name = sc.next();
                            System.out.println("请输入第" + (i + 1) + "首歌曲的歌手:");
                            String singer = sc.next();
                            Song song = new Song(id, name, singer);
                            p1.addToPlayList(song);
                        }
                        continue;
                    }
                    case 2: {
                        System.out.println("请输入要添加的播放列表名称:");
                        String listName = sc.next();
                        boolean flag = player.search(listName);
                        if (flag) {
                            System.out.println("请输入要添加的歌曲数量");
                            int num = sc.nextInt();
                            while (true) {
                                for (int i = 0; i < num; i++) {
                                    System.out.println("请输入第" + (i + 1) + "首歌曲:");
                                    System.out.println("请输入第" + (i + 1) + "首歌曲的id:");
                                    String id = sc.next();

                                    if (p1.getMusicList().get(i).getId().equals(id)) {
                                        player.searchPlayListByName(listName).addToPlayList(p1.searchSongById(id));
                                        continue;
                                    } else {
                                        System.out.println("该歌曲在主播放列表中不存在,请输入该歌曲的其他信息");
                                        System.out.println("请输入第" + (i + 1) + "首歌曲的名称:");
                                        String name = sc.next();
                                        System.out.println("请输入第" + (i + 1) + "首歌曲的歌手:");
                                        String singer = sc.next();
                                        Song song = new Song(id, name, singer);
                                        player.searchPlayListByName(listName).addToPlayList(song);
                                    }
                                }
                                break;
                            }
                        } else
                            System.out.println("该播放列表未找到,请先在播放器中添加该播放列表。");
                        p1.displayAllSong();
                        player.searchPlayListByName(listName).displayAllSong();
                        continue;
                    }

                    case 3: {
                        System.out.println("输入播放列表名称:");
                        String listName = sc.next();
                        for (Entry<String, PlayList> entry : entrySet) {
                            if (entry.getKey().equals(listName)) {
                                System.out.println("请输入查询歌曲的id");
                                String id = sc.next();
                                System.out.println("查询到的歌曲信息为:");
                                System.out.println(entry.getValue().searchSongById(id));
                                break;
                            } else
                                System.out.println("该歌曲在" + listName + "中不存在。");
                        }
                        continue;
                    }
                    case 4: {
                        System.out.println("输入播放列表名称:");
                        String listName = sc.next();
                        for (Entry<String, PlayList> entry : entrySet) {
                            if (entry.getKey().equals(listName)) {
                                System.out.println("请输入查询歌曲的名称");
                                String name = sc.next();
                                System.out.println("查询到的歌曲信息为:");
                                System.out.println(entry.getValue().searchSongByName(name));
                                break;
                            } else
                                System.out.println("该歌曲在" + listName + "中不存在。");
                        }
                        continue;

                    }
                    case 5: {
                        System.out.println("请输入修改歌曲的id");
                        String id = sc.next();
                        for (Entry<String, PlayList> entry : entrySet) {
                            entry.getValue().updateSong(id);
                        }
                        continue;
                    }
                    case 6: {
                        System.out.println("请输入删除歌曲的id");
                        String id = sc.next();
                        for (Entry<String, PlayList> entry : entrySet) {
                            entry.getValue().deleteSong(id);
                        }
                        continue;
                    }
                    case 7: {
                        for (Entry<String, PlayList> entry : entrySet) {
                            entry.getValue().displayAllSong();
                        }

                        continue;
                    }

                    case 8:
                        p1.exportPlayList();
                        continue;
                    case 9:
                    }
                    break;
                }
                continue;
            }

            // **播放器管理**"
//                 1--向播放器添加播放列表;
//                 2--从播放器删除播放列表;
//                 3--通过名字查询播放列表信息;
//                 4--显示所有播放列表名称;
//                 9--返回上一级菜单;
            case 2: {
                while (true) {
                    t.playerMenu();
                    int y = sc.nextInt();
                    switch (y) {
                    case 1: {
                        System.out.println("请输入要添加的播放列表名称");
                        String listName = sc.next();
                        PlayList p = new PlayList(listName, musicList);
                        player.addPlayList(p);
                        continue;
                    }
                    case 2: {
                        System.out.println("请输入要删除的播放列表名称");
                        String listName = sc.next();
                        for (Entry<String, PlayList> entry : entrySet) {
                            if (entry.getKey().equals(listName))
                                player.deletePlayList(entry.getValue());
                        }
                        continue;
                    }
                    case 3: {
                        System.out.println("请输入要查询的播放列表名称");
                        String listName = sc.next();
                        PlayList ps = player.searchPlayListByName(listName);
                        System.out.println("该播放列表存在,该播放列表的信息为:");
                        System.out.println(ps);
                        continue;
                    }
                    case 4:
                        player.displayPlayListName();
                        continue;
                    case 9:
                    }
                    break;
                }
                continue;
            }
            case 0:
                break;
            }
            break;
        }
    }
}
//song类
public class Song {
    private String id;
    private String name;
    private String singer;

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return this.id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSinger() {
        return singer;
    }

    public void setSinger(String singer) {
        this.singer = singer;
    }

    public Song(String id, String name, String singer) {
        this.setId(id);
        this.setName(name);
        this.setSinger(singer);
    }
    public Song() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((singer == null) ? 0 : singer.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
    if(this==obj)
        return true;
    if(obj.getClass()==Song.class) {
        Song s=(Song)obj;
        return (this.getId().equals(s.getId()))&&(this.getName().equals(s.getName()))&&(this.getSinger().equals(s.getSinger()));
    }
    else return false;
    }

    @Override
    public String toString() {
        return "歌曲信息:id为:" + id + ",歌曲名称为:" + name + ",演唱者为:" + singer ;
    }
    
    
    

}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

//PlayList类
public class PlayList implements Serializable{
    private String playListName;
    private List<Song> musicList = new ArrayList<Song>();
    Scanner sc = new Scanner(System.in);

    public String getPlayListName() {
        return playListName;
    }

    public void setPlayListName(String playListName) {
        this.playListName = playListName;
    }

    public List<Song> getMusicList() {
        return musicList;
    }

    public void setMusicList(List<Song> musicList) {
        this.musicList = musicList;
    }

    public PlayList(String playListName, List<Song> musicList) {
        this.setPlayListName(playListName);
        this.setMusicList(musicList);
    }

    public PlayList() {
    }

    // 添加歌曲到播放列表的方法
    public void addToPlayList(Song song) {
        this.musicList.add(song);
    }

    // 显示播放列表歌曲的方法
    public void displayAllSong() {
        System.out.println(this.getPlayListName());
        System.out.println("播放列表中的所有歌曲为:");
        for (int i = 0; i < this.musicList.size(); i++)
            System.out.println((Song)this. musicList.get(i));
    }

    // 通过id查询歌曲的方法
    public Song searchSongById(String id) {
        Song song = new Song();
            boolean flag = false;
            for (int i = 0; i < this.musicList.size(); i++) {
                if ((this.musicList.get(i).getId()) .equals(id)) {
                    song = (Song)this. musicList.get(i);
                    flag = true;
                    break;
                }
            }
            if (flag == false) // 编程常犯错误:将赋值当等于 (flag=false)
                System.out.println("未找到该歌曲。");
            return song;
        }
    

    // 通过歌名查询歌曲的方法
    public Song searchSongByName(String n) {
        Song song = new Song();
            boolean flag = false;
            for (int i = 0; i < musicList.size(); i++) {
                if ((musicList.get(i).getName()).equals(n)) {
                    song = (Song) musicList.get(i);
                    flag = true;
                    break;
                }
            }
            if (flag == false)
                System.out.println("未找到该歌曲。");
        return song;
    }

    // 修改歌曲的方法
    public void updateSong(String id) {
            boolean flag = false;
            for (int i = 0; i < musicList.size(); i++) {
                if ((musicList.get(i).getId()).equals(id)) {
                    flag = true;
                    System.out.println("修改的歌曲信息为:");
                    System.out.println((Song) musicList.get(i));
                    System.out.println("请输入修改后的歌曲名称");
                    (musicList.get(i)).setName(sc.next());
                    System.out.println("请输入修改后的歌曲演唱者");
                    (musicList.get(i)).setSinger(sc.next());
                    System.out.println("修改成功!");
                    break;
                }
            }
            if (flag == false)
                System.out.println("未找到该歌曲。");
    }

    // 通过id删除歌曲的方法
    public void deleteSong(String id) {
        for (int i = 0; i < musicList.size(); i++) {
            if((musicList.get(i).getId()).equals(id))
                musicList.remove(i);
        }
        System.out.println("删除后的播放列表为:");
        for (int i = 0; i < musicList.size(); i++) {
        System.out.println((Song)musicList.get(i));
        }
    }
    
    //导出歌单
    public void exportPlayList() {
        try {
            FileOutputStream fos=new FileOutputStream(this.getPlayListName()+"的歌单.txt");
            ObjectOutputStream oos=new ObjectOutputStream(fos);
            oos.writeObject(musicList);
            oos.flush();
            oos.close();
            fos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.Scanner;

//PlayListCollection类
public class PlayListCollection {
    Map<String,PlayList> playListMap=new HashMap<String,PlayList>();
    
    public Map<String, PlayList> getPlayListMap() {
        return playListMap;
    }
    public void setPlayListMap(Map<String, PlayList> playListMap) {
        this.playListMap = playListMap;
    }
    
    public PlayListCollection(Map<String, PlayList> playListMap) {
        this.setPlayListMap(playListMap);
    }
    //添加播放列表
    public void addPlayList(PlayList playList) {
        playListMap.put(playList.getPlayListName(), playList);
        }
        
    //删除播放列表
    public void deletePlayList(PlayList playList) {
        playListMap.remove(playList.getPlayListName());
    }
    //通过名字查询播放列表
    public PlayList searchPlayListByName(String playListName) {
        Set<Entry<String, PlayList>> entrySet = playListMap.entrySet();
        PlayList p=new PlayList();
        for(Entry<String, PlayList> entry:entrySet) {
            if(entry.getKey().equals(playListName))
                p=entry.getValue();
        }
        return p;
    }
    public boolean search(String playListName) {
        Set<Entry<String, PlayList>> entrySet = playListMap.entrySet();
        boolean flag=false;
        for(Entry<String, PlayList> entry:entrySet) {
            if(entry.getKey().equals(playListName))
            flag=true;
        }
        return flag;
    }
    //显示所有播放列表名称
    public void displayPlayListName(){
        System.out.println("播放列表名称为:");
        Set<Entry<String, PlayList>> entrySet = playListMap.entrySet();
        for(Entry<String, PlayList> entry:entrySet) {
            System.out.println(entry.getKey());
        }
    }
}


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

测试类中同学定义了一个musicList

http://img1.sycdn.imooc.com//climg/5c7c9d0d0001e25004360049.jpg

在创建普通播放列表时,传入的也是这个musicList对象,导致主播放列表和普通播放列表拥有同一个musicList,所以添加歌曲到主播放列表也会同时添加到普通播放列表;

修改建议:PlayList增加一个单参构造方法,为:PlayList pl=new PlayList("主播放列表");

http://img1.sycdn.imooc.com//climg/5c7c9da100010ea904060102.jpg

创建普通播放列表时,也是: PlayList p = new PlayList(listName);

这样在创建播放列表时,就不会拥有同一个musicList了。

祝学习愉快!

  • qq_cookies_oqrHNO 提问者 #1
    请问老师,使用单参构造方法创建播放列表时时就直接创建了属于自己的音乐表了吗
    2019-03-04 17:02:56
  • 同学你好,使用这种方式可以创建播放列表,另外可以调用addPlayList()方法将播放列表添加到播放器中。祝:学习愉快~
    2019-03-04 19:04:41
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师