已经实现的功能:
- 能够自定义投入钱;
- 能够显示当前余额;
- 能提供给客户所选择的商品。
源代码如下:
import java.util.Scanner;
public class VendingMachine {
int price = 50;//商品单价
int balance;//余额
int total;//总金额
void showPrompt(){//欢迎信息
System.out.println("welcome!");
}
void insertMoney(){//投入钱
Scanner scanner = new Scanner(System.in);
System.out.print("请输入整数: ");
int amount = scanner.nextInt();
balance = balance + amount;
}
void showBalance(){//显示余额
System.out.print("当前余额为: ");
System.out.println(balance);
}
void getFood(){//获得商品
if( balance >= price)
{
System.out.println("Here you are!");
balance = balance - price;
total = total + price;
}
}
void peroration(){//结束语
System.out.println("Looking forward to your next visit!");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
VendingMachine vm = new VendingMachine();
vm.showPrompt();
vm.showBalance();
vm.insertMoney();
vm.showBalance();
vm.getFood();
vm.showBalance();
vm.peroration();
}
}