mit java open course assignment #4
package come;
public class Library {
// Add the missing implementation to this class
String realLocation;
String[][] bookCollection = new String[2][10];
int index = 0;
public Library(String Location) {
realLocation = Location;
}
static void printOpeningHours() {
System.out.println("Libraries are open daily from 9am to 5pm.");
}
void printAddress() {
System.out.println(realLocation);
}
void addBook(Book collection) {
bookCollection[0][index] = collection.title;
bookCollection[1][index] = "1";
index++;
}
void borrowBook(String S) {
int index1 = 0;
while (!S.equals(bookCollection[0][index1])) {//越界的数字不能放在数组里面
index1++;
if(bookCollection[0].length == index1)
break;
}
if (index1 == bookCollection[0].length) {
System.out.println("Sorry, this book is not in our catalog.");
} else if (bookCollection[1][index1] == "1") {
System.out.println("You successfully borrowed The Lord of the Rings");
bookCollection[1][index1] = "0";
} else if (bookCollection[1][index1] == "0") {
System.out.println("Sorry, this book is already borrowed.");
}
}
void printAvailableBooks() {
int index2 = 0;
int flag = 0;
while (true) {
if (index2 == bookCollection[0].length){
break;
}
if (bookCollection[1][index2] == "1"){
flag = 1;
System.out.println(bookCollection[0][index2]);}
index2++;
}
if(flag == 0){
System.out.println("no book in catalog");
}
}
void returnBook(String bookName) {
for (int i = 0; i < bookCollection[0].length; i++) {
if (bookName.equals(bookCollection[0][i])) {
bookCollection[1][i] = "1";
break;
}
}
System.out.println("You successfully returned The Lord of the Rings");
}
public static void main(String[] args) {
// Create two libraries
Library firstLibrary = new Library("10 Main St.");
Library secondLibrary = new Library("228 Liberty St.");
// set the array 0
for (int i = 0; i < firstLibrary.bookCollection[0].length; i++) {
firstLibrary.bookCollection[1][i] = "0";
secondLibrary.bookCollection[1][i] = "0";
}
// Add four books to the first library
firstLibrary.addBook(new Book("The Da Vinci Code"));
firstLibrary.addBook(new Book("Le Petit Prince"));
firstLibrary.addBook(new Book("A Tale of Two Cities"));
firstLibrary.addBook(new Book("The Lord of the Rings"));
// Print opening hours and the addresses
System.out.println("Library hours:");
printOpeningHours();
System.out.println();
System.out.println("Library addresses:");
firstLibrary.printAddress();
secondLibrary.printAddress();
System.out.println();
// Try to borrow The Lords of the Rings from both libraries
System.out.println("Borrowing The Lord of the Rings:");
firstLibrary.borrowBook("The Lord of the Rings");
firstLibrary.borrowBook("The Lord of the Rings");
secondLibrary.borrowBook("The Lord of the Rings");
System.out.println();
// Print the titles of all available books from both libraries
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();
System.out.println();
System.out.println("Books available in the second library:");
secondLibrary.printAvailableBooks();
System.out.println();
// Return The Lords of the Rings to the first library
System.out.println("Returning The Lord of the Rings:");
firstLibrary.returnBook("The Lord of the Rings");
System.out.println();
// Print the titles of available from the first library
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();
}
}
可优化的地方:此处用的一个String的二维数组来模拟的,实际上应该写一个类,然后建立一个对象的链表或数组就很好
String ar[2][3]即表示两行,每行有3个串
mit java open course assignment #4的更多相关文章
- mit java open course assignment #2
package come; public class Marothon { public static void FirstName(String[] args1,int[] args2){ int ...
- Java基础-赋值运算符Assignment Operators与条件运算符Condition Operators
Java基础-赋值运算符Assignment Operators与条件运算符Condition Operators 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.赋值运算符 表 ...
- Java实习生面试题整理
一.数据类型 包装类型 八个基本类型: boolean/1 byte/8 char/16 short/16 int/32 float/32 long/64 double/64 基本类型都有对应的包装类 ...
- 【BATJ面试必会】Java 基础篇
一.数据类型 包装类型 缓存池 二.String 概览 不可变的好处 String, StringBuffer and StringBuilder String Pool new String(&qu ...
- Abschlussarbeit:Konstruktion und Implementierung von Dota2 Datenbank Intelligent Verwaltungsplatfom
1.Die Hintergrund und Bedeutung des Themas Dank nicht ausreichendes Erkenntnisse der Spielplanner un ...
- JStorm之Topology调度
topology在服务端提交过程中,会经过一系列的验证和初始化:TP结构校验.创建本地文件夹并拷贝序列化文件jar包.生成znode用于存放TP和task等信息,最后一步才进行任务分配.例如以下图 ...
- DIY申请达姆施塔特工业大学计算机专业(Informatik)硕士(Master)【附个人简历和动机信】
1.个人概况 双非院校本科毕业两年,已来德国一年. APS绩点2.5,均分80.17. 2020.6月考出5443德福成绩,7月中下旬递交材料,9月初获得Zulassung(录取) PS:后来考出54 ...
- SpringBoot定时任务 - 什么是ElasticJob?如何集成ElasticJob实现分布式任务调度?
前文展示quartz实现基于数据库的分布式任务管理和job生命周期的控制,那在分布式场景下如何解决弹性调度.资源管控.以及作业治理等呢?针对这些功能前当当团队开发了ElasticJob,2020 年 ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
随机推荐
- HDU1043 Eight(BFS)
Eight(South Central USA 1998) Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- CodeForces 214B Hometask
本题求n个数组成的大数,要求是2,3,5的倍数. 因为是2 和5 的倍数,所以个位为 0:所以若n个数中没有0,直接输出-1: 难点就是要求为3 的倍数. 因为若某个数为3的倍数,则其各位数的和必然是 ...
- VB.NET生成Excel,已存在提示框点否时报错
如题 Exception from HRESULT: 0x800A03EC 最终没有好的解决方案,只好屏蔽掉 Try obook.SaveAs(excelSaveName) Catch ex As S ...
- java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream(转)
java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream 使用Tomcat的Manag ...
- Oracle EBS-SQL (INV-11):检查子库存会计信息.sql
select OOD.ORGANIZATION_CODE 库存组织代码, ...
- YUI的模块化开发
随着互联网应用越来越重,js代码越来越庞大,如何有效的去组织自己的代码,变得非常重要.我们应该学会去控制自己的代码,而不是到最后一堆bug完全不知道从哪冒出来.前端的模块化开发可以帮助我们有效的去管理 ...
- LINUX总结第13篇:LINUX下动态库及版本号控制
感觉讲得挺详细 注: ln 命令用法 ln –s 源文件 目标文件 (目标文件即为软链接文件) 可用ls -l查看软链接文件具体指向哪个文件 目录[-] 1. File libhello.c 2. F ...
- 怎样查看修改sqlserver数据库的编码格式
原文地址:http://zhidao.baidu.com/question/107168202.html SELECT COLLATIONPROPERTY('Chinese_PRC_Stroke_CI ...
- Eclipse setting Java code style and codetemplate
1.open the eclipse tool window First click the Window menu,then check the children's menu which name ...
- 【HDU】4092 Nice boat(多校第四场1006) ——线段树 懒惰标记
Nice boat Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...