UVA 230 Borrowers (book Management system) _uva

Source: Internet
Author: User

The Simulation book Management system, first input a number of books titles and authors (the title is different to end), and then a number of instructions: Borrow instructions to borrow books, return instructions to return the book, The shelve directive indicates that the books that have been returned but have not yet been placed are sorted and inserted into the bookshelf and entered the title and insertion position of the book (perhaps the first book or the back of a book), and the method of book sorting is to sort by author from small to large, and then by title from small to Before dealing with the first instruction, you should sort all the books in this way.

Train of thought: direct simulation, set up book{title, author}, use vector to save, then sort by rules, and write down the author of each book in map, shelve the back of the book is sorted according to the above rules, And then save it to the vector type of returnbook, and each output one, the first data is removed from the Returnbook. Know Returnbook is empty.

Import java.util.Collections;
Import Java.util.Comparator;
Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.Scanner;

Import Java.util.Vector;
		public class Main {public static void main (string[] args) {Scanner scan = new Scanner (system.in);
		Vector<book> v = new vector<> ();
		vector<book> Returnbook = new vector<> ();
		map<string,string> map = new hashmap<> ();
			while (true) {String str = scan.nextline ();
			if ("End". Equals (str)) break;
			int index = str.indexof (' "', 1);
			String bookname = str.substring (1,index);
			String author = str.substring (index+5);
			V.add (new book (Bookname,author));
		Map.put (BookName, author);
		} collections.sort (V, New CMP ());
			while (true) {String str = scan.nextline ();
			if ("End". Equals (str)) break;
					if ("SHELVE". Equals (str)) {if (!returnbook.isempty ()) {Collections.sort (returnbook,new Cmp ());
					Collections.sort (V,new Cmp ()); for (int i=0;i<v.size () &&!returnbook. IsEmpty (); i++) {if (V.get (i). Bookname.equals (Returnbook.get (0). BookName)) {if (i!=0) {System.out.print
							F ("Put \"%s\ "after \"%s\ "\ n", V.get (i). Bookname,v.get (i-1). BookName);
							}else{System.out.printf ("Put \"%s\ "first\n", V.get (i). BookName);
						} returnbook.remove (0);
			}} System.out.println ("End");
				}else{String bookname = str.substring (8,str.length ()-1);
				String author = map.get (bookname);
				if (Str.charat (0) = = ' B ') {remove (v,bookname);
					} if (Str.charat (0) = = ' R ') {Returnbook.add (new book (Bookname,author));
				V.add (new book (Bookname,author)); }}} public static void-Remove (vector<book> v,string book) {for (int i=0;i<v.size (); i++) {if (v.ge
				T (i). Bookname.equals (book)) {v.remove (i);
			Return }} Static Class Cmp implements comparator<book>{@Override public int compare (book O1, book O2) {// TODO auto-generated Method Stub if (o1.author.coMparetoignorecase (O2.author)!=0) return o1.author.compareToIgnoreCase (O2.author);
		else return o1.bookname.compareToIgnoreCase (o2.bookname);
		} static class book{String bookname;
		String author;
			Public book (String bookname,string author) {this.bookname = BookName;
		This.author = author; }
	}
}


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.