----------main函数--------------------
package com.zhidi.object ;
import java.util.Random;
import java.util.Scanner;
public class Storetest {
public static void main (String[] args) {
while (true ){
Store t=new Store();
Storetest e=new Storetest();
System.out .println("欢迎使用牛X点餐系统," );
System.out .println("请选择餐品:1、牛肉面 (10/12元);2、鸡丁面(8/10元);"
+ "3、鱼丸粗面(12/15元);4、随便;5、查看营收" );
Scanner sc=new Scanner(System.in );
int s=sc.nextInt();
if (s==4 ){
Random ran=new Random();
t.type=ran.nextInt(2 )+1 ;
}else if (s==5 ){
System.out .println("营业额为:" +t.money+",销量为:" +t.num);
continue ;
}else {
t.type=s;
}
System.out .println("请选择分量:1、小份;2、大份" );
int f=sc.nextInt();
switch (f){
case 1 :
t.weight=true ;
break ;
case 2 :
t.weight=false ;
break ;
}
t.check();
}
}
}
----------类函数--------------------
package com.zhidi.object ;
import java.util.Random;
public class Store {
public int type;
public boolean weight;
public static int money;
public static int num;
public Store (){
this .type=type;
this .weight=true ;
}
public void check (){
if (weight){
switch (type){
case 1 :money+=10 ;
num++;
System.out .println("点餐成功,小份牛肉面,实收10元!" );
break ;
case 2 : money+=8 ;
num++;
System.out .println("点餐成功,小份鸡丁面,实收8元!" );
break ;
case 3 : money+=12 ;
num++;
System.out .println("点餐成功,小份鱼丸面,实收12元!" );
break ;
}
}else {
switch (type){
case 1 :money+=12 ;
num++;
System.out .println("点餐成功,大份牛肉面,实收12元!" );
break ;
case 2 : money+=10 ;
num++;
System.out .println("点餐成功,大份鸡丁面,实收10元!" );
break ;
case 3 : money+=15 ;
num++;
System.out .println("点餐成功,大份鱼丸面,实收15元!" );
break ;
}
}
}
}