[Java] BookList类用来存Book类,查找书 →→→→→进入此内容的聊天室

来自 , 2019-08-27, 写在 Java, 查看 106 次.
URL http://www.code666.cn/view/752d25a1
  1. import java.util.*;
  2. public class Main{
  3.        
  4.         public static void main(String[] args) {
  5.                 Scanner s = new Scanner(System.in);
  6.                 BookList bl = new BookList();
  7.                 int n = s.nextInt();
  8.                 for (int i=0; i<n;i++) {
  9.                         bl.addBook(new Book(s.next(),s.nextInt(),s.next(),s.nextInt()));
  10.                 }
  11.                 bl.searchBook(new Book(s.next(),0,s.next(),s.nextInt()));
  12.         }
  13. }
  14.  
  15. class Book{
  16.         String name;
  17.         int price;
  18.         String author;
  19.         int number;
  20.         public Book(String na, String au, int num) {
  21.                 name = na;
  22.                 author = au;
  23.                 number = num;
  24.         }
  25.         public Book(String na, int p, String au, int num) {
  26.                 name = na;
  27.                 price = p;
  28.                 author = au;
  29.                 number = num;
  30.         }
  31.         public String getName() {
  32.                 return name;
  33.         }
  34.         public String getAuthor() {
  35.                 return author;
  36.         }
  37.         public int getNumber() {
  38.                 return number;
  39.         }
  40. }
  41.  
  42. class BookList{
  43.         List<Book> L = new LinkedList<Book>();
  44.         public void addBook(Book b) {
  45.                 L.add(b);
  46.         }
  47.         public void searchBook(Book b) {
  48.                 int i, flag = 0;
  49.                 for(i = 0; i < L.size(); i++) {
  50.                         if(L.get(i).getName().equals(b.getName()) && L.get(i).getAuthor().equals(b.getAuthor()) && L.get(i).getNumber()==b.getNumber()) {
  51.                                 flag = 1;
  52.                                 System.out.println("found: " +i);
  53.                         }
  54.                 }
  55.                 if(flag == 0)
  56.                         System.out.println("not found");
  57.         }
  58. }

回复 "BookList类用来存Book类,查找书"

这儿你可以回复上面这条便签

captcha