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个串

最新文章

  1. Error:failed to find Build Tools revision 23.0.0 rc3
  2. DrawerLayout的openDrawer()和closeDrawer()方法
  3. Yii javascript 的结合 账号禁用 激活 的设置。
  4. Lumia刷机Win10 Mobile 10.0.10166惊魂记
  5. python编写接口
  6. [原创]使用benchmarksql和pgbench对PostgreSQL Plus Advanced Server进行性能测试
  7. 使用webstorm上传代码到github
  8. 05_Excel操作_01_简单导入导出
  9. verview of Spring Framework--转
  10. SQL SERVER 自定义函数 split
  11. Hadoop大数据零基础高端实战培训系列配文本挖掘项目
  12. LeetCode——ZigZag Conversion
  13. PHP面向对象之解释器模式
  14. Vue.js-06:第六章 - 按键修饰符的使用
  15. Python协程(真才实学,想学的进来)
  16. IDEA 出现错误:找不到或无法加载主类
  17. for循环以及数据类型
  18. HTML、CSS知识点,面试开发都会需要--No.7 数据列表
  19. 蓝牙协议分析(11)_BLE安全机制之SM
  20. pyhthon常用模块hashlib

热门文章

  1. python连接redis002
  2. 如何设置 Internal 类,方法,属性对其他项目可见
  3. Android事件侦听器回调方法浅谈
  4. Nand flash 的发展和eMMC
  5. Intuit Quicken Home &amp; Business 2016(Manage your business and personal finances)
  6. 在WPF中自定义你的绘制(四)
  7. 2014第8周三杂记及web标准学习
  8. SQLCLUSTER sql数据库监测工具
  9. phpadmin
  10. xmpp总结