package rentbook; import java.util.Scanner; public class Rent { public static void main(String[] args) { System.out.println("***欢迎来到租书系统,请根据有效提示进行选择!***"); System.out.println("1-按照名称查找图书;2-按照序号查找图书"); Scanner input = new Scanner(System.in); Book[] book = { new Book(1, "数据结构"), new Book(2, "高数"), new Book(3, "算法"), new Book(4, "论语") }; int x = input.nextInt(); if (x == 1) { System.out.println("输入图书名称:"); String y = input.next(); for (int i = 0; i < book.length; i++) { Book book1 = (Book) book[i]; try { if (y.equals(book1.getBookName())) { System.out.println(y); } } catch (Exception e) { e.printStackTrace(); } } } else if (x == 2) { System.out.println("输入图书序号:"); int z = input.nextInt(); for (int i = 0; i < book.length; i++) { Book book1 = (Book) book[i]; if (z == (book1.getBookNum())) { System.out.println(book[book1.getBookNum()-1].getBookName()); } else{ System.out.println("图书不存在"); } } } else { System.out.println("command is wrong!"); } } }
package rentbook;
public class Book {
private String bookName;
private int bookNum;
public Book(int bookNum, String bookName) {
this.bookNum=bookNum;
this.bookName=bookName;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getBookNum() {
return bookNum;
}
public void setBookNum(int bookNum) {
this.bookNum = bookNum;
}
}
ziom
相关分类