MIT Java Open Course Assignment #4

Source: Internet
Author: User

 Packagecome; Public classLibrary {//ADD The missing implementation to this classString reallocation; String[][] Bookcollection=NewString[2][10]; intindex = 0;  PublicLibrary (String location) {reallocation=Location ; }    Static voidprintopeninghours () {System.out.println ("Libraries is open daily from 9am to 5pm."); }    voidprintaddress () {System.out.println (reallocation); }    voidAddbook (Book collection) {bookcollection[0][index] =Collection.title; bookcollection[1][index] = "1"; Index++; }    voidBorrowbook (String S) {intindex1 = 0;  while(! S.equals (Bookcollection[0][index1])) {//numbers that are out of bounds cannot be placed inside an arrayindex1++; 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."); }    }    voidPrintavailablebooks () {intIndex2 = 0; intFlag = 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"); }            }    voidReturnbook (String bookname) { for(inti = 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 voidMain (string[] args) {//Create A librariesLibrary firstlibrary =NewLibrary ("Ten Main St."); Library secondlibrary=NewLibrary ("228 Liberty St."); //set the array 0         for(inti = 0; i < firstlibrary.bookcollection[0].length; i++) {firstlibrary.bookcollection[1][i] = "0"; secondlibrary.bookcollection[1][i] = "0"; }        //Add Four Books to the first libraryFirstlibrary.addbook (NewBook ("The Da Vinci Code")); Firstlibrary.addbook (NewBook ("Le Petit Prince")); Firstlibrary.addbook (NewBook ("A Tale of Cities")); Firstlibrary.addbook (NewBook ("The Lord of the Rings")); //Print Opening Hours and the addressesSystem.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 librariesSystem.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 librariesSystem.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 librarySystem.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 librarySystem.out.println ("Books available in the first library:");    Firstlibrary.printavailablebooks (); }}
View Code

The place to be optimized: a two-dimensional array of strings used here to simulate, actually should write a class, and then build an object linked list or array is good

String Ar[2][3] that represents two rows with 3 strings per row

MIT Java Open Course Assignment #4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.