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的更多相关文章

  1. mit java open course assignment #2

    package come; public class Marothon { public static void FirstName(String[] args1,int[] args2){ int ...

  2. Java基础-赋值运算符Assignment Operators与条件运算符Condition Operators

    Java基础-赋值运算符Assignment Operators与条件运算符Condition Operators 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.赋值运算符 表 ...

  3. Java实习生面试题整理

    一.数据类型 包装类型 八个基本类型: boolean/1 byte/8 char/16 short/16 int/32 float/32 long/64 double/64 基本类型都有对应的包装类 ...

  4. 【BATJ面试必会】Java 基础篇

    一.数据类型 包装类型 缓存池 二.String 概览 不可变的好处 String, StringBuffer and StringBuilder String Pool new String(&qu ...

  5. Abschlussarbeit:Konstruktion und Implementierung von Dota2 Datenbank Intelligent Verwaltungsplatfom

    1.Die Hintergrund und Bedeutung des Themas Dank nicht ausreichendes Erkenntnisse der Spielplanner un ...

  6. JStorm之Topology调度

      topology在服务端提交过程中,会经过一系列的验证和初始化:TP结构校验.创建本地文件夹并拷贝序列化文件jar包.生成znode用于存放TP和task等信息,最后一步才进行任务分配.例如以下图 ...

  7. DIY申请达姆施塔特工业大学计算机专业(Informatik)硕士(Master)【附个人简历和动机信】

    1.个人概况 双非院校本科毕业两年,已来德国一年. APS绩点2.5,均分80.17. 2020.6月考出5443德福成绩,7月中下旬递交材料,9月初获得Zulassung(录取) PS:后来考出54 ...

  8. SpringBoot定时任务 - 什么是ElasticJob?如何集成ElasticJob实现分布式任务调度?

    前文展示quartz实现基于数据库的分布式任务管理和job生命周期的控制,那在分布式场景下如何解决弹性调度.资源管控.以及作业治理等呢?针对这些功能前当当团队开发了ElasticJob,2020 年 ...

  9. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

随机推荐

  1. SQL Server 创建索引的 5 种方法

    前期准备: create table Employee (            ID int not null primary key,            Name nvarchar(4),  ...

  2. 再次优化NGINX+php-fpm上传

    上次写了一篇nginx+php-fpm优化上传,一位博友留言介绍了,第三方nginx upload module http://www.grid.net.ru/nginx/upload.en.html ...

  3. Eclipse Useful Plugins Links

    1.maven for eclipse http://m2eclipse.sonatype.org/sites/m2e 2. svn1.6  for ecipse http://subclipse.t ...

  4. 博客终于开通了happy

    HelloWorld! 在我不懈的申请下,我的博客终于在第4次申请后成功开通了! 作为一个毕业两年的码农,现在才开始想要记录一些东西,似乎有点晚 -_-! 希望多年以后可以在这看到我长长的足迹!

  5. Rule Or WorkFlow

    The main value of a Workflow engine is that it makes it possible to customize the flows through some ...

  6. Hadoop--Hadoop的机架感知

    Hadoop的机架感知 Hadoop有一个“机架感知”特性.管理员可以手工定义每个slave数据节点的机架号.为什么要做这么麻烦的事情?有两个原因:防止数据丢失和提高网络性能.     为了防止数据丢 ...

  7. STM32F103控制两个步进电机按照一定转速比运动

    这个暑假没有回家,在学校准备九月份的电子设计竞赛.今天想给大家分享一下STM32定时器控制两个步进电机按照一定速度比转动的问题. 这次做的05年的电子设计竞赛题目,运动悬挂系统..本实验是控制两个步进 ...

  8. #include <boost/shared_ptr.hpp>

    共享指针 这个智能指针命名为boost::shared_ptr,定义在boost/shared_ptr.hpp里.智能指针boost::shared_ptr基本上类似于boost::scoped_pt ...

  9. 设计师Yoyo:为用户设计产品,让他们生活更美好

    Yoyo设计走过的路:纽约爱立信,西雅图美国在线,硅谷雅虎,ATT,深圳腾讯,华为:Yoyo不仅是顶级的交互体验设计师,还是很Open的知识分享者,从职业选择,以及对年轻人的建议几个角度,摘录他的文章 ...

  10. HTML 基础 1

    1. 文件结构: HTML文件的固定结构: <html> <head>...</head> <body>...</body> </ht ...