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);
}
}
```