JAVA萌新学习day16

JAVA萌新学习day16

设计三个类(每个类名前加前缀 为 你的名这字的全拼)

  1. Food类(菜)(价格,名称, 编号 ,类别)
  2. Menu类(菜单)(可以根据类别保存所有的食物,商家名)
  3. Manager类(商家)(管理所有商家的菜单)
    3.1 统计某个商家的菜品总价
    3.2 根据菜名搜索获得所有的菜(返回值不仅一个)
    3.3 根据菜名和商家名删除一个菜
    3.4 给商家菜按价格排序
    3.5 根据指定多个菜名,获得此次所有食物的最低报价

每步操作后,需要打印出结果

Manager类中有一个test静态方法,在里面写上所有测试代码

所有类的包名统一用 com.java1904.mytest;

Food


import java.util.Comparator;

public class Food implements Comparator<Food> {
    private String name;
    private int price;
    private int number;
    private String type;

    public Food(){
        this("",0,0,"");
    }


    public Food(String name, int price, int number, String type) {
        this.name = name;
        this.price = price;
        this.number = number;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "Food{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", number=" + number +
                ", type='" + type + '\'' +
                '}';
    }

    @Override
    public int compare(Food o1, Food o2) {
        return o1.getPrice()-o2.getPrice();
    }
}

Menu


import java.util.ArrayList;
import java.util.Random;

public class Menu {
    private ArrayList<Food> foods;
    private String name;

    public Menu(){
        this("");
    }
    public Menu(String name) {
        this.name = name;

        //产生当前menu对象的所有food对象,假数据
        randomFoods();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    private String type(){
        String[] names = {"海鲜","肉类","蔬菜","豆制品"};
        Random random = new Random();
        int index = random.nextInt(names.length);
        return names[index];
    }
    //辅助方法,用来产生随机值的food对象
    private Food food(int number){

        Random random = new Random();
        int price = random.nextInt(80)+20;
        String name = Manager.randomString(8);
        String type= type();
        Food food = new Food(name,price,number,type);
        return food;
    }

    public void randomFoods(){
        foods = new ArrayList<>();
        //添加food对象
        Random random = new Random();
        int count = random.nextInt(10)+5;

        for (int i = 0; i <count ; i++) {
            Food food = food(i+1);
            foods.add(food);
        }

    }
    public int totalPrice(){

        int total =0;
        for(Food food : foods){
            total += food.getPrice();
        }
        return total;
    }
    public int foodCount(){
       return foods.size();
    }
    //根据位置获得食物对象
    public Food searchFoodIndex(int index){
        if(index>=0 && index<foodCount()){

            Food food = foods.get(index);

            return food;
        }
        return null;
    }
    public Food searchFood(String name){

        for(Food food : foods){
            if(food.getName().equals(name)){
                return food;
            }
        }
        return null;

    }
    public void sort(int price){

        //System.out.println(this);
        foods.sort(new Food());
        //System.out.println(this);
    }
    public boolean deleteFood(String name){
        for(Food food : foods){
            if(food.getName().equals(name)){
                foods.remove(food);
                return true;
            }
        }
        return false;
    }
    @Override
    public String toString() {

        StringBuffer buffer = new StringBuffer();

        for(Food food : foods){
            buffer.append(food.toString());
            buffer.append("|");
        }
        buffer.append("Menu{" +
                "name='" + name + '\'' +
                '}');

        return buffer.toString();
    }
}

Manager

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;

public class Manager {
    private ArrayList<Menu> menus;

    public Manager() {


        //产与所有商家的假数据
        randomMenus();
    }
    public void randomMenus(){
        menus = new ArrayList<>();

        Random random = new Random();
        int count = random.nextInt(5)+5;

        for (int i = 0; i <count ; i++) {

            String name = randomString(5);

            Menu menu = new Menu(name);

            menus.add(menu);

        }

    }
    public int searchTotalPriceByName(String name){

        for(Menu menu: menus){
            if(menu.getName().equals(name)){
                return menu.totalPrice();
            }
        }
        return 0;
    }
    public boolean deleteFoodByName(String menuName,String foodName){

        for(Menu menu: menus){
            if(menu.getName().equals(menuName)){
                boolean success = menu.deleteFood(foodName);
                return success;
            }
        }

        return false;
    }
    public ArrayList<Food> searchFoodByNames(String[] names){

        ArrayList<Food> searchFoods= new ArrayList<>();

        for (int i = 0; i <names.length ; i++) {

            int minPrice = 110;//大于100就可以,因为目前 最高价格就是100

            Food tempFood =null ;
            for(Menu menu : menus){

                Food food = menu.searchFood(names[i]);
                if(food!=null){
                    if(food.getPrice()<minPrice){
                        minPrice = food.getPrice();
                        tempFood = food;
                    }
                }
            }
            searchFoods.add(tempFood);

        }
        return searchFoods;
    }
    public void sortFoodByName(String name){

        Iterator<Menu> iterator = menus.iterator();

        while (iterator.hasNext()){

            Menu menu = iterator.next();
            if(menu.getName().equals(name)){
                menu.sort(0);
                break;
            }
        }

    }

    //获得指定长度的随机字符
    public static String randomString(int length){
        String source = "abcdegfhijklmnopqrstuvwxyz";
        Random random = new Random();
        String result = "";

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(source.length());
            //从原字符串中获得指定索引位置的字符
            result += source.charAt(index);

        }
        return result;

    }
    private void printFoodsName(){

        for(Menu menu: menus){
            int count = menu.foodCount();
            String str = "";
            System.out.println("商家名:" + menu.getName());
            for (int i = 0; i < count; i++) {
                Food food = menu.searchFoodIndex(i);
                if(food!=null){
                    str += food.getName()+",";
                }
            }
            System.out.println(str);
            System.out.println();

        }
    }

    public static void test(){
        Manager manager = new Manager();

        Scanner input = new Scanner(System.in);

        manager.printFoodsName();

        System.out.println("请输入一个商家的名称:");

        String name = input.next();

        int totalPrice = manager.searchTotalPriceByName(name);
        System.out.println("商家:"+ name+ "菜价总和是:" + totalPrice);

        System.out.println("请输入一个商家的名称 和一个食物的名称:");
        String menuName = input.next();
        String foodName = input.next();

        boolean success = manager.deleteFoodByName(menuName,foodName);
        if(success){
            System.out.println("找到了");
        }
        else{
            System.out.println("没找到");
        }

        System.out.println("请输入三个食物的名称:");
        String[] names = new String[3];
        for (int i = 0; i <names.length ; i++) {
            names[i] = input.next();
        }
        ArrayList<Food> foods = manager.searchFoodByNames(names);
        if(foods!=null && foods.size()!=0){
            for(Food food: foods){
                System.out.println(food);
            }
        }

        //manager.printFoodsName();

        System.out.println("请输入一个商家的名称:");
        String tempName= input.next();
        manager.sortFoodByName(tempName);

       // manager.printFoodsName();



    }
}

ManagerDemo

public class ManagerDemo {
    public static void main(String[] args) {
        Manager.test();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值