手机专卖店为了促销自己的产品,决定发行内部购物券,但其他商场不能发行该购物券。编写一个MobileShop类(模拟手机专卖店),该类中有一个名字为InnerPurchaseMoney的内部类(模拟内部购物券)。 现在会发放10000和20000两种购物券,10000购物券能购买2部手机,20000购物券能购买4部手机。
时间: 2024-02-25 19:58:28 浏览: 42
好的,这个问题很简单。以下是MobileShop类和InnerPurchaseMoney内部类的实现:
```java
public class MobileShop {
private int numberOfPhones;
public MobileShop(int numberOfPhones) {
this.numberOfPhones = numberOfPhones;
}
public void sell(int quantity) {
numberOfPhones -= quantity;
System.out.println("You have bought " + quantity + " phones.");
}
public class InnerPurchaseMoney {
private int value;
public InnerPurchaseMoney(int value) {
this.value = value;
}
public void use() {
int quantity = 0;
if (value == 10000) {
quantity = 2;
} else if (value == 20000) {
quantity = 4;
}
if (quantity > numberOfPhones) {
System.out.println("Sorry, we don't have enough phones.");
} else {
sell(quantity);
}
}
}
}
```
MobileShop类有一个sell方法,用于出售手机。InnerPurchaseMoney类用于处理内部购物券的逻辑,包括购买手机的数量和检查库存是否足够。
下面是使用方法的示例:
```java
MobileShop shop = new MobileShop(10);
MobileShop.InnerPurchaseMoney coupon1 = shop.new InnerPurchaseMoney(10000);
MobileShop.InnerPurchaseMoney coupon2 = shop.new InnerPurchaseMoney(20000);
coupon1.use(); // You have bought 2 phones.
coupon2.use(); // You have bought 4 phones.
coupon2.use(); // Sorry, we don't have enough phones.
```
相关推荐
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)