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} /* ...
随机推荐
- ——转 token 介绍
学习Token Token是什么? Token是服务端生成的一串字符串,以作客户端进行请求的一个令牌,当第一次登录后,服务器生成一个Token便将此Token返回给客户端,以后客户端只需带上这个Tok ...
- css案例学习之relative与absolute
代码 <!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...
- Java报表开发组件DynamicReports
DynamicReports 是一个基于 JasperReports 进行扩展的 Java 报表库,可用它来快速创建报表而无需可视化报表设计工具. From : http://www.oschina ...
- sql 2000 "无法执行查询,因为一些文件缺少或未注册"的
sql 2000 "无法执行查询,因为一些文件缺少或未注册"的解决办法 在SQL server 2000中打开表查看数据的时候,提示说“无法执行查询,因为一些文件缺少或未注册” 用 ...
- hdu 1279 验证角谷猜想(简单的模拟)
Problem Description 数论中有许多猜想尚未解决,其中有一个被称为“角谷猜想”的问题,该问题在五.六十年代的美国多个著名高校中曾风行一时,这个问题是这样描述的:任何一个大于一的自然数, ...
- redi中删除所有的数据
EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 *
- Baby Ming and Matrix games(dfs计算表达式)
Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- uva 10635 - Prince and Princess(LCS)
题目连接:10635 - Prince and Princess 题目大意:给出n, m, k,求两个长度分别为m + 1 和 k + 1且由1~n * n组成的序列的最长公共子序列长的. 解题思路: ...
- Linux 程序启停脚本
start.sh #!/bin/sh java -jar ./program.jar & echo $! > /var/run/program.pid stop.sh #!/bin/sh ...
- XP用户:消除误解,大胆拥抱Linux
4月23日.知名家评论家Silviu Stahie发表文章.题为"Windows Users and Their Misconceptions About Linux". ...