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

自己写的,仅供参考!

package com.xixi.book;


import java.util.InputMismatchException;

import java.util.Scanner;


import com.xixi.book.beans.Books;

import com.xixi.book.dao.BookShow;

import com.xixi.book.dao.BooksDao;

import com.xixi.book.exception.BookException;


public class Main_book {

public static void main(String[] args) {

try {

new Main_book().show();

} catch (BookException e) {

e.printStackTrace();

}

}

private static final BooksDao booksdao = new BooksDao();

public static void show() throws BookException{

try {

String name = "";

int id;

Scanner scan = new Scanner(System.in);

BookShow show = new BookShow();

System.out.println("输入命令: 1 -- 按照名称查找图书;2 -- 按照序号查找图书");

int count = scan.nextInt();

if(count == 1) {

System.out.println("输入图书名称:");

scan=new Scanner(System.in);

name = scan.nextLine();

show.getNameShow(name);

}else if(count == 2) {

System.out.println("输入图书ID:");

id = scan.nextInt();

show.getIdShow(id);

}else {

System.out.println("输入错误!");

show();

}

} catch (InputMismatchException e) {

System.out.println("命令输入错误!请根据提示输入数字命令");

show();

}catch (Exception e) {

show();

}

}

}




package com.xixi.book.beans;


public class Books {

private int id;

private String BookName;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getBookName() {

return BookName;

}

public void setBookName(String bookName) {

BookName = bookName;

}

@Override

public String toString() {

return "序号:\t" + id + ", 书籍名称:\t" + BookName + "";

}

}




package com.xixi.book.dao;


import java.util.Vector;


import com.xixi.book.beans.Books;


public class BooksDao {

public BooksDao() {

initialization();

}

public Vector<Books> getAllBook(){

return books;

}

Vector<Books> books = new Vector<>();

public void initialization() {

Books books = new Books();

books.setId(1);

books.setBookName("数据结构");

this.books.add(books);

books = new Books();

books.setId(2);

books.setBookName("高数");

this.books.add(books);

books = new Books();

books.setId(3);

books.setBookName("Java程序设计");

this.books.add(books);

books = new Books();

books.setId(4);

books.setBookName("软件工程与UML");

this.books.add(books);

books = new Books();

books.setId(5);

books.setBookName("Web前端开发");

this.books.add(books);

}

}





package com.xixi.book.dao;


import java.util.Scanner;

import java.util.Vector;


import com.xixi.book.Main_book;

import com.xixi.book.beans.Books;

import com.xixi.book.exception.BookException;


public class BookShow {


Scanner scan = new Scanner(System.in);


private static final BooksDao booksdao = new BooksDao();


public void getNameShow(String name) throws BookException {


Vector<Books> books = booksdao.getAllBook();


// for (int i = 0; i < books.size(); i++) {

// if(books.get(i).equals(name)) {

// System.out.println(books.get(i));

// return;

// }else if(i==books.size()-1)

// System.out.println("名为:"+name+"的图书不存在");

// }


boolean noHave=true;

for(Books book:books) {

if(book.getBookName().equals(name)) {

System.out.println(book);

noHave=false;

break;

}

}


if(noHave) {

System.out.println("名为:"+name+"的图书不存在");

new Main_book().show();

}

}


public void getIdShow(int id) {


Vector<Books> books = booksdao.getAllBook();

boolean noHave = true;

for(Books book : books) {

if(book.getId() == id) {

System.out.println(book);

noHave = false;

break;

}

}

if(noHave)

System.out.println("序号为:"+id+",不存在!");

}


}






package com.xixi.book.exception;


public class BookException extends Exception{

public BookException() {

}

public BookException(String name) {

super(name);

}

}



https://img1.sycdn.imooc.com//5d0b4883000197ba03100270.jpg

正在回答

2 回答

大佬啊

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

运行结果:

https://img1.sycdn.imooc.com//5d0b48d60001677e05150414.jpg

https://img1.sycdn.imooc.com//5d0b48f30001f8bb04940386.jpg

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

举报

0/150
提交
取消

自己写的,仅供参考!

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