一、实验内容
- 题目1
基于类继承的思想,在编写一个媒体库程序,实现对CD、DVD、VideoGame、BoardGame四种媒体类型对象的进行统一管理,支持增、删、改、打印操作。
提示:请按照下图中类的继承关系编写程序。
二、实验运行结果
三、程序源代码
1.Item类
public class Item {
protected String title;
private int playingTime;
private String comment;
private boolean gotIt = false;
public Item(String title, int playingTime, String comment) {
//super();
this.title = title;
this.playingTime = playingTime;
this.comment = comment;
}
public void print() {
// TODO Auto-generated method stub
System.out.print(title);
}
}
2.CD类
package summer.dome;
public class CD extends Item {
private String artist;
private int numofTracks;
public CD(String title, String artist, int numofTracks, int playingTime, String comment) {
super(title, playingTime, comment);
this.artist = artist;
this.numofTracks = numofTracks;
}
public void print()
{
super.print();
System.out.println(":"+artist+"(CD)");
}
public static void main(String[] args) {
CD cd = new CD("Fire", "Tom", 10, 60, "Good");
cd.print();
}
}
3.DVD类
public class DVD extends Item {
private String director;
public DVD(String title, String director, int playingTime, String comment) {
super(title,playingTime,comment);
this.director = director;
}
public void print()
{
super.print();
System.out.println(":"+director+"(DVD)");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
4.Game类
public class Game extends Item {
private String numberOfPlayer;
public Game(String title, String numberOfPlayer, int playingTime, String comment) {
super(title,playingTime,comment);
this.numberOfPlayer = numberOfPlayer;
}
public void print()
{
super.print();
System.out.print(":"+numberOfPlayer);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
5.VideoGame类
public class VideoGame extends Game {
private String platform;
public VideoGame(String title, int playingtime, String comment, String numberOfPlayer,String platform) {
super(title,numberOfPlayer, playingtime, comment );
this.platform=platform;
// TODO Auto-generated constructor stub
}
@Override
public void print() {
super.print();
System.out.println(":"+platform+"(VideoGame)");
// TODO Auto-generated method stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
VideoGame vd=new VideoGame("dddf",6,",,,","2","lol");
vd.print();
}
}
6.BoardGame类
public class BoardGame extends Game {
private String platform;
public BoardGame(String title, int playingtime, String comment, String numberOfPlayer,String platform) {
super(title,numberOfPlayer, playingtime, comment );
this.platform=platform;
// TODO Auto-generated constructor stub
}
@Override
public void print() {
super.print();
System.out.println(":"+platform+"(BoardGame)");
// TODO Auto-generated method stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BoardGame bd=new BoardGame("lll",9,";;;","6","3");
bd.print();
}
}
7.DataBase类
package summer.dome;
import java.util.ArrayList;
public class DataBase {
//创建一个Itme容器
private ArrayList<Item> listItem = new ArrayList<Item>();
//增加一个itme
public void add(Item item) {
listItem.add(item);
}
//根据下标删除一个item
public void remove(int index){listItem.remove(index);}
//根据下标修改item
public void setListItem(int index,Item item){listItem.set(index,item);}
//依次打印容器中的item
public void list() {
for (Item item : listItem) {
item.print();
}
}
public static void main(String[] args) {
DataBase db = new DataBase();//新建一个媒体库对象
System.out.println("——————向媒体库中添加各媒体控件后打印——————");
db.add(new CD("Fire", "Tom", 10, 60, "Good"));
db.add(new CD("Haa", "Alice", 12, 100, "Bad"));
db.add(new DVD("MV", "Zhang", 120, "....."));
db.add(new VideoGame("英雄联盟",60,"好玩","10","电脑"));
db.add(new BoardGame("中国象棋",40,"烧脑","2","手机"));
db.list();
System.out.println();
System.out.println("——————向媒体库中删除第二个控件后打印——————");
db.remove(1);
db.list();
System.out.println();
System.out.println("——————向媒体库中修改第一个控件后打印——————");
db.setListItem(0,new CD("The sun", "Alice", 12, 100, "Bad"));
db.list();
}
}