Java实验:类的设计

1)实验题目:This question involves the design and implementation of a LibraryBook class of objects representing library books.

a) Decide what information needs to be maintained about each library book. At this stage, do not worry about information needed to support operations on library books, but only about information that to represent the books in the library. Indicate the type of each piece of information (you should not need to use types other than the basic types and String). You should assume there is only one copy of each book.

b) The only complex operations of the LibraryBook class are those involved in the borrowing process. These are as follows:

reserve: Mark the books as being reserved. Only one person can reserve a book at a time and a book cannot be borrowed or reserved by anyone else whilst it is reserved. Do nothing if reservation is not possible.

ableToBorrow: Returns a boolean value indicating whether the book can currently be borrowed, which is only possible if the book is not already on loan or reserved by someone else.

borrow: Record that the book is on loan, if it is currently able to be borrowed.

cancelReservation: Cancels a previous reservation. This can only be done by the person who placed the reservation.

return: Record that the book has been returned. This can only be done by the person who borrowed it.

You should assume that the person attempting to do the operation is passed in as a String parameter for each of these operations. Your first task is to write the method signatures of these five methods.

c) Now, design algorithms for each of the five operations in the previous

question.

d) Finally, implement the LibraryBook class. Before you write the methods, declare any instance variables, including both the variable from the first part of the question and any instance variables needed in the methods you have just designed. After that, implement the methods by translating your algorithms into Java. Finally, implement a constructor method, this should take parameters corresponding to each piece of data you identified in the first part of the question and should initialize that data using those parameters. You should also ensure that any other instance variables you use in your methods are initialized properly (e.g. a book should not be on loan or reserved when the object representing it is created).

 

2)实验思路:

在LibraryBook类下创建成员变量id和bookname,表示该书的编号和名字,其中id能唯一标识该书。

布尔变量isreserved和isborrowed分别表示该书是否被预定和是否被借出。person标记当前预定或借出该书的人的名字。

ableToBorrow方法当且仅当该书既没被预定也没被借出时返回true。

预定/取消预定/借出/还书四个方法传入表示人名的字符串参数,执行完后输出当前操作的具体信息,方便观察实验结果。

因为题目要求每本书只能有一个副本,所以应该避免同一id的书被创建两次。用HashMap记录已经创建的书,私有化构造方法,用静态方法creatnewbook来创建新书。当一本书已经被创建并被记录在HashMap中时,第二次创建id与之相同的书将会失败。

 

3)实验源码

package 实验;
import java.util.*;

class LibraryBook
{
	private int id;
	private String bookname;
	static HashMap<Integer, String> map = new HashMap<Integer, String>(); //储存已经创建的书
	private LibraryBook(int id,String bookname){  //构造方法
		this.id=id;
		this.bookname=bookname;
	}
	public static LibraryBook creatnewbook(int id,String bookname) { //确保每本书只被创建一次
		if(map.containsKey(id)) {
			System.out.println(" there should be only one copy of each book. Book can't be created twice.");
			return null;
		}
		LibraryBook book = new LibraryBook(id, bookname); 
		map.put(id, bookname); //记录该书
		return book;
	}
	
	private boolean isreserved; //该书是否被预定
	private boolean isborrowed; //该书是否被借出
	private String person;      //预定或者借出该书的人
	
	public void reserve(String person) {  //预定该书
		if(this.ableToBorrow()==true) {
			this.person=person;
			this.isreserved=true;
			System.out.println(this.bookname+" is reserved by "+person);
		}
		else System.out.println(this.bookname+" can't be reserved by "+person);
	}
	
	public boolean ableToBorrow() {   //该书是否可借
		return (!this.isborrowed)&&(!this.isreserved);
	}
	
	public void borrow(String person) {  //借出该书
		if(this.ableToBorrow()==true) {
			this.person=person;
			this.isborrowed=true;
			System.out.println(this.bookname+" is borrowed by "+person);
		}
		else System.out.println(this.bookname+" can't be borrowed by "+person);
	}
	
	public void cancelReservation(String person) { //取消预定该书
		if(this.person==person &&  this.isreserved==true) {
			this.isreserved=false;
			System.out.println(this.bookname+"'s reservation is cancelled by "+person);
		}
		else System.out.println(this.bookname+" can't be cancelled by ."+person);
	}
	
	public void returnbook(String person) {  //还书
		if(this.person==person && this.isborrowed==true) {
			this.isborrowed=false;
			System.out.println(this.bookname+" is returned by "+person);
		}
		else System.out.println(this.bookname+" can't be returned by ."+person);
	}
	
}
public class Text {

	public static void main(String[] args) {
		LibraryBook book1 = LibraryBook.creatnewbook(0, "语文");
		
		book1.borrow("张三");
		book1.reserve("李四");
		book1.returnbook("张三");
		book1.reserve("李四");
		
		LibraryBook book2 = LibraryBook.creatnewbook(1, "英语");
		LibraryBook book3 = LibraryBook.creatnewbook(1, "英语"); 
		
		book2.reserve("王五");
		book2.borrow("赵六");
		book2.cancelReservation("王五");
	}

}

 

4)实验结果:

当一本书被借出时,不能被其他人预定,除非该书被归还。

创建id=1的英语书两次,则第二次不能成功,因为一本书只能有一个副本。

实验结果说明代码实现符合题目要求。

 

5)实验心得:

该题涉及了Java类的建立和相关操作。

构造方法不能有返回值,方法名应和类名相同,且不能用static和final关键字修饰。当类中存在显式构造方法,默认的无参构造方法将不复存在,除非显式定义无参构造方法。

因为题目每本书只能有一个副本,所以不能有两个成员变量id相同的实例被创建。在此应该把构造方法私有化,用静态方法creatnewbook返回创建的实例。这样,用HashMap记录已经创建的书,在调用静态方法creatnewbook就可以检查该id标识的书是否已经被创建过,避免创建id相同的书。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值