JAVA2、使用对象模拟“商店客户折扣卡”功能 编程实现一个模拟的用户消费信用卡的示例,在其中用户Customer类具有用户名、地址、卡号、消费额度、可以享受的打折度等多种个人信息; 编写CustomerDemo测试类,在类中建立3个消费者,对其分别进行操作
时间: 2024-04-17 08:28:28 浏览: 27
你可以使用以下Java代码来实现商店客户折扣卡的功能:
```java
public class Customer {
private String username;
private String address;
private int cardNumber;
private double creditLimit;
private double discountRate;
public Customer(String username, String address, int cardNumber, double creditLimit, double discountRate) {
this.username = username;
this.address = address;
this.cardNumber = cardNumber;
this.creditLimit = creditLimit;
this.discountRate = discountRate;
}
public String getUsername() {
return username;
}
public String getAddress() {
return address;
}
public int getCardNumber() {
return cardNumber;
}
public double getCreditLimit() {
return creditLimit;
}
public double getDiscountRate() {
return discountRate;
}
public void setCreditLimit(double creditLimit) {
this.creditLimit = creditLimit;
}
public void setDiscountRate(double discountRate) {
this.discountRate = discountRate;
}
}
public class CustomerDemo {
public static void main(String[] args) {
Customer customer1 = new Customer("John", "123 Main St", 123456, 1000.0, 0.1);
Customer customer2 = new Customer("Jane", "456 Oak St", 654321, 2000.0, 0.2);
Customer customer3 = new Customer("Bob", "789 Elm St", 987654, 3000.0, 0.3);
// 对customer1进行操作
System.out.println("Username: " + customer1.getUsername());
System.out.println("Address: " + customer1.getAddress());
System.out.println("Card Number: " + customer1.getCardNumber());
System.out.println("Credit Limit: " + customer1.getCreditLimit());
System.out.println("Discount Rate: " + customer1.getDiscountRate());
customer1.setCreditLimit(1500.0);
customer1.setDiscountRate(0.15);
System.out.println("Updated Credit Limit: " + customer1.getCreditLimit());
System.out.println("Updated Discount Rate: " + customer1.getDiscountRate());
// 对customer2进行操作
// ...
// 对customer3进行操作
// ...
}
}
```
在这个示例中,`Customer`类具有用户名、地址、卡号、消费额度和打折度等个人信息的属性。`Customer`类包含构造方法和相应的getter和setter方法来访问和修改这些属性。在`CustomerDemo`测试类中,我们创建了3个`Customer`对象,并对每个对象进行操作。你可以根据需要修改和添加其他操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)