写一个程序,制作一个咖啡售卖机
时间: 2023-06-19 09:02:55 浏览: 57
好的,下面是一个简单的咖啡售卖机程序:
```python
# 咖啡售卖机程序
# 定义咖啡的种类和价格
coffee_menu = {
"美式咖啡": 20,
"拿铁咖啡": 25,
"卡布奇诺": 30
}
# 打印菜单
print("欢迎来到咖啡售卖机,请选择您需要的咖啡:")
for coffee in coffee_menu:
print(coffee + ":" + str(coffee_menu[coffee]) + "元")
# 获取用户输入
choice = input("请输入您需要的咖啡:")
# 根据用户选择计算价格
if choice in coffee_menu:
price = coffee_menu[choice]
print("您选择的是" + choice + ",需要支付" + str(price) + "元。")
else:
print("抱歉,我们没有这种咖啡。")
```
该程序首先定义了咖啡的种类和价格,然后打印出菜单供用户选择。用户输入选择后,程序会根据用户选择计算价格并打印出来。如果用户选择的咖啡不在菜单中,则会提示“抱歉,我们没有这种咖啡。”
相关问题
用java写一个程序,制作一个咖啡售卖机,用三个类
好的,下面是我给您提供的代码,共包含三个类:CoffeeMachine、Coffee和Customer。
CoffeeMachine类:
```java
public class CoffeeMachine {
private int coffeeCount; // 咖啡库存数量
private double balance; // 咖啡机余额
public CoffeeMachine(int coffeeCount, double balance) {
this.coffeeCount = coffeeCount;
this.balance = balance;
}
public int getCoffeeCount() {
return coffeeCount;
}
public void setCoffeeCount(int coffeeCount) {
this.coffeeCount = coffeeCount;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
// 咖啡售卖方法
public void sellCoffee(Customer customer) {
if (coffeeCount <= 0) {
System.out.println("咖啡机已售罄!");
return;
}
if (customer.getBalance() < 10) {
System.out.println("余额不足,请先充值!");
return;
}
coffeeCount--;
balance += 10;
customer.setBalance(customer.getBalance() - 10);
System.out.println("咖啡售出成功!");
}
}
```
Coffee类:
```java
public class Coffee {
private String name; // 咖啡名称
private double price; // 咖啡价格
public Coffee(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
```
Customer类:
```java
public class Customer {
private double balance; // 余额
public Customer(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
```
这三个类分别表示咖啡售卖机、咖啡和顾客。其中,Coffee类只是一个简单的咖啡类,用于存储咖啡的名称和价格。Customer类表示顾客,只有一个余额属性。CoffeeMachine类是咖啡售卖机,拥有咖啡库存数量和余额属性,以及售卖咖啡的方法sellCoffee()。在该方法中,首先判断咖啡库存数量是否大于0,如果不是,则提示“咖啡机已售罄!”;然后判断顾客的余额是否足够购买咖啡,如果不足,则提示“余额不足,请先充值!”;否则,咖啡库存数量减1,咖啡机余额加上咖啡价格,顾客余额减去咖啡价格,最后提示“咖啡售出成功!”。
下面是一个简单的测试代码:
```java
public class Test {
public static void main(String[] args) {
CoffeeMachine coffeeMachine = new CoffeeMachine(10, 0);
Customer customer = new Customer(20);
Coffee coffee = new Coffee("拿铁", 10);
coffeeMachine.sellCoffee(customer);
System.out.println("余额:" + customer.getBalance() + ",咖啡机余额:" + coffeeMachine.getBalance() + ",咖啡库存:" + coffeeMachine.getCoffeeCount());
coffeeMachine.sellCoffee(customer);
System.out.println("余额:" + customer.getBalance() + ",咖啡机余额:" + coffeeMachine.getBalance() + ",咖啡库存:" + coffeeMachine.getCoffeeCount());
customer.setBalance(5);
coffeeMachine.sellCoffee(customer);
System.out.println("余额:" + customer.getBalance() + ",咖啡机余额:" + coffeeMachine.getBalance() + ",咖啡库存:" + coffeeMachine.getCoffeeCount());
}
}
```
该测试代码创建了一个咖啡售卖机对象、一个顾客对象和一个咖啡对象。第一次售卖咖啡时,因为顾客余额足够且咖啡库存充足,所以售卖成功;第二次售卖咖啡时,由于咖啡库存不足,所以售卖失败;第三次售卖咖啡时,由于顾客余额不足,所以售卖失败。最后,打印出顾客余额、咖啡机余额和咖啡库存的信息。
用verilog写一个自动售卖机
一个简单的自动售卖机可以实现以下功能:
1. 有一个选择货物的接口,可以使用按钮等方式选择货物。
2. 当用户选择货物并投入足够的金额时,售卖机会释放货物并找零。
3. 如果用户投入的金额不足,售卖机会拒绝交易并保持原状态。
下面是一个简单的Verilog自动售卖机的实现示例:
```verilog
module vending_machine(
input clk,
input rst,
input [3:0] selection, // 货物选择输入
input [7:0] money_in, // 投入金额输入
output reg [3:0] item_out, // 货物输出
output reg [3:0] change_out // 找零输出
);
// 货物价格表
parameter [3:0] ITEM_PRICE [7:0] = {
4'h0: 8, // 商品0价格为8元
4'h1: 10, // 商品1价格为10元
4'h2: 12, // 商品2价格为12元
4'h3: 5, // 商品3价格为5元
4'h4: 6, // 商品4价格为6元
4'h5: 15, // 商品5价格为15元
4'h6: 20, // 商品6价格为20元
4'h7: 3 // 商品7价格为3元
};
reg [7:0] money; // 当前余额
reg [3:0] item; // 当前选择的货物
reg [3:0] change; // 当前找零
// 状态机
parameter IDLE = 2'b00, SELECTED = 2'b01, VENDING = 2'b10;
reg [1:0] state = IDLE;
always@(posedge clk or posedge rst) begin
if(rst) begin
state <= IDLE;
money <= 0;
item <= 0;
change <= 0;
end else begin
case(state)
IDLE: begin
if(selection != 4'hF && ITEM_PRICE[selection] <= money_in) begin
item <= selection;
money <= money_in;
state <= SELECTED;
end
end
SELECTED: begin
if(money >= ITEM_PRICE[item]) begin
item_out <= item;
change <= money - ITEM_PRICE[item];
money <= 0;
state <= VENDING;
end else if(selection != 4'hF && ITEM_PRICE[selection] <= money + money_in) begin
item <= selection;
money <= money + money_in;
end
end
VENDING: begin
if(change > 0) begin
change_out <= change;
change <= 0;
item_out <= 0;
state <= IDLE;
end
end
endcase
end
end
endmodule
```
这个自动售卖机模块有四个输入和两个输出:
1. 时钟输入 `clk`。
2. 复位输入 `rst`。
3. 货物选择输入 `selection`,4位二进制数,其中F表示无效输入。
4. 投入金额输入 `money_in`,8位二进制数。
5. 货物输出 `item_out`,4位二进制数,表示售出的货物编号,F表示无货出售。
6. 找零输出 `change_out`,4位二进制数,表示找零金额,F表示无找零。
该模块使用状态机实现自动售卖机的状态转换。它有三个状态:
1. IDLE:空闲状态,等待货物选择和投币。
2. SELECTED:货物已选择状态,等待用户投入足够的金额或更换选择。
3. VENDING:售货中状态,等待售货完成并找零。
在每个状态下,模块会根据输入和当前状态进行相应的操作,并更新状态和输出。