Day12

在线图书网站练习

commons包cart类

public class Cart {
    private HashMap<String, CartItem>  items = new HashMap<String, CartItem>();
    
    private int totalQuantity;
    
    private float amount; 

    public HashMap<String, CartItem> getItems() {
        return items;
    }

    public void setItems(HashMap<String, CartItem> items) {
        this.items = items;
    }

    public int getTotalQuantity() {
        totalQuantity = 0;
        // for (List  list : lists)
        for(Map.Entry<String, CartItem> item:items.entrySet()){
            totalQuantity += item.getValue().getQuantity();
        }
        return totalQuantity;
    }

    public void setTotalQuantity(int totalQuantity) {
        this.totalQuantity = totalQuantity;
    }

    public float getAmount() {
        amount = 0;
        for(Map.Entry<String, CartItem> item:items.entrySet()){
            amount += item.getValue().getTotalPrice();
        }
        return amount;
    }

    public void setAmount(float amount) {
        this.amount = amount;
    }
    
    public void addBook(Book book){
        if(items.containsKey(book.getId())){// 图书已存在
            CartItem item = items.get(book.getId());/
            item.setQuantity(item.getQuantity()+1);
        }else{// 图书不存在
                CartItem item = new CartItem(book);
                item.setQuantity(1);
                items.put(book.getId(), item);
        }
    }
}
View Code

CartItem类

public class CartItem {

    private Book book;
    
    private int quantity;
    
    private float totalPrice;
    

    public CartItem(Book book) {
        this.book = book;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public float getTotalPrice() {
        return book.getPrice()*quantity;
    }

    public void setTotalPrice(float totalPrice) {
        this.totalPrice = totalPrice;
    }
    
}
View Code

MyFunction类

public class MyFunction {
    
    public static BusinessService service = new BusinessServiceImpl();
        
        public static String showCategoryName(String categoryId){
    
            Category category = service.findCategoryById(categoryId);
    
                if(category != null){
                    return category.getName();
                }
                return null;
            }
        
    }
View Code

Page类

public class Page {
    private List records;
    
    private int pageSize = 3;

    private int currentPageNum;
    
    private int totalPage;
    
    private int prePageNum;

    private int nextPageNum;

    private int startIndex;
    
    private int totalRecords;
    
    private String url;
    
                5                    7
    public Page(int currentPagenum, int totalRecords) {
        //            7            7
        this.totalRecords = totalRecords;
        //            5                5
        this.currentPageNum = currentPagenum;
    
        totalPage = totalRecords%pageSize  ==0?totalRecords/pageSize    :   totalRecords/pageSize+1;
        
        
        startIndex = (currentPageNum-1)*pageSize;
        
        
    }
    
    public List getRecords() {
        return records;
    }
    public void setRecords(List records) {
        this.records = records;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getCurrentPageNum() {
        return currentPageNum;
    }
    public void setCurrentPageNum(int currentPageNum) {
        this.currentPageNum = currentPageNum;
    }
    public int getTotalPage() {
        return totalPage;
    }
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public int getPrePageNum() {
        //上一页 =  当前页码 -1  要求大于0 才拥有上一页,否则则1 
        prePageNum = currentPageNum - 1>0?currentPageNum - 1:1;
        return prePageNum;
    }
    public void setPrePageNum(int prePageNum) {
        
        
        this.prePageNum = prePageNum;
    }
    public int getNextPageNum() {
        //下一页 =  当前页码 + 1  如果大于总页数   
        nextPageNum = currentPageNum + 1 >totalPage?totalPage : currentPageNum + 1;
        return nextPageNum;
    }
    public void setNextPageNum(int nextPageNum) {
        
        
        this.nextPageNum = nextPageNum;
    }
    public int getStartIndex() {
        return startIndex;
    }
    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }
    public int getTotalRecords() {
        return totalRecords;
    }
    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public String toString() {
        return "Page [records=" + records + ", pageSize=" + pageSize
                + ", currentPageNum=" + currentPageNum + ", totalPage="
                + totalPage + ", prePageNum=" + prePageNum + ", nextPageNum="
                + nextPageNum + ", startIndex=" + startIndex
                + ", totalRecords=" + totalRecords + ", url=" + url + "]";
    }
    
    
}
View Code

entity包Book类

public class Book {
    
    
    

    private String id; //  图书编号
    private String name;// 图书名称
    private String author;// 作者
    private String description;// 图书描述
    private String publish;// 图书的出版社
    private float  price;// 图书的单价
    // 图片 的显示 = 图片的路径                         + 图片的名称
    // 示例:E:\20181119-201811-28\资料\图解\2018-11-21.png
    // 图片的路径
    private String path;
    // 图片的名称
    private String photoName;
    // 分类的id
    private String categoryId;
    
//    private Category categoryIds;//这是面向对象的写法,日后这么写
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getPublish() {
        return publish;
    }
    public void setPublish(String publish) {
        this.publish = publish;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    public String getPath() {
        return path;
    }
    public void setPath(String path) {
        this.path = path;
    }
    public String getPhotoName() {
        return photoName;
    }
    public void setPhotoName(String photoName) {
        this.photoName = photoName;
    }
    public String getCategoryId() {
        return categoryId;
    }
    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }
    @Override
    public String toString() {
        return "图书详细:"+"<br/><br/>"+"图书名称=" + name +"<br/><br/>"+ ", 图书作者=" + author
                + "<br/><br/>"+", 图书描述=" + description +"<br/><br/>"+ ", 图书出版社=" + publish
                + "<br/><br/>"+", 图书售价=" + price ;
    }
    
}
View Code

Category类

public class Category implements Serializable {
    
    // 字符串类型的分类编号
    private String id;
    
    //字符串类型的分类名称
    private String name;
    
    //字符串类型的分类描述
    private String description;

    
    
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "Category [id=" + id + ", name=" + name + ", description="
                + description + "]";
    }

}
View Code

User类

public class User {

    private  String id;
    
    private  String username;
    
    private  String password ;

    // 用户角色问题: 0是普通用户、 1是管理员
    private String type;
    

    private  String sex;

//     alt+shift+s  -- r  生成了getter 和setter 方法
    
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password="
                + password + ", type=" + type + ", sex=" + sex + "]";
    }
    
}
View Code

 

posted @ 2022-01-16 19:41  灰幕  阅读(26)  评论(0)    收藏  举报