为了账号安全,请及时绑定邮箱和手机立即绑定

【作业】添书和借书系统

/**
*额外加了个可以添加图书的功能,
*不过有exception之后用户要重新输入所有内容,有点累赘。
*/
package com.imooc.librarysystem;

import com.imooc.librarysystem.exception.BookExistException;
import com.imooc.librarysystem.exception.BookNotExistException;
import com.imooc.librarysystem.exception.WrongCommandException;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Application {
    public static List<Book> bookList = new ArrayList<Book>();

    public List<String> addBookInfo(Scanner scanner){
        List<String> info = new ArrayList<String>();
        System.out.print("Please insert the book name: ");
        String name = scanner.next();
        System.out.print("Please insert the book code: ");
        String code = scanner.next();
        info.add(name);
        info.add(code);
        return info;
    }

    public List<String> searchBookInfo(Scanner scanner) throws WrongCommandException {
        System.out.print("Search by 1==>Book name; 2==> Book code : ");
        String command = scanner.next();
        List<String> info = new ArrayList<String>();
        switch (command){
            case "1":
                System.out.print("Please insert the book name: ");
                info.add(command);
                info.add(scanner.next());
                break;
            case "2":
                System.out.print("Please insert the book code: ");
                info.add(command);
                info.add(scanner.next());
                break;
            default:
                throw new WrongCommandException("You have inserted a wrong command.");
        }
        return info;
    }

    public Book addBook(String name, String code) throws BookExistException, WrongCommandException{
        for(Book book:bookList){
            if(book.getName().equals(name)){
                throw new BookExistException("Already exist a book named: "+name);
            }else if(book.getCode().equals(code)){
                throw new BookExistException("The book code is already used.");
            }
        }
        try {
            Integer.parseInt(code);
            Book book = new Book();
            book.setName(name);
            book.setCode(code);
            return book;
        } catch(NumberFormatException e){
            throw new WrongCommandException("Book code should be numerical.");
        }
    }

    public Book searchBookByName(String name) throws BookNotExistException{
        for(Book book:bookList){
            if(book.getName().equals(name)){
                return book;
            }
        }
        throw new BookNotExistException("There is no book named: "+name);
    }

    public Book searchBookByCode(String code) throws BookNotExistException,WrongCommandException{
        try{
            Integer.parseInt(code);
            for(Book book:bookList){
                if(book.getCode().equals(code)){
                    return book;
                }
            }
            throw new BookNotExistException("There is no book has code "+code);
        }catch (NumberFormatException e){
            throw new WrongCommandException("Book code should be numerical.");
        }
    }

    public static void main(String[] args) {
        Application app = new Application();
        Scanner scanner = new Scanner(System.in);
        Boolean quit = false;
        Book book;
        while(!quit){
            System.out.print("Press 1==>Add book; 2==>Search book ; 0==>Quit :");
            String choice = scanner.next();
            switch (choice) {
                case "1":
                    List<String> info = app.addBookInfo(scanner);
                    try {
                        book = app.addBook(info.get(0), info.get(1));
                        bookList.add(book);
                        System.out.println("You have added a book: " + info.get(0));
                    } catch (BookExistException e) {
                        System.out.println(e.getMessage());
                        System.out.println("Please reinsert a book info.");
                    } catch (WrongCommandException e){
                        System.out.println(e.getMessage());
                        System.out.println("Please reinsert a book info.");
                    } catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case "2":
                    try {
                        List<String> info2 = app.searchBookInfo(scanner);
                        if (info2.get(0).equals("1")){
                            book = app.searchBookByName(info2.get(1));
                        }else{
                            book = app.searchBookByCode(info2.get(1));
                        }
                        System.out.println("Congratulations! Found book :"+book.getName());
                    } catch (WrongCommandException e) {
                        System.out.println(e.getMessage());
                        System.out.println("Please insert a correct command.");
                    } catch (BookNotExistException e){
                        System.out.println(e.getMessage());
                        System.out.println("Sorry.");
                    } catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case "0":
                    System.out.println("Thank you for using the book system.");
                    quit = true;
                    break;
                default:
                    System.out.println("You have inserted a wrong command.");
                    break;
            }
        }
    }
}
package com.imooc.librarysystem.exception;

public class BookExistException extends Exception{
    public BookExistException(String message) {
        super(message);
    }
}
package com.imooc.librarysystem.exception;

public class BookNotExistException extends Exception{
    public BookNotExistException(String message) {
        super(message);
    }
}
package com.imooc.librarysystem.exception;

public class WrongCommandException extends Exception{

    public WrongCommandException(String message) {
        super(message);
    }
}
package com.imooc.librarysystem;

public class Book {
    private String name;
    private String code;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}


正在回答

1 回答

这个包的说明有吗

0 回复 有任何疑惑可以回复我~

你这是要谁给你做了吗

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409792    人
  • 解答问题       4340    个

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

进入课程

【作业】添书和借书系统

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信