文章目录
- 前言
- 一、思维导图
- 二、先解决music包
- 1.创建Music类 用来存放歌曲属性。
- 2.Musiclist类
- 3 解决mtest用户包
- 4 实现operation的类
- 三、效果展示
- 总结
前言
利用面向对象及线性表知识完成朴实无华的音乐管理系统
一、思维导图
二、先解决music包
1.创建Music类 用来存放歌曲属性。
`public class Music {
private String song;
private int year;
private String singer;
private String type;
private String album;
public Music(String song, int year, String singer, String type, String album) {
this.song = song;
this.year = year;
this.singer = singer;
this.type = type;
this.album = album;
}
public Music() {
}
public String getSong() {
return song;
}
public void setSong(String song) {
this.song = song;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getSinger() {
return singer;
}
public void setSinger(String singer) {
this.singer = singer;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
@Override
public String toString() {
return "music{" +
"song='" + song + '\'' +
", year=" + year +
", singer='" + singer + '\'' +
", type='" + type + '\'' +
", album='" + album + '\'' +
'}';
}
}
2.Musiclist类
public class Musiclist {
public Music[] music=new Music[100];//Music类型数组
private int usedSize; //存放个数
//构造方法
public Musiclist() {
music[0]=new Music("七里香",2004,"周杰伦","情歌","七里香");
this.usedSize=1;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
//获取歌曲编号pos
public Music getPos(int pos){
return this.music[pos];
}
public void setMusic(int pos,Music music) {
this.music[pos] = music;
}
}
3 解决mtest用户包
User父类
public abstract class User {
protected String name;
protected Ioperation[] arr;//arr数组
public User(String name) {
this.name = name;
}
//doWork方法来arr数组接受主函数的choice
public void doWork(int choice, Musiclist music){
arr[choice].work(music);
}
public abstract int menu();//菜单抽象类
}
普通用户类
public class NormalUser extends User{
public NormalUser(String name){
super(name);
this.arr=new Ioperation[] {
new Exit(), //0下标开始对应选项
new Add(), //1下标
new Delete(),
new Search(),
new Modify(),
new Display()
};
}
public int menu(){
System.out.println("-----欢迎来到普通用户------");
System.out.println("1.增加歌曲");
System.out.println("2.删除歌曲");
System.out.println("3.查询歌曲");
System.out.println("4.修改歌曲");
System.out.println("5.打印歌曲");
System.out.println("0.退出系统");
System.out.println("-------------------------");
System.out.println("请输入你的选择:");
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
return x;
}
}
管理员用户类
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.arr=new Ioperation[] {
new Display()
};
}
public int menu(){
System.out.println("----------管理员用户-------");
System.out.println("请输入你的选择:");
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
return x;
}
}
4 实现operation的类
(1)增加Add
public class Add implements Ioperation{
public void work(Musiclist music){
System.out.println("1.增加歌曲");
int size=music.getUsedSize();
System.out.println("请输入增加的歌曲:");
Scanner sc =new Scanner(System.in);
String song=sc.next();
System.out.println("请输入增加的发行年份:");
int year=sc.nextInt();
System.out.println("请输入增加的歌手:");
String singer=sc.next();
System.out.println("请输入增加的类型:");
String type=sc.next();
System.out.println("请输入增加的歌曲专辑:");
String album=sc.next();
Music music1=new Music(song,year,singer,type,album);
music.setMusic(size,music1);
music.setUsedSize(size+1);
System.out.println("增加成功!");
}
}
(2)Delete
public class Delete implements Ioperation{
public void work(Musiclist music){
System.out.println("2.删除歌曲");
System.out.println("请输入删除的歌曲名称:");
Scanner sc=new Scanner(System.in);
String x=sc.nextLine();
int currentSize = music.getUsedSize();
int index = 0;//存储找到的下标
int i = 0;
for (; i < currentSize; i++) {
Music music1 = music.getPos(i);
if(music1.getSong().equals(x)) {
index = i;
break;
}
}
if(i >= currentSize) {
System.out.println("没有你要删除的歌曲!");
return;
}
// 2、进行删除
for (int j = index; j < currentSize-1; j++) {
Music music2 = music.getPos(j+1);
music.setMusic(j,music2);
}
music.setMusic(currentSize,null);
music.setUsedSize(currentSize-1);
System.out.println("删除成功!");
}
}
(3)Search
public class Search implements Ioperation{
public void work(Musiclist music){
System.out.println("3.查询歌曲");
System.out.println("请输入查询的编号:");
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
Music music1=music.getPos(x);
System.out.println(music1);
}
}
(4)Modify
public class Modify implements Ioperation{
public void work(Musiclist music){
System.out.println("4.修改歌曲");
System.out.println("请输入要修改的歌曲:");
Scanner sc=new Scanner(System.in);
String x=sc.nextLine();
int sz = 0;
int size=music.getUsedSize();
for (int i = 0; i <size ; i++) {
if(x.equals(music.getPos(i).getSinger())){
sz=i;
break;
}
}
System.out.println("请输入修改后的歌曲:");
String z=sc.nextLine();
Music music1=new Music(z,music.getPos(sz).getYear(),music.getPos(sz).getSinger(),
music.getPos(sz).getType(),music.getPos(sz).getAlbum());
music.setMusic(sz,music1);
}
}
(5)Display
public class Display implements Ioperation{
public void work(Musiclist music){
System.out.println("5.打印歌曲");
int size=music.getUsedSize();
for (int i = 0; i <size ; i++) {
Music music1=music.getPos(i);
System.out.println(music1);
}
}
}
(0)退出系统
public class Exit implements Ioperation{
public void work(Musiclist music){
System.out.println("0.退出系统");
System.exit(0);
}
}
主函数
public class Main {
public static User login(){
System.out.println("请输入姓名:");
Scanner sc=new Scanner(System.in);
String name=sc.nextLine();
System.out.println("请输入你的身份:1.管理员 2.普通用户");
int x=sc.nextInt();
if(x==1){
return new AdminUser(name);
}else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
Musiclist music= new Musiclist();
User user=login(); //根据login函数调用管理员或者普通用户对象
while (true){
int choice=user.menu(); //choice接受选择的选项
user.doWork(choice,music); //调用doWork方法
}
}
}
三、效果展示
总结
这是用线性表实现的,链表也可以实现。中间很多地方还有漏洞,比如没有判断输入值的合法性,满了扩容问题,数组为空的情况等等,大家可以自行改进。