9

package po;
 
public class Book {
    private int bid;
    private String bname;
    private double bprice;
 
    public Book() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    public Book(int bid, String bname, double bprice) {
        super();
        this.bid = bid;
        this.bname = bname;
        this.bprice = bprice;
    }
 
    public int getBid() {
        return bid;
    }
 
    public void setBid(int bid) {
        this.bid = bid;
    }
 
    public String getBname() {
        return bname;
    }
 
    public void setBname(String bname) {
        this.bname = bname;
    }
 
    public double getBprice() {
        return bprice;
    }
 
    public void setBprice(double bprice) {
        this.bprice = bprice;
    }
 
    @Override
    public String toString() {
        return "Book [bid=" + bid + ", bname=" + bname + ", bprice=" + bprice + "]";
    }
 
}
package po;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
public class Lib {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        List<Book> blist = new ArrayList<>();
        Book b1 = new Book();
        b1.setBid(1);
        b1.setBname("三国");
        b1.setBprice(55);
        blist.add(b1);
        Book b2 = new Book();
        b2.setBid(2);
        b2.setBname("水浒");
        b2.setBprice(45);
        blist.add(b2);
        Book b3 = new Book();
        b3.setBid(3);
        b3.setBname("西游");
        b3.setBprice(35);
        blist.add(b3);
        w: while (true) {
            System.out.println("1.增加2.删除3.修改4.查询");
            System.out.println("输入您的选择:");
            int x = sc.nextInt();
            int i;
            String n;
            double p;
            switch (x) {
            case 1:
                System.out.println("输入Book的id:");
                i = sc.nextInt();
                System.out.println("输入Book的name:");
                n = sc.next();
                System.out.println("输入Book的price:");
                p = sc.nextDouble();
                add(i, n, p, blist);
                break;
            case 2:
                System.out.println("输入Book的id:");
                i = sc.nextInt();
                delete(i, blist);
                break;
            case 3:
                System.out.println("输入Book的id:");
                i = sc.nextInt();
                System.out.println("输入Book的price:");
                p = sc.nextDouble();
                update(i, p, blist);
                break;
            case 4:
                System.out.println("是否查询所有:(y/n)");
                String pd = sc.next();
                if (pd.equals("y")) {
                    for (Book book : blist) {
                        System.out.println(book.toString());
                    }
                } else {
                    System.out.println("输入Book的name:");
                    n = sc.next();
                    List<Book> blist1 = select(n, blist);
                    for (Book book : blist1) {
                        System.out.println(book.toString());
                    }
                }
 
                break;
            default:
 
                break w;
            }
        }
    }
 
    public static void add(int i, String n, double p, List<Book> blist) {
        Book b1 = new Book();
        b1.setBid(i);
        b1.setBname(n);
        b1.setBprice(p);
        blist.add(b1);
    }
 
    public static void delete(int i, List<Book> blist) {
        for (Book b : blist) {
            if (b.getBid() == i) {
                blist.remove(b);
            }
        }
    }
 
    public static void update(int i, double p, List<Book> blist) {
        for (Book b : blist) {
            if (b.getBid() == i) {
                b.setBprice(p);
            }
        }
    }
 
    public static List<Book> select(String n, List<Book> blist) {
        List<Book> blist1 = new ArrayList<>();
        for (Book b : blist) {
            if (b.getBname().equals(n)) {
                blist1.add(b);
            }
        }
        return blist1;
    }
}

posted @ 2022-05-01 15:09  咸鱼大佬  阅读(13)  评论(0编辑  收藏  举报