栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java随机点菜

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java随机点菜

根据我们校区的情况,解决干饭人在干饭道路上的一些不便,于是我做了一个随机点菜程序。还行,需要的老铁拿走

----------------->>>>不需要的拿走,不喜勿喷<<<<------------------------

随机获取菜名及价格
实现荤素搭配
自定义菜数

超类-文件流输入文件中的菜名装入数组

数组的长度定义为length=1,在读入一行后扩容,当读取到null时,菜名数组在上一次读取时扩容了1个长度,所以在读到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,12,12,18,13,16,15,12,18,14,15,15,16,16,15,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;
}

}

 子类-以菜的类型分为5个类,主要获取菜名文件的文件名并返回给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文件可以自己定义菜名

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/967377.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号