
buy.java
public class Buy { public static void main(String[] args) {
// TODO Auto-generated method stub
Shops shops = new Shops(); } }
shops.java
import java.util.Scanner; public class Shops {
public static int m;
public static int numberCup = 0 , numberRubber = 0, numberPen = 0, allPrice = 0;
/** 构 造 函 数 */
public Shops(){ showAllGoods();
} /** 显 示 所 有 的 商 品 */
public void showAllGoods(){
System.out.println("欢迎来到自助超市, 我们这里有以下商品\n1.笔\n2.水杯\n3.橡皮\n4.退出\n请输入相应的编号购买:");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
switch (i) {
case 1:
System.out.println("请输入数量:");
inputCode();
numberPen += m;
showAllBuyGoods(); break;
case 2:
System.out.println("请输入数量:");
inputCode();
numberCup += m;
showAllBuyGoods(); break; case 3:
System.out.println("请输入数量:");
inputCode();
numberRubber += m;
showAllBuyGoods();
break;
case 4:
System.exit(1);
break;
default:
break;
} }
/** 用 户 输 入 商 品 编 码 函 数 */
public void inputCode(){
Scanner scann = new Scanner(System.in);
m = scann.nextInt();
}
/** 显 示 已 经 购 买 的 商 品 */
public void showAllBuyGoods(){
allPrice = numberCup * 5 + numberPen * 3 + numberRubber * 2 ;
System.out.println("你选择了"+numberPen+"只笔,"+numberCup+"个水杯,"+numberRubber+"个橡皮擦, 一共"+allPrice+"元.还需要其他的吗, 需要请输入1, 不需要请输入2.");
isGoOn(); }
/** 看 用 户 是 否 继 续 */
public void isGoOn(){
Scanner scann = new Scanner(System.in);
m = scann.nextInt();
if (m == 1) {
showAllGoods();
}else if (m == 2) {
//TODO
allPrice = numberCup * 5 + numberPen * 3 + numberRubber * 2 ;
buy(allPrice);
}else{
System.out.println("输入有误, 请重新输入.");
isGoOn();
} }
/** 购 买 函 数 */
public void buy(int a){
if (a > 100) {
System.out.println("选中的物品大于100元,请删除:");
deleteGoods();
}else{
System.out.println("付款成功.");
} }
/** 删 除 商 品 */
public void deleteGoods(){
System.out.println("1.笔 2.水杯 3.橡皮");
Scanner scanner = new Scanner(System.in);
int j = scanner.nextInt();
switch (j) {
case 1:
System.out.println("请输入数量:");
inputCode();
numberPen -= m;
showAllBuyGoods(); break;
case 2:
System.out.println("请输入数量:");
inputCode();
numberCup -= m;
showAllBuyGoods();
break; case 3:
System.out.println("请输入数量:");
inputCode();
numberRubber -= m;
showAllBuyGoods();
break;
case 4:
System.exit(1);
break;
default:
break; }
}
}