Java项目实战---歌曲管理系统

Java项目实战—歌曲管理系统

声明:本人仅整理了代码,方便大家进行调试优化,功能上还存在很多纰漏,欢迎大家评论区讨论
代码原地址放于文章末尾

一、实验目的:
掌握类的定义,JAVA应用程序的设计与实现。

二、实验内容:
开发环境
Eclipse + JDK + List/Map + File
系统中的对象
歌曲(Song.java):编号、歌名、语言(中文/英文)、类别(流行/通俗)、歌手
系统功能描述
对已有的曲目进行管理:
查找歌曲信息(按歌名\语言\歌手\类别等进行查询)
增加歌曲
删除某歌曲
技术要求
List/Map,File, 输入输出流对象
画面参考在这里插入图片描述
三、实验要求:

  1. 通过实验掌握文件输入输出流的使用方法;
  2. 程序必须能够从键盘接收字符串并保存在文件中;
  3. 程序必须能够读出文件内容显示在屏幕上;
  4. 数据保存在文件中;

四、实验步骤:

  1. 分析需求,设计类并画出类框图;
  2. 实现类,完成相应功能
  3. 调试运行程序。

代码展示

以下为 Song.java 部分代码

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Song {
    private int sid;
    private String sname;
    private String category;
    private String singer;

    public Song() {
    }

    public Song(int sid, String sname, String category, String singer) {
        super();
        this.sid = sid;
        this.sname = sname;
        this.category = category;
        this.singer = singer;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getSinger() {
        return singer;
    }

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


}

以下为 SongDao.java 部分的代码

import java.util.List;

public class SongDao {
    private List<Song> songList;

    public SongDao() {
    }

    public SongDao(List<Song> songList) {
        super();
        this.songList = songList;
    }

    //    新增歌曲
    public void add(Song song) {
        songList.add(song);
    }

    //    查询歌曲
    //    按id查找
    public Song search(int sid) {
        for (Song song : songList) {
            if (song.getSid() == sid) {
                return song;
            }
        }
        return null;
    }//    按对象查找

    public Song search(Song song) {
        if (song == null) {
            return null;
        }
        return search(song.getSid());
    }

    //    删除歌曲
    //按id删除歌曲
    public void delete(int sid) {
        Song song = search(sid);
        if (song != null) {
            songList.remove(song);
        }
    }

    //按对象删除歌曲
    public void delete(Song song) {
        if (song != null) {
            delete(song.getSid());
        }
    }

    public List<Song> getSongList() {
        return songList;
    }

    public void setSongList(List<Song> songList) {
        this.songList = songList;
    }
}

以下为 SongService.java 部分的代码

import java.util.List;

public class SongService {
    private SongDao dao;

    public SongService() {
        super();
        // TODO Auto-generated constructor stub
    }

    public SongService(SongDao dao) {
        super();
        this.dao = dao;
    }

    public void add(Song song) {
        dao.add(song);
    }

    public Song search(int sid) {
        return dao.search(sid);
    }

    public Song search(Song song) {
        return dao.search(song);
    }

    public void delete(Song song) {
        dao.delete(song);
    }

    public void delete(int id) {
        dao.delete(id);
    }


    public List<Song> trace() {
        return dao.getSongList();
    }

    public void alter(Song song) {

    }

    public SongDao getDao() {
        return dao;
    }

    public void setDao(SongDao dao) {
        this.dao = dao;
    }


}

以下为 SongView.java 部分的代码

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.util.ArrayList;
import java.util.List;
import javax.swing.text.AbstractDocument.BranchElement;

public class SongView {
    private SongService service;

    public SongView() {
        super();
        //TODO Auto-generated constructor stub
    }

    public SongView(SongService service) {
        super();
        this.service = service;
    }

    public void show() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        boolean stop = false;
        while (!stop) {
            System.out.println("****************************************************\n"
                    + "                                       **歌曲管理** \n"
                    + "    1-----------------------------------查找歌曲\n"
                    + "    2-----------------------------------增加歌曲\n"
                    + "    3-----------------------------------删除歌曲\n"
                    + "    4-----------------------------------显示所有歌曲\n"
                    + "    5-----------------------------------退出系统\n"
                    + "****************************************************\n"
                    + "                                       **input [1-5]:");
            String line = br.readLine();
            int command = Integer.parseInt(line);
            switch (command) {
                case 1:
                    System.out.println("Sccording to id or complete info?Input[lor2]");
                    line = br.readLine();
                    command = Integer.parseInt(line);
                    if (command == 2) {
                        System.out.println("Input song's id");
                        String sid = br.readLine();
                        int id = Integer.parseInt(sid);
                        System.out.println("Input song's name:");
                        String sname = br.readLine();
                        System.out.println("Input singer's name");
                        String singerName = br.readLine();
                        System.out.println("Input song's category");
                        String category = br.readLine();
                        Song song = new Song(id, sname, category, singerName);
                        print(service.search(song));
                    } else {
                        System.out.println("Input song's id");
                        String sid = br.readLine();
                        int id = Integer.parseInt(sid);
                        print(service.search(id));
                    }
                    break;
                case 2:
                    System.out.println("Input new song's id");
                    String sid = br.readLine();
                    int id = Integer.parseInt(sid);
                    System.out.println("Input new song's name");
                    String sname = br.readLine();

                    System.out.println("Input new singer's name");
                    String singerName = br.readLine();
                    System.out.println("Input song's category");
                    String category = br.readLine();
                    Song song = new Song(id, sname, category, singerName);
                    service.add(song);
                    break;
                case 3:
                    System.out.println("请输入待删除歌曲的编号");
                    sid = br.readLine();
                    id = Integer.parseInt(sid);
                    service.delete(id);
                    System.out.println("Delete seccessfully");
                    break;
                case 4:
                    List<Song> list = service.trace();
                    print(list);
                    break;
                case 5:
                    stop = true;
                    System.out.println("Byebye");
                    break;
                default:
                    System.out.println("Error in input, try again");
                    break;

            }
        }

    }

    public void print(Song song) {
        System.out.printf("%-10s%-15s%-15s%-15s\n", "ID", "NAME", "SINGER", "CATEGORY");
        System.out.println("----------------------------------------------------------------------");
        System.out.printf("%-15d%-20s%-25s%-20s\n", song.getSid(), song.getSname(), song.getSinger(), song.getCategory());
    }

    public void print(List<Song> list) {
        System.out.printf("%-10s%-15s%-15s%-15s\n", "ID", "NAME", "SINGER", "CATEGORY");
        System.out.println("----------------------------------------------------------------------");
        for (Song song : list)
            System.out.printf("%-15d%-20s%-25s%-20s\n", song.getSid(), song.getSname(), song.getSinger(), song.getCategory());

    }

    public static void main(String[] args) {
        SongView view = new SongView();
        SongService service = new SongService();
        SongDao dao = new SongDao();
        List<Song> list = new ArrayList<>();
        dao.setSongList(list);
        service.setDao(dao);
        view.setSongService(service);
        try {
            view.show();
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e);
        }
    }

    public SongService getSongService() {
        return service;
    }

    public void setSongService(SongService songService) {
        this.service = songService;
    }
}

转载于:https://www.cnblogs.com/kazama/p/10181095.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值