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} /* ...
随机推荐
- px,dp,dip,sp,in,mm,pt详细分析
px,dp,dip,sp,in,mm,pt详细分析 px :(pixels),屏幕的像素点,不同的设备显示效果相同,一般我们HVGA代表320x480像素,这个用的比较多. dip :(devi ...
- Mybatis.net与MVC入门配置及联合查询动态SQL拼接和简单事务
第一次学习Mybatis.net,在博客园也找到好多资料,但是在配置成功之后也遇到了一些问题,尤其是在动态SQl拼接时候,这里把遇到的问题还有自己写的一个Demo贴出来,希望能帮到新手,有不适合的地方 ...
- Google机器学习笔记 4-5-6 分类器
转载请注明作者:梦里风林 Google Machine Learning Recipes 4 官方中文博客 - 视频地址 Github工程地址 https://github.com/ahangchen ...
- SQL Server 创建索引的 5 种方法
前期准备: create table Employee ( ID int not null primary key, Name nvarchar(4), ...
- 局域网内IP冲突怎么办
对于在Internet和Intranet网络上,使用TCP/IP协议时每台主机必须具有独立的IP地址,有了IP地址的主机才能与网络上的其它主机进行通讯.但IP地址冲突会造成网络客户不能正常工作,只 ...
- 四巧工作简化法(ECRS)
在构思项目的工作方法时,可以运用ECRS(四巧工作简化法)技术,即E取消.C合并.R重排.S简化等四种技巧. 1.取消(Eliminate) 对所做的项目,首先应当考虑取消的可能性.如果所做的项目.工 ...
- NEC红外遥控协议理解与实现
红外发射管有2个管脚,发送的是经过38KHz时钟调制过的信号.例如下图使用PWM产生一个等占空时钟信号用于调制. 接收管收下来的信号已经经过了解调,可以直接连接系统的外部中断脚. 下面通过逻辑分析仪来 ...
- 【SDK编程】
#include <stdio.h> #include <windows.h> int main() { DeleteFile("C:\\test.txt" ...
- Cannot convert '0000-00-00 00:00:00' to TIMESTAMP
在url上添加参数 zeroDateTimeBehavior=convertToNull jdbc:mysql://localhost/myDatabase?zeroDateTimeBehavior= ...
- C语言strcmp()函数:比较字符串(区分大小写)
头文件:#include <string.h> strcmp() 用来比较字符串(区分大小写),其原型为: int strcmp(const char *s1, const char *s ...