The purpose of this refactoring is to try and summarize it.CodeStructure. Many new users, like me, will always call a lot of methods in the main method, making the main method very bloated, such code is too ugly, I think, the main method should only serve ourProgramThe entry point is that it is only a starting function. It is best to have only one method to call, and then this method calls other methods. In this way, the call level will be higher, then we can also know how the logical connection between each method is. The main function is to process it like this, we can find out the logic redundancy in our code (some methods with similar functions but different appearances), and some unnecessary temporary variables and parameter lists, our method list can also be more concise. Another principle is that I think a method is better to be just a for loop in the logic sense (here the logic meaning refers to what we do in this loop, for example, I used to like to write a traversal loop at the beginning of a method and then write an output loop. This is two different things: Two logics, but if it is nested loop, then there is no way). Do not always write a lot of for loops in a method. This method is very bloated and ugly, it is best not to include the switch-case and while loops (if it is really necessary and there is no way), especially the switch-case, which makes our code unsightly executors.
These are my personal summaries. If you have any ideas, you can discuss them with me.
The Code is as follows:
Public Class Library { Public Static Void Main (string [] ARGs) Throws Interruptedexception, ioexception { Checkbookandgettimeofcheck (); } @ Suppresswarnings ( "Deprecation" ) Private Static Void Checkbookandgettimeofcheck () Throws Ioexception {string name =Entername (); thread t = New Thread ( New Call (); T. Start (); Long Starttime = System. currenttimemillis (); getbookfromlibrary (name); T. Stop (); Long Endtime = System. currenttimemillis (); system. Out. println ("Retrieval time:" + "\ n" +(Endtime-Starttime));
} Private Static String entername () Throws Ioexception {bufferedreader name = New Bufferedreader ( New Inputstreamreader (system. In); string entername = Name. Readline (). tolowercase (); Return Entername ;} Private Static Void Getbookfromlibrary (string name)Throws Ioexception {list <Ratingbook> List = Checknameandshowbookinzhongzhuantai (name); List <Ratingbook> mbooklist = Getbooklistinlibrary (name ); For ( Int I = 0; I <mbooklist. Size (); I ++ ){ For ( Int J = 0; j <list. Size (); j ++ ){ If (Mbooklist. get (I ). getbook ()). equals (list. get (j ). getbook () {mbooklist. get (I ). setrating (list. get (j ). getrating () ;}} showlist ( "List of books in the library :" , Mbooklist );} Private Static List <ratingbook> Checknameandshowbookinzhongzhuantai (string name) Throws Ioexception {list <Ratingbook> List = addbooktolist (6 ); List <Ratingbook> containsbook = New Arraylist <ratingbook> (); List <Ratingbook> deletebook = New Arraylist <ratingbook> (); System. Out. println ( "List of intermediate turntable books :" ); For ( Int I = 0; I <list. Size (); I ++ ) {String book = List. Get (I). getbook (). tolowercase (); system. Out. println (list. Get (I). getbook () + "Rating:" + List. Get (I). getrating ()); If (Book. Contains (name) {containsbook. Add (list. Get (I ));} Else {Deletebook. Add (list. Get (I) ;}} showlist ( "Eligible books :" , Containsbook); showlist ( "Books to be deleted :" , Deletebook ); Return Containsbook ;} Private Static List <ratingbook> addbooktolist ( Int Rating) {list <Ratingbook> List = New Arraylist <ratingbook> (); For ( Int I = 0; I <returnindex (10); I ++ ) {Ratingbook = New Ratingbook (); booklibrary bookarray = New Booklibrary (); book. setbook (bookarray. bookarray [returnindex ( 10)]); Book. setrating (returnindex (rating); list. Add (book );} Return List ;} Private Static Int Returnindex ( Int I ){ Return New Random (). nextint (I );} Private Static Void Showlist (string, list <ratingbook> List) {sortbook (list); system. Out. println (string ); For ( Int I = 0; I <list. Size (); I ++ ) {System. Out. println (list. Get (I). getbook () + "Rating:" + List. Get (I). getrating ());}} Private Static List <ratingbook> sortbook (list <ratingbook> List ){ // Remove a book of the same name For ( Int I = 0; I <list. Size ()-1; I ++ ){ For ( Int J = I + 1; j <list. Size (); j ++ ){ If (List. get (I ). getbook ()). equals (list. get (j ). getbook () {list. remove (j) ;}} sortbookbyrating (list ); Return List ;} Private Static Void Sortbookbyrating (list <ratingbook> List ){ For ( Int I = 0; I <list. Size ()-1; I ++ ){ For ( Int J = I + 1; j <list. Size (); j ++ ){ If (List. Get (I). getrating () <= (List. Get (j). getrating () {string temp = List. Get (I). getbook (); Int Rating = List. get (I ). getrating (); list. get (I ). setbook (list. get (j ). getbook (); list. get (I ). setrating (list. get (j ). getrating (); list. get (j ). setbook (temp); list. get (j ). setrating (rating );}}}} Private Static List <ratingbook> Getbooklistinlibrary (string name) {list <Ratingbook> booklist = addbooktolist (1 ); List <Ratingbook> mbooklist = New Arraylist <ratingbook>(); For ( Int I = 0; I <Booklist. Size (); I ++ ){ If (Booklist. Get (I). getbook (). Contains (name) {mbooklist. Add (Booklist. Get (I ));}} Return Mbooklist ;}}
The result is as follows:
List of intermediate turntable books:
Java and JavaScript score: 4
Java score: 2
Eligible books:
Java and JavaScript score: 4
Books to be deleted:
Java score: 2
List of books in the library:
Java and JavaScript score: 4
The search takes the following time:
3