第九周

1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询

package Book;

public class Book {
     private int id;
     private String name;
     private double price;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public Book(int id, String name, double price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }
    public Book() {
        super();
    }
    
}
package Book;

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

public class main {

    static List<Book> list = new ArrayList<Book>();
    static Scanner sc=new Scanner(System.in);
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
            Book book1 = new Book(1,"唐诗三百首",30);
            Book book2 = new Book(2,"中华上下五千年",45);
            Book book3 = new Book(3,"唐诗宋词",29.9);
            list.add(book1);
            list.add(book2);
            list.add(book3);
            show();

    }
    
     public static void show() {
            System.out.println("请选择你需要的操作");
            System.out.println("1.添加图书");
            System.out.println("2.查询图书");
            System.out.println("3.修改图书");
            System.out.println("4.删除图书");
            System.out.println("============================");
            main();
        }

     public static void main() {
            int i=sc.nextInt();
            switch (i) {
            case 1:add();
                break;
            case 2:select();
                break;
            case 3:updata();
                break;
            case 4:del();
                break;
            default:
                break;
            }
        }

    private static void del() {
        // TODO Auto-generated method stub
        System.out.println("请输入图书id");
        int id=sc.nextInt();
            for (int i = 0; i < list.size(); i++) {
                if (id == list.get(i).getId()) {
                    list.remove(i);
                    System.out.println("删除成功");
                    }
            }
            for (int i = 0; i < list.size(); i++) {
                Book b1=list.get(i);
                System.out.println("id为"+b1.getId()+":"+b1.getName()+"¥价格为"+b1.getPrice());
            }
            System.out.println("-------------------------------");
            show();
    }

    private static void updata() {
        // TODO Auto-generated method stub
         System.out.println("请输入图书id");
            int id=sc.nextInt();
            
                for (int i = 0; i < list.size(); i++) {
                    if (id == list.get(i).getId()) {
                        list.remove(i);
                        System.out.println("请输入图书name");
                        String name=sc.next();
                        System.out.println("请输入图书price");
                        int price=sc.nextInt();
                        Book b=new Book(id,"name",price);
                        b.setId(id);
                        b.setName(name);
                        b.setPrice(price);
                        list.add(b);
                        System.out.print("操作成功为");
                    
                    }
                }
                for (int i = 0; i < list.size(); i++) {
                    Book b1=list.get(i);
                    System.out.println("id为"+b1.getId()+":"+b1.getName()+"¥价格为"+b1.getPrice());
                }
                System.out.println("-------------------------------");
                show();
    }

    private static void select() {
        // TODO Auto-generated method stub
         System.out.println("请输入图书id");
         int id=sc.nextInt();
             for (int i = 0; i < list.size(); i++) {
                 if (id == list.get(i).getId()) {
                         System.out.println("查询结果为");
                         Book b1=list.get(i);
                         System.out.println("id为"+b1.getId()+"\n"+b1.getName()+"   ¥价格为"+b1.getPrice());                                                    
                     }
             }
             System.out.println("-------------------------------");
             show();
        
    }

    private static void add() {
        // TODO Auto-generated method stub
        System.out.println("请输入图书id");
        int id=sc.nextInt();
        System.out.println("请输入图书name");
        String name=sc.next();
        System.out.println("请输入图书price");
        double price=sc.nextDouble();
        Book b=new Book(id,"name",price);
        b.setId(id);
        b.setName(name);
        b.setPrice(price);
        list.add(b);
        for (int i = 0; i < list.size(); i++) {
            Book b1=list.get(i);
            System.out.println("id为"+b1.getId()+":"+b1.getName()+"¥价格为"+b1.getPrice());
        }
        System.out.println("-------------------------------");
        show();
    }

}

 

 

 2.上题的类,在一个JSP页面中,创建一个集合,里面放3个图书,集合循环遍历显示在页面上。

<%@page import="Book.Book"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
  </head>
  <body>
      <%
        List<Book> list = new ArrayList<Book>();
        Book book = new Book();
        Book book1 = new Book(1, "liu", 444);
        Book book2 = new Book(2, "de", 555);
        Book book3 = new Book(3, "pu", 666);
        list.add(book1);
        list.add(book2);
        list.add(book3);
        for(int i=0;list!=null&&i<list.size();i++){
        out.print(list.get(i).getId()+" "+list.get(i).getName()+" "+list.get(i).getPrice()+"<br>");
        }
      %>
  </body>
</html>

  

 

3.在MySQL中创建Book表,里面id,name,price,
用命令实现,
添加一个图书,
根据名称删除图书,
把所有名称是“我”开头的图书删除,
删除全部图书,
把20元以上的图书价格都修改为18.8,
查看全部图书,
查看价格高于10块钱的全部图书

 

 

 

posted @ 2022-05-01 15:40  刘德璞  阅读(32)  评论(0编辑  收藏  举报