Uva230

来源:互联网 发布:天池大数据竞赛官网 编辑:程序博客网 时间:2024/04/30 06:23
package test;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.Random;import java.util.Scanner;public class Test{    static class Book{        String author;        String title;        public Book(String author, String title) {            super();            this.author = author;            this.title = title;        }                        @Override        public String toString() {            return author+":"+title;        }    }        static int n = 5;    static ArrayList<Book> shelf = new ArrayList<Book>();    static ArrayList<Book> pile = new ArrayList<Book>();    static ArrayList<Book> borrow = new ArrayList<Book>();        public static void main(String[] args) {        shelf.add(new Book("adfas","sadasds"));        shelf.add(new Book("weraw","ngfh"));        shelf.add(new Book("weraw","nghh"));        shelf.add(new Book("werew","ngfh"));        shelf.add(new Book("gfdsa","bxcvxc"));        shelf.add(new Book("weweq","qqqe"));        shelf.add(new Book("poii","wewr"));                        Scanner scanner = new Scanner(System.in);        String com = null;        Random r = new Random();                Comparator<Book> comparator = new Comparator<Book>() {            @Override            public int compare(Book o1, Book o2) {                if(o1.author.compareToIgnoreCase(o2.author)>0){                    return 1;                }else if((o1.author.compareToIgnoreCase(o2.author)<0)){                    return -1;                }else{                    if((o1.title.compareToIgnoreCase(o2.title)>0)){                        return 1;                    }else if(o1.title.compareToIgnoreCase(o2.title)<0){                        return -1;                    }                }                                return 0;            }        };                Collections.sort(shelf,comparator);        System.out.println("书架:"+shelf);        System.out.println("借出:"+borrow);        System.out.println("归还:"+pile);                while(!"END".equals(com=scanner.nextLine())){            if(com.startsWith("B")){                if(shelf.size()>0){                    borrow.add(shelf.remove((r.nextInt(shelf.size()))));                    System.out.println("书架:"+shelf);                    System.out.println("借出:"+borrow);                    System.out.println("归还:"+pile);                }            }else if(com.startsWith("R")){                if(borrow.size()>0){                    pile.add(borrow.remove((r.nextInt(borrow.size()))));                    System.out.println("书架:"+shelf);                    System.out.println("借出"+borrow);                    System.out.println("归还"+pile);                }            }else if(com.startsWith("S")){                if(pile.size()>0){                    Collections.sort(pile,comparator);                    int i=0;                    for(;i<shelf.size();i++){                                            if(pile.get(0).author.compareTo(shelf.get(i).author)<0 &&                                pile.get(0).title.compareTo(shelf.get(i).title)<0){                            shelf.addAll(i, pile);                            break;                        }                    }                    if(i==shelf.size()){                        shelf.addAll(pile);                    }                    pile.clear();                        System.out.println("书架:"+shelf);                    System.out.println("借出:"+borrow);                    System.out.println("归还:"+pile);                }            }        }        scanner.close();    }          }


0 0
原创粉丝点击