Design a simple Book Seller System with strategy pattern. (25 points) (a)You don’t care all the functions of the system. (b)There are three kinds of client which include VIPMember(0.9*price), NormalMember(1*price), SuperMember(0.8*price). They have the different discount when they buy book from the system. (c)You must use strategy pattern when you design the system. (d)Firstly, you should give the UML class diagram with the strategy pattern. (5 points) (e)Secondly, you should write the code for each class you design in the UML. (15 points) (f)Thirdly, you should write a class named Test which include a main() method, we support there are three clients named Tom(VIPMember), John(NormalMember) and Mike(SuperMember). The class you write can sell a book to the three clients. We support the price of the book is 150.(5 points)用Java写

时间: 2024-03-03 07:52:47 浏览: 13
(a) Functions of the Book Seller System: - Add/remove/update books in the system - Sell books to different clients with different discounts - Generate sales report (b) Client types: - VIPMember: 10% discount (0.9*price) - NormalMember: no discount (1*price) - SuperMember: 20% discount (0.8*price) (c) Strategy pattern: We will use the strategy pattern to implement the different discount strategies for each client type. Each client type will have its own strategy implementation. (d) UML class diagram: ``` +---------------------------------+ | BookSeller | +---------------------------------+ | - bookList: List<Book> | +---------------------------------+ | + addBook(book: Book): void | | + removeBook(book: Book): void | | + updateBook(book: Book): void | | + sellBook(client: Client): void| | + generateSalesReport(): void | +---------------------------------+ | | | +--------------+ | Book | +--------------+ | - title: String | | - author: String| | - price: double | +--------------+ | | | +-----------------------+ | Client | +-----------------------+ | - name: String | | - membershipType: int | | - discountStrategy: DiscountStrategy | +-----------------------+ | | +---------------------------+ | DiscountStrategy | +---------------------------+ | + calculateDiscount(price: double): double | +---------------------------+ | +------------------------+ | VIPStrategy | +------------------------+ | + calculateDiscount(price: double): double | +------------------------+ | +----------------------------+ | NormalStrategy | +----------------------------+ | + calculateDiscount(price: double): double | +----------------------------+ | +--------------------------+ | SuperStrategy | +--------------------------+ | + calculateDiscount(price: double): double | +--------------------------+ ``` (e) Java code for each class: Book.java ```java public class Book { private String title; private String author; private double price; public Book(String title, String author, double price) { this.title = title; this.author = author; this.price = price; } // getters and setters public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } } ``` Client.java ```java public class Client { private String name; private int membershipType; private DiscountStrategy discountStrategy; public Client(String name, int membershipType) { this.name = name; this.membershipType = membershipType; if (membershipType == 0) { discountStrategy = new VIPStrategy(); } else if (membershipType == 1) { discountStrategy = new NormalStrategy(); } else if (membershipType == 2) { discountStrategy = new SuperStrategy(); } } public double calculateDiscount(double price) { return discountStrategy.calculateDiscount(price); } // getters and setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMembershipType() { return membershipType; } public void setMembershipType(int membershipType) { this.membershipType = membershipType; } public DiscountStrategy getDiscountStrategy() { return discountStrategy; } public void setDiscountStrategy(DiscountStrategy discountStrategy) { this.discountStrategy = discountStrategy; } } ``` DiscountStrategy.java ```java public interface DiscountStrategy { double calculateDiscount(double price); } ``` VIPStrategy.java ```java public class VIPStrategy implements DiscountStrategy { @Override public double calculateDiscount(double price) { return price * 0.9; } } ``` NormalStrategy.java ```java public class NormalStrategy implements DiscountStrategy { @Override public double calculateDiscount(double price) { return price; } } ``` SuperStrategy.java ```java public class SuperStrategy implements DiscountStrategy { @Override public double calculateDiscount(double price) { return price * 0.8; } } ``` BookSeller.java ```java import java.util.ArrayList; import java.util.List; public class BookSeller { private List<Book> bookList; public BookSeller() { bookList = new ArrayList<>(); } public void addBook(Book book) { bookList.add(book); } public void removeBook(Book book) { bookList.remove(book); } public void updateBook(Book book) { for (int i = 0; i < bookList.size(); i++) { if (bookList.get(i).getTitle().equals(book.getTitle())) { bookList.set(i, book); break; } } } public void sellBook(Client client) { double price = 150; double discount = client.calculateDiscount(price); double totalPrice = price - discount; System.out.println("Client: " + client.getName() + " | Book price: " + price + " | Discount: " + discount + " | Total price: " + totalPrice); } public void generateSalesReport() { // implementation omitted } } ``` (f) Test.java ```java public class Test { public static void main(String[] args) { BookSeller bookSeller = new BookSeller(); // Add books to the system bookSeller.addBook(new Book("Book 1", "Author 1", 100)); bookSeller.addBook(new Book("Book 2", "Author 2", 200)); bookSeller.addBook(new Book("Book 3", "Author 3", 300)); // Sell books to clients Client tom = new Client("Tom", 0); bookSeller.sellBook(tom); Client john = new Client("John", 1); bookSeller.sellBook(john); Client mike = new Client("Mike", 2); bookSeller.sellBook(mike); } } ```

相关推荐

最新推荐

recommend-type

DesignModeler实用技巧.docx

DesignModeler实用技巧 作为CAE人员,前处理将花费较大的时间。如果用熟练一些常用前处理技巧,将使模型的处理效率大大提升。
recommend-type

USB3300_Hi-Speed USB Host, Device or OTG PHY with ULPI Low Pin Interface.pdf

Microchip’s low latency high speed and full speed receiver provide the option of re-using existing UTMI Links with a simple wrapper to convert UTMI to ULPI. The ULPI interface allows the USB3300 PHY...
recommend-type

YT8511_reference_design_v1.1_20191018.pdf

裕泰 phy芯片参考设计。YT8511_reference_design_v1.1_20191018.pdf
recommend-type

AI accelerator design.pdf

韩国首尔大学教授 讲解的关于设计人工智能(AI)芯片的资料,关于人工智能加速器设计的资料。韩国汉阳大学IDEC课程课件。
recommend-type

CMOS VLSI Design A Circuits and Systems Perspective 奇数偶数题答案

CMOS VLSI Design A Circuits and Systems Perspective 奇数偶数题 答案
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。