歌曲
编号、歌名、语言(中文/英文)、类别(流行/通俗)、歌手
Song.java
1 package ex8; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Scanner; 6 import java.util.Set; 7 8 public class Song { 9 10 11 private int sid; 12 private String sname; 13 private String category; 14 private String singer; 15 16 public Song() { 17 18 } 19 20 public Song(int sid, String sname, String category, String singer) { 21 super(); 22 this.sid = sid; 23 this.sname = sname; 24 this.category = category; 25 this.singer = singer; 26 } 27 28 public int getSid() { 29 return sid; 30 } 31 32 public void setSid(int sid) { 33 this.sid = sid; 34 } 35 36 public String getSname() { 37 return sname; 38 } 39 40 public void setSname(String sname) { 41 this.sname = sname; 42 } 43 44 public String getCategory() { 45 return category; 46 } 47 48 public void setCategory(String category) { 49 this.category = category; 50 } 51 52 public String getSinger() { 53 return singer; 54 } 55 56 public void setSinger(String singer) { 57 this.singer = singer; 58 } 59 60 61 }
SongDao.java
1 package ex8; 2 3 import java.util.List; 4 5 public class SongDao { 6 7 private List<Song> songList; 8 9 public SongDao() { 10 11 } 12 13 public SongDao(List<Song> songList) { 14 super(); 15 this.songList = songList; 16 } 17 18 // 新增歌曲 19 public void add(Song song) { 20 songList.add(song); 21 } 22 23 // 查询歌曲 24 // 按id查找 25 public Song search(int sid) { 26 for(Song song : songList) { 27 if(song.getSid()==sid){ 28 return song; 29 } 30 } 31 return null; 32 } 33 // 按对象查找 34 public Song search(Song song) { 35 if(song==null) { 36 return null; 37 } 38 return search(song.getSid()); 39 } 40 41 // 删除歌曲 42 //按id删除歌曲 43 public void delete(int sid) { 44 Song song=search(sid); 45 if(song!=null) { 46 songList.remove(song); 47 } 48 } 49 //按对象删除歌曲 50 public void delete(Song song) { 51 if(song!=null) { 52 delete(song.getSid()); 53 } 54 } 55 56 public List<Song> getSongList() { 57 return songList; 58 } 59 60 public void setSongList(List<Song> songList) { 61 this.songList = songList; 62 } 63 }
SongService.java
1 package ex8; 2 3 import java.util.List; 4 5 import javax.swing.Spring; 6 7 public class SongService { 8 private SongDao dao; 9 10 public SongService() { 11 super(); 12 // TODO Auto-generated constructor stub 13 } 14 15 public SongService(SongDao dao) { 16 super(); 17 this.dao = dao; 18 } 19 20 public void add(Song song) { 21 dao.add(song); 22 } 23 24 public Song search(int sid) { 25 return dao.search(sid); 26 } 27 28 public Song search(Song song) { 29 return dao.search(song); 30 } 31 32 public void delete(Song song) { 33 dao.delete(song); 34 } 35 36 public void delete(int id) { 37 dao.delete(id); 38 } 39 40 41 42 43 public List<Song> trace(){ 44 return dao.getSongList(); 45 } 46 47 public void alter(Song song) { 48 49 } 50 51 public SongDao getDao() { 52 return dao; 53 } 54 55 public void setDao(SongDao dao) { 56 this.dao = dao; 57 } 58 59 60 }
SongView.java
1 package ex8; 2 3 import java.io.BufferedReader; 4 import java.io.InputStreamReader; 5 import java.net.DatagramPacket; 6 import java.util.ArrayList; 7 import java.util.List; 8 import javax.swing.text.AbstractDocument.BranchElement; 9 10 public class SongView { 11 12 private SongService service; 13 14 public SongView() { 15 super(); 16 // TODO Auto-generated constructor stub 17 } 18 19 public SongView(SongService service) { 20 super(); 21 this.service = service; 22 } 23 24 public void show() throws Exception{ 25 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 26 boolean stop=false; 27 while(!stop){ 28 System.out.println("**************************************************** " 29 +" **歌曲管理** " 30 +" 1-----------------------------------查找歌曲 " 31 +" 2-----------------------------------增加歌曲 " 32 +" 3-----------------------------------删除歌曲 " 33 +" 4-----------------------------------显示所有歌曲 " 34 +" 5-----------------------------------退出系统 " 35 +"**************************************************** " 36 +" **input [1-5]:"); 37 String line = br.readLine(); 38 int command=Integer.parseInt(line); 39 switch (command) { 40 case 1: 41 System.out.println("According to id or complete info?Input[1or2]"); 42 line = br.readLine(); 43 command=Integer.parseInt(line); 44 if(command==2) { 45 System.out.println("Input song's id"); 46 String sid=br.readLine(); 47 int id=Integer.parseInt(sid); 48 System.out.println("Input song's name"); 49 String sname=br.readLine(); 50 System.out.println("Input singer's name"); 51 String singerName=br.readLine(); 52 System.out.println("Input song's category"); 53 String category=br.readLine(); 54 Song song=new Song(id,sname,category,singerName); 55 print(service.search(song)); 56 } 57 else { 58 System.out.println("Input song's id"); 59 String sid=br.readLine(); 60 int id=Integer.parseInt(sid); 61 print(service.search(id)); 62 } 63 break; 64 case 2: 65 System.out.println("Input new song's id"); 66 String sid=br.readLine(); 67 int id=Integer.parseInt(sid); 68 System.out.println("Input new song's name"); 69 String sname=br.readLine(); 70 System.out.println("Input new singer's name"); 71 String singerName=br.readLine(); 72 System.out.println("Input song's category"); 73 String category=br.readLine(); 74 Song song=new Song(id,sname,category,singerName); 75 service.add(song); 76 break; 77 case 3: 78 System.out.println("请输入待删除歌曲的编号"); 79 sid=br.readLine(); 80 id=Integer.parseInt(sid); 81 service.delete(id); 82 System.out.println("Delete seccessfully"); 83 break; 84 case 4: 85 List<Song>list=service.trace(); 86 print(list); 87 break; 88 case 5: 89 stop=true; 90 System.out.println("Byebye"); 91 break; 92 default: 93 System.out.println("Error in input,try again"); 94 break; 95 } 96 } 97 } 98 99 100 101 102 103 104 public void print(Song song) { 105 System.out.printf("%-10s%-15s%-15s%-15s ","ID","NAME","SINGER","CATEGORY"); 106 System.out.println("----------------------------------------------------------------------"); 107 System.out.printf("%-15d%-20s%-25s%-20s ", 108 song.getSid(),song.getSname(),song.getSinger(),song.getCategory()); 109 } 110 111 public void print(List<Song>list) { 112 System.out.printf("%-10s%-15s%-15s%-15s ","ID","NAME","SINGER","CATEGORY"); 113 System.out.println("----------------------------------------------------------------------"); 114 for(Song song: list) 115 System.out.printf("%-15d%-20s%-25s%-20s ", 116 song.getSid(),song.getSname(),song.getSinger(),song.getCategory()); 117 } 118 119 public static void main(String[] args) { 120 SongView view=new SongView(); 121 SongService service=new SongService(); 122 SongDao dao=new SongDao(); 123 List<Song>list=new ArrayList<>(); 124 dao.setSongList(list); 125 service.setDao(dao); 126 view.setSongService(service); 127 try { 128 view.show(); 129 } catch (Exception e) { 130 // TODO: handle exception 131 System.out.println(e); 132 } 133 } 134 135 public SongService getSongService() { 136 return service; 137 } 138 139 public void setSongService(SongService songService) { 140 this.service = songService; 141 } 142 143 }