Java---Design [Book Management System]

library management system

Design requirements

Design a library management system to achieve the following functions:

(1) Enter the book number, book title and price of 5 books;
(2) Calculate the total price and average price of the books, and display the books with the highest and lowest prices;
(3) Sort by price in descending order;
(4) Calculate higher than The average price and the number of books below the average price;
(5) Enter the book number to query the price and title of the book.

full code

package work.chengxu;
import java.util.*;
public class day1 {
    
       
    
    public static void main(String[] args) {
    
       
    
        // TODO Auto-generated method stub
        @SuppressWarnings("resource")
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入第一本书名、书号、价格(输入时用*隔开,例:红楼梦*1001*100):");
        String str1=sc.next();
        Book book1= day1.ReturnBook(str1);
        System.out.println("请输入第二本书名、书号、价格(输入时用*隔开,例:红楼梦*1001*100):");
        String str2=sc.next();
        Book book2= day1.ReturnBook(str2);
        System.out.println("请输入第三本书名、书号、价格(输入时用*隔开,例:红楼梦*1001*100):");
        String str3=sc.next();
        Book book3= day1.ReturnBook(str3);
        System.out.println("请输入第四本书名、书号、价格(输入时用*隔开,例:红楼梦*1001*100):");
        String str4=sc.next();
        Book book4= day1.ReturnBook(str4);
        System.out.println("请输入第五本书名、书号、价格(输入时用*隔开,例:红楼梦*1001*100):");
        String str5=sc.next();
        Book book5= day1.ReturnBook(str5);
        //增加元素到链表books
        List<Book> books=new ArrayList<Book>();
        books.add(book1);
        books.add(book2);
        books.add(book3);
        books.add(book4);
        books.add(book5);
        //准备输出 System.out.println("----------------------------------------------");
        double sumprice=book1.getPrice()+book2.getPrice()+book3.getPrice()+book4.getPrice()+book5.getPrice();
        System.out.println("五本书的总价格为:"+sumprice);
        double avgprice=sumprice/5;
        System.out.println("五本书的平均价格为:"+avgprice);
        List<Double> prices=new ArrayList<Double>();//增加元素到prices对象中
        prices.add(book1.getPrice());
        prices.add(book2.getPrice());
        prices.add(book3.getPrice());
        prices.add(book4.getPrice());
        prices.add(book5.getPrice());
        Collections.sort(prices);//按升序排序
        List<String> Name=new ArrayList<String>();//增加对象到Name对象中
        Name.add(book1.getName());
        Name.add(book2.getName());
        Name.add(book3.getName());
        Name.add(book4.getName());
        Name.add(book5.getName());
        //输出最高,最低价格
        System.out.println("价格最高的为:"+Name.get(4)+prices.get(4));
        System.out.println("价格最低的为:"+Name.get(0)+prices.get(0));
        //按价格降序输出 (反向输出)
        Integer[] arr = {
    
       
    (int) book1.getPrice(), (int) book2.getPrice(), (int) book3.getPrice(), (int) book4.getPrice(), (int) book5.getPrice()};
        Arrays.sort(arr, Collections.reverseOrder());//利用Collections的reverseOrder方法
        System.out.println("价格降序为");
        for (Integer x : arr) {
    
       
    
            System.out.print(x + " ");
        }
        //用for循环进行价格比较
        List<Book> books2=new ArrayList<Book>();
        List<Book> books3=new ArrayList<Book>();
        for(Book b:books){
    
       
    
            if(b.getPrice()>avgprice){
    
       
    
                books2.add(b);
            }else if(b.getPrice()<avgprice){
    
       
    
                books3.add(b);
            }
        }
        System.out.println("\n高于平均价格的书有:"+ day1.toShow(books2));
        System.out.println("低于平均价格的书有:"+ day1.toShow(books3));
        System.out.println("----------------------------------------------");
        //查询
        while(true){
    
       
    
            System.out.println("请输入书号查询书籍(输入0退出):");
            int bookId=sc.nextInt();
            if(bookId==0){
    
       
    
                break;
            }
            else{
    
       
    
                Book book=new Book();
                for(Book b:books){
    
       
    
                    if(bookId==b.getBookId()){
    
       
    
                        book=b;
                    }
                }
                System.out.println("当前书籍为:书名    "+book.getName()+",书号   "+book.getBookId()+",价格   "+book.getPrice());
            }
        }
    }
    @SuppressWarnings("unused")
    private static void println(String string)//静态方法,同主类(调用快捷方便)
    {
    
       
    
        // TODO Auto-generated method stub

    }
    @SuppressWarnings("unused")
    private static double prices() {
    
       
    
        // TODO Auto-generated method stub
        return 0;
    }
    //存入初始输入
    public static Book ReturnBook(String str){
    
       
    
        Book book=new Book(str.substring(0, str.indexOf("*")),
                str.substring(str.indexOf("*")+1,str.lastIndexOf("*")),
                str.substring(str.lastIndexOf("*")+1,str.length()));
        return book;
    }
    //输出高于,低于平均价格的书
    public static String toShow(List<Book> books){
    
       
    
        String str="";
        for(Book b:books){
    
       
    
            str+=b.getName()+"  ";
        }
        return str;
    }
}

class Book{
    
       
    
    //创建数据成员
    private String name;
    private double price;
    private int bookId;
    public Book(){
    
       
    }
    public Book(String name,String bookId,String price)//调用数据成员
    {
    
       
    
        this.name=name;
        this.price=Double.parseDouble(price);
        this.bookId=Integer.parseInt(bookId);
    }
    public String getName() {
    
       
    
        return name;
    }
    public void setName(String name) //构造方法
    {
    
       
    
        this.name = name;
    }
    public double getPrice() {
    
       
    
        return price;
    }
    public void setPrice(double price) {
    
       
    
        this.price = price;
    }
    public int getBookId() {
    
       
    
        return bookId;
    }
    public void setBookId(int bookId) {
    
       
    
        this.bookId = bookId;
    }
}

operation result

insert image description here

Later, everyone can enter the corresponding book title according to their own needs, etc., to realize the functions of this system.

Related Posts