Java面向对象自动贩卖机程序包含多个子类多态接口继承投币支付还有微信支付和支付宝支付库存不足异常和具体货架位置的代码
时间: 2024-02-09 18:09:36 浏览: 67
好的,下面是一个简单的Java面向对象自动贩卖机程序的示例代码,其中包括多个子类、多态、接口、继承、投币支付、微信支付和支付宝支付、库存不足异常以及具体货架位置等:
首先,我们定义一个自动贩卖机类 VendingMachine,它包含了一个 Map 对象 shelves,用于记录货架位置和货架编号的对应关系:
```java
import java.util.HashMap;
import java.util.Map;
public class VendingMachine {
protected Map<String, String> shelves;
public VendingMachine() {
shelves = new HashMap<>();
}
public void showShelves() {
System.out.println("Shelves: " + shelves.toString());
}
}
```
然后,我们定义一个商品类 Item,它包含了商品的名称、价格和数量:
```java
public class Item {
private String name;
private double price;
private int count;
public Item(String name, double price, int count) {
this.name = name;
this.price = price;
this.count = count;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
```
接下来,我们定义一个货架类 Shelf,它包含了一个商品对象和一个位置编号:
```java
public class Shelf {
private Item item;
private String position;
public Shelf(Item item, String position) {
this.item = item;
this.position = position;
}
public Item getItem() {
return item;
}
public String getPosition() {
return position;
}
}
```
然后,我们定义一个支付接口 Payable,它包含了投币支付、微信支付和支付宝支付的方法:
```java
public interface Payable {
public void coinPay(double amount);
public void weChatPay(double amount);
public void aliPay(double amount);
}
```
接下来,我们定义一个自动贩卖机的子类 CoinVendingMachine,它实现了支付接口 Payable:
```java
public class CoinVendingMachine extends VendingMachine implements Payable {
private double balance;
public CoinVendingMachine() {
super();
balance = 0;
}
public void coinPay(double amount) {
balance += amount;
System.out.println("Coin payment of " + amount + " accepted.");
System.out.println("Current balance: " + balance);
}
public void weChatPay(double amount) {
System.out.println("WeChat payment is not supported.");
}
public void aliPay(double amount) {
System.out.println("AliPay payment is not supported.");
}
public void buyItem(String position) throws OutOfStockException {
String shelfNumber = shelves.get(position);
Shelf shelf = getShelf(shelfNumber);
Item item = shelf.getItem();
if (item.getCount() == 0) {
throw new OutOfStockException("Out of stock: " + item.getName());
}
item.setCount(item.getCount() - 1);
System.out.println("Item purchased: " + item.getName());
balance -= item.getPrice();
System.out.println("Current balance: " + balance);
}
private Shelf getShelf(String shelfNumber) {
// Implementation omitted
}
}
```
我们还定义了一个自动贩卖机的子类 MobileVendingMachine,它实现了支付接口 Payable,并支持微信支付和支付宝支付:
```java
public class MobileVendingMachine extends VendingMachine implements Payable {
private double balance;
public MobileVendingMachine() {
super();
balance = 0;
}
public void coinPay(double amount) {
System.out.println("Coin payment is not supported.");
}
public void weChatPay(double amount) {
balance += amount;
System.out.println("WeChat payment of " + amount + " accepted.");
System.out.println("Current balance: " + balance);
}
public void aliPay(double amount) {
balance += amount;
System.out.println("AliPay payment of " + amount + " accepted.");
System.out.println("Current balance: " + balance);
}
public void buyItem(String position) throws OutOfStockException {
String shelfNumber = shelves.get(position);
Shelf shelf = getShelf(shelfNumber);
Item item = shelf.getItem();
if (item.getCount() == 0) {
throw new OutOfStockException("Out of stock: " + item.getName());
}
item.setCount(item.getCount() - 1);
System.out.println("Item purchased: " + item.getName());
balance -= item.getPrice();
System.out.println("Current balance: " + balance);
}
private Shelf getShelf(String shelfNumber) {
// Implementation omitted
}
}
```
最后,我们定义了一个库存不足异常类 OutOfStockException,它继承自 Exception 类:
```java
public class OutOfStockException extends Exception {
public OutOfStockException(String message) {
super(message);
}
}
```
这就是一个简单的Java面向对象自动贩卖机程序的示例代码,它包含了多个子类、多态、接口、继承、投币支付、微信支付和支付宝支付、库存不足异常以及具体货架位置等。
阅读全文