您的位置:首页 > 移动开发 > 微信开发

Java —— 关于图书馆图书存放的操作小程序

2017-03-06 20:55 435 查看
/*
*
* class TestLibrary
*
* the main
*
*/

public class TestLibrary {

public static void main(String[] args){

Library l = new Library();
System.out.println(l.toString());
Book b1 = new Book("laoshe","《茶馆》","978-7-111-33414-9");
Book b2 = new Book("luxun","《祝福》","978-7-111-33414-9");
System.out.println(b1.toString());
System.out.println(b2.toString());
if(b1.equals(b2)){
System.out.println("b1 is equal to b2.");
}
else{
System.out.println("b1 is not equal to b2.");
}
DVD d;
d = new DVD("Star wars");
l.addAMedia(d);
l.Media_sum ++;
for(int i = 0; i<100; i++){
l.addAMedia(b1);
l.Media_sum ++;
}
System.out.println(l.toString());
System.out.println("用一个静态成员变量对图书计数:"+l.Media_sum);
System.out.println("调用库函数来得到图书的数量:"+l.getNumberOfBooks());
}
}

/*
*
* class Library
*
*/

// 将java中含有ArrayLiat的包引入
//也可以写成是import java.util.*;
//这样写是引入所有的包,当然包括指定的含有ArrayList的包了...
import java.util.ArrayList;

public class Library {
/*
*
* 本来ArrayList<> ..; 中写的是Book,代表的是这个图书馆的''库''是用来存储Book类中创建的对象的。
* 现在改成了Object,则是说明现在的库中元素是一个Object对象,代表它可以存储所有的类中的对象,换句话说,
* 这个图书馆中存放的元素现在是通用的了。
* 当然了,后边的objects是怎么回事呢。这个就是自愿的了。
* 就像C++ STL库中的stack,queue,map等的容器一样,就是你给容器起的一个名字。是程序员主观上定义的。
*
*/
public ArrayList<Object> objects;
public static int Media_sum= 0;
public Library(){
objects = new ArrayList<Object>();
}

public void addAMedia(Object b){
objects.add(b);
}

public String toString(){
return "Media in the library:\n" + objects.toString();
}

public void remove(Book b){
objects.remove(b);
}
public int getNumberOfBooks(){
return objects.size();
}

}

/*
*
* class Book
*
*/

public class Book {
private String author;
private String title;
private String isbn;

public Book(String au,String ti,String is){
author = au;
title = ti;
isbn = is;
}
public String toString(){
return "Book: "+ title + " author:"+author+" ISBN:"+isbn + "\n";
}

public boolean equals(Object o){
boolean result = false;
Book temp = (Book)o;
if(this.isbn == temp.isbn && this.title.equals(temp.getTitle()) ){
result = true;
}
return result;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getIsen() {
return isbn;
}

public void setIsen(String isbn) {
this.isbn = isbn;
}
}

/*
*
* class DVD
*
*/

public class DVD {
private String title;

public DVD(String title){
this.title = title;
}

public String getTitel() {
return title;
}

public String toString(){
return "DVD: " + title + "\n";
}
public void setTitel(String title) {
this.title = title;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐