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;
}
}
*
* 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;
}
}
相关文章推荐
- 关于在Java程序当中创建对象等基本操作的性能测试
- 关于JAVA的分页查询操作技术(zz)
- 关于把 java 程序编译成 exe 文件
- 已知三个无符号数a,b,c,存放在ADR开始的连续单元,编制完成下列操作的程序:若a,b,c同时为偶数,将FLAG单元置1,若同时为奇数,FLAG置0,否则为全1
- java操作外部程序
- 用java写的远程监控程序,可以看到别人计算机上正在进行的操作(2)
- JAVA与C++::关于JNI中文字符串操作问题总结
- 关于"以前的某个程序安装已在安装计算机上创建挂起的文件操作"解决办法
- 关于《以前的某个程序安装已在安装计算机上创建挂起的文件操作》的解决办法
- 今天写了个用jxl.jar操作excel文件的java程序
- 关于六种Java异常处理程序的陋习
- 用UML设计Java应用程序-处理图书馆借阅和预定图书和杂志
- java中关于星期时间的操作
- [转]关于:以前的某个程序安装已在安装计算机上创建挂起的文件操作 解决办法
- 关于JAVA的分页查询操作技术
- 关于:以前的某个程序安装已在安装计算机上创建挂起的文件操作解决办法(SQL2000)
- 关于JAVA的分页查询操作技术
- Java 中以DataGrid形式显示数据库查询结果和相关JTable操作程序
- 关于安装SQL SERVER时出现“某个程序安装已在安装计算机上创建挂起的文件操作”的解决办法
- java中关于时间日期操作的常用函数