根据我们校区的情况,解决了干饭人在干饭道路上的一些不便,所以我做了一个随机点菜程序。没关系,需要的老铁被拿走了
----------------->>>>不需要拿走,不喜欢就不喷<<<<------------------------
随机获取菜名和价格 实现荤素搭配 自定义菜数
超件流输入文件中的菜名装入数组
定义了数组的长度length=1.读入一行后扩容,读取时null上次读的时候菜名数组扩容了一个长度,所以读的时候null时要缩容;
一个价格数组,一个菜名数组,价格和菜名在两个数组中下标是一样的。只要在随机获取菜名的同时获取菜名下标,菜名和价格就可以根据下标取出价格数组中的价格来实现;
package ording; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Random; import java.util.Scanner; public abstract class ObjectMenu { int stNum; String[] stir = new String[1]; String[] suCai = new String[1]; String[] Cold = new String[1]; String[] Speciality = new String[1]; String[] DryWok = new String[1]; int[] SuP = {18,15、16、16、16、16、12、18、13、16、15、16、16、15、15、15、16、15、16、16、15、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、16、 int[] Meat ={18, 18, 20,18, 22, 30, 38, 18,20, 20, 20, 18, 18, 18, 18, 38, 20, 20, 18, 48, 20, 20, 18, 18, 22, 20, 20, 20,20, 30, 18, 30, 18, 18, 30, 18, 18, 40, 18, 38, 30, 38,}; int[] specialityPrice = {0, 0, 48, 38, 38, 38, 38, 38, 48, 38, 32, 32, 35, 38, 28, 40, 22, 40}; int[] Coldprice = {8, 8, 8, 22, 12, 22, 10, 12, 10, 15}; int[] DryWokPrice = {46, 48, 46, 48, 48, 48, 48, 46, 20, 20, 46, 48}; int[] MenuPrice; String[] sui; String name; public String[] ObjectMenu(String fileName) throws IOException { FileInputStream fis = new FileInputStream(fileName); InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8); BufferedReader br = new BufferedReader(isr); String line; // if (fileName == "./src/Menu/StirFry.txt") { // sui = stir; // // name = "荤菜"; // }else if (fileName=="./src/Menu/ColdDishes.txt"){ // sui = Cold; // name ="凉菜"; // // // }else if (fileName =="./src/Menu/suCai.txt"){ // sui = suCai; // name = "素菜"; // } switch (fileName){ case "./src/Menu/StirFry.txt": sui = stir; MenuPrice = Meat; name = "荤菜"; break; case "./src/Menu/ColdDishes.txt": sui = Cold; MenuPrice = Coldprice; name ="凉菜"; break; case "./src/Menu/suCai.txt": sui = suCai; MenuPrice = SuP; name = "素菜"; break; case "./src/Menu/Speciality.txt": sui = Speciality; MenuPrice = specialityPrice; name = "特色菜"; break; case "./src/Menu/DryWok.txt": sui = DryWok; MenuPrice = DryWokPrice; name = "干锅"; break; } for (int i = 0; i < sui.length; i ) { if ((line = br.readLine()) != null) { sui[i] = line; sui = Arrays.copyOf(sui,sui.length 1); } else { sui = Arrays.copyOf(sui,sui.length-1); break; } } //System.out.println(Arrays.toString(sui)); br.close(); return sui; } public void getRandomArray(int stNum){ this.stNum = stNum; Random rand = new Random(); ///随机选择的元素装入数组sf int[] Price = new int[stNum]; String[] sf = new String[stNum]; int index; for (int i = 0; i < stNum; i ) { index = rand.nextInt(sui.length-i); sf[i] = sui[index]; Price[i] = MenuPrice[index]; for (int j = index; j < sui.length-i-1; j ) { sui[j] = sui[j 1]; } } for (int i = 0; i < sf.length; i ) { System.out.println(sf[i] "\t" Price[i] "元"); } // for ( String str:sf){ // System.out.println(str "\t" Price "元"); // } } public int StNum(){ Scanner scanner = new Scanner(System.in); System.out.println("请输入您的要点" name "个数:"); stNum = scanner.nextInt(); return stNum; } }
子类以菜类分为五类,主要获取菜名文件的文件名并返回fileName方便文件流调
package ording; //干锅类 public class DryWok extends ObjectMenu{ public String DryWok(){ String fileName = "./src/Menu/DryWok.txt"; return fileName; } } package ording; //凉菜类 public class ColdDishes extends ObjectMenu{ public String ColdDishes(){ String fileName = "./src/Menu/ColdDishes.txt"; return fileName; } } package ording; ///特色菜 public class Speciality extends ObjectMenu{ public String Speciality(){ String fileName = "./src/Menu/Speciality.txt"; return fileName;
}
}
package ording;
//农家小炒类
public class StirFry extends ObjectMenu {
public String SFry(){
String fileName = "./src/Menu/StirFry.txt";
return fileName;
}
}
package ording;
//素菜类
public class Vegetables extends ObjectMenu{
public String Vege(){
String fileName = "./src/Menu/suCai.txt";
return fileName;
}
}
运行结果:
菜单文件-txt文件可以自己定义菜名