如何在Java的Customer类中实现一个HashMap来存储Ticket对象?
时间: 2024-11-28 21:28:18 浏览: 17
为了在Java的`Customer`类中使用`HashMap`来存储`Ticket`对象,你可以按照以下步骤进行:
### 1. 导入必要的包
首先,确保导入`java.util.HashMap`和`java.util.Map`包。
```java
import java.util.HashMap;
import java.util.Map;
```
### 2. 定义`Customer`类
在`Customer`类中定义一个`HashMap`来存储每种票的数量。
```java
public class Customer {
private String firstName;
private String surname;
private Map<Ticket, Integer> tickets;
// 构造方法
public Customer(String firstName, String surname) {
this.firstName = firstName;
this.surname = surname;
this.tickets = new HashMap<>();
}
// 获取客户的名字
public String getFirstName() {
return firstName;
}
// 设置客户的名字
public void setFirstName(String firstName) {
this.firstName = firstName;
}
// 获取客户的姓氏
public String getSurname() {
return surname;
}
// 设置客户的姓氏
public void setSurname(String surname) {
this.surname = surname;
}
// 添加票
public void addTicket(Ticket ticket, int quantity) {
if (tickets.containsKey(ticket)) {
int currentQuantity = tickets.get(ticket);
tickets.put(ticket, currentQuantity + quantity);
} else {
tickets.put(ticket, quantity);
}
}
// 移除票
public boolean removeTicket(Ticket ticket, int quantity) {
if (tickets.containsKey(ticket)) {
int currentQuantity = tickets.get(ticket);
if (currentQuantity >= quantity) {
if (currentQuantity == quantity) {
tickets.remove(ticket);
} else {
tickets.put(ticket, currentQuantity - quantity);
}
return true;
}
}
return false;
}
// 获取所有票的信息
public Map<Ticket, Integer> getTickets() {
return tickets;
}
// 计算总成本(包括折扣)
public double calculateTotalCost() {
double totalCost = 0.0;
int totalTickets = 0;
for (Map.Entry<Ticket, Integer> entry : tickets.entrySet()) {
Ticket ticket = entry.getKey();
int quantity = entry.getValue();
totalCost += ticket.getPrice() * quantity;
totalTickets += quantity;
}
// 应用折扣
if (totalTickets >= 26) {
totalCost *= 0.75; // 25% 折扣
} else if (totalTickets >= 11) {
totalCost *= 0.85; // 15% 折扣
} else if (totalTickets >= 6) {
totalCost *= 0.90; // 10% 折扣
}
return totalCost;
}
// 计算原始总成本(不包括折扣)
public double calculateOriginalTotalCost() {
double totalCost = 0.0;
for (Map.Entry<Ticket, Integer> entry : tickets.entrySet()) {
Ticket ticket = entry.getKey();
int quantity = entry.getValue();
totalCost += ticket.getPrice() * quantity;
}
return totalCost;
}
}
```
### 3. `Ticket`类的定义
假设你已经有一个`Ticket`类,它至少包含票价和名称属性。
```java
public class Ticket implements Comparable<Ticket> {
private String name;
private double price;
public Ticket(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
@Override
public int compareTo(Ticket other) {
return this.name.compareTo(other.name);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Ticket ticket = (Ticket) obj;
return name.equals(ticket.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
}
```
### 4. 使用示例
以下是如何在`MainProgram`类中使用`Customer`和`Ticket`类的示例。
```java
public class MainProgram {
public static void main(String[] args) {
// 创建一些票
Ticket ticket1 = new Ticket("Magpie Line", 5.59);
Ticket ticket2 = new Ticket("Cross City", 2.50);
// 创建一个客户
Customer customer = new Customer("Pam", "Royal");
// 添加票
customer.addTicket(ticket1, 5);
customer.addTicket(ticket2, 10);
// 显示客户信息
System.out.println("Customer: " + customer.getFirstName() + " " + customer.getSurname());
System.out.println("Original Total Cost: " + customer.calculateOriginalTotalCost());
System.out.println("Discounted Total Cost: " + customer.calculateTotalCost());
// 移除票
customer.removeTicket(ticket1, 2);
// 再次显示客户信息
System.out.println("After removing tickets:");
System.out.println("Original Total Cost: " + customer.calculateOriginalTotalCost());
System.out.println("Discounted Total Cost: " + customer.calculateTotalCost());
}
}
```
通过以上步骤,你可以在`Customer`类中使用`HashMap`来存储和管理不同类型的`Ticket`对象及其数量,并计算相关的费用和折扣。
阅读全文