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

时间: 2025-01-02 03:40:21

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 MinIndex,MinTime,i; MinTime = 10000; MinIndex = 0; for(i = 0; i < 16; i++){ if (args2[i] < MinTime){ MinTime = args2[i]; MinIndex = i; } } System.out

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

Java基础-赋值运算符Assignment Operators与条件运算符Condition Operators 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.赋值运算符 表达式的数据类型要与左边变量的类型兼容 1>.常规赋值 1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:[email protected]

Java实习生面试题整理

一.数据类型 包装类型 八个基本类型: boolean/1 byte/8 char/16 short/16 int/32 float/32 long/64 double/64 基本类型都有对应的包装类型,基本类型与其对应的包装类型之间的赋值使用自动装箱与拆箱完成. ? 1 2 Integer x = 2;     // 装箱 int y = x;         // 拆箱 缓存池 new Integer(123) 与 Integer.valueOf(123) 的区别在于: new Intege

java面试之java 基础

一.数据类型基本类型包装类型缓存池 二.String概览不可变的好处String, StringBuffer and StringBuilderString Poolnew String("abc") 三.运算参数传递float 与 double隐式类型转换switch 四.关键字finalstatic 五.Object 通用方法概览equals()hashCode()toString()clone() 六.继承访问权限抽象类与接口super重写与重载 七.反射 八.异常 九.泛型 十

最全的静态网站生成器(开源项目)

将动态网页静态化,可以有效减轻服务器端的压力,并且静态网页的访问速度要快于动态网页.此外,使用静态网页还有利于搜索引擎的收录,从而提高网站的搜索排名. 下面是StaticSiteGenerators网站收集整理的开源的静态网站生成器,按照编程语言进行分类,每个分类按照Star数进行排序. (关注ITeye官微,随时随地查看最新开发资讯.技术文章.) 目 录 [ - ] JavaScript 版 Ruby 版 Java/Groovy/Scala 版 PHP 版本 Python 版 Go 语言版 P

JStorm之Topology调度

topology在服务端提交过程中,会经过一系列的验证和初始化:TP结构校验.创建本地目录并拷贝序列化文件jar包.生成znode用于存放TP和task等信息,最后一步才进行任务分配,如下图: 提交主函数位于ServiceHandler.java中 private void makeAssignment(String topologyName, String topologyId, TopologyInitialStatus status) throws FailedAssignTopology

CSCI3120

CSCI3120Assignment 1Due: 11:59 AM, Tuesay, May 28, 2019IntroductionThe concepts applicable to operating systems are applicable in many other systems as well.For example, manufacturing, transportation, and bureaucracies. The air transportationsystem (

Web-crawler

导航 (返回顶部) 1. spider 1.1 robots.txt 1.2 ads.txt 1.3 security.txt 1.4 blogger.com 上的 robots.txt和ads.txt 2. 爬虫软件列表 2.1 介绍一些比较方便好用的爬虫工具和服务 2.2 33款可用来抓数据的开源爬虫软件工具(全都没试过) 3. 更多相关 1. spider 1.1 robots.txt 1.2 ads.txt 1.3 security.txt 1.4 blogger.com 上的 robo

AlgorithmsI Programming Assignment 1: PercolationStats.java

import edu.princeton.cs.algs4.StdOut; import edu.princeton.cs.algs4.StdRandom; import edu.princeton.cs.algs4.StdStats; /* *How do I generate a site uniformly at random among all blocked sites for use in PercolationStats? * Pick a site at random (by u