weixin_63267465 2022-06-27 04:48 采纳率: 100%
浏览 97
已结题

java多态和接口题,要代码和运行截图

img


面向对象(2)(接口和多态、Random类使用)小明现在想点外卖,美团外卖的优惠劵规则是:满50随机生成15以内的随机优惠劵(100两张,以此类推),配送费5元;饿了吗的优惠劵规则:满100随机生成25以内的优惠劵一张,配送费10元。

  • 写回答

1条回答 默认 最新

  • it_hao528 2022-06-27 07:25
    关注

    外卖平台接口类

    public interface Takeaway {
        // 计算总费用
        double getTotalMoney(List<Shop> shops);
        // 计算优惠费用
        double getDiscountMoney(double totalMoney);
    }
    

    美团实现类

    public class Meituan implements Takeaway {
        @Override
        public double getTotalMoney(List<Shop> shops) {
            double sum = 0;
            for (Shop shop : shops) {
                sum += shop.getPrice() * shop.getCount();
            }
            return sum;
        }
        @Override
        public double getDiscountMoney(double totalMoney) {
            Random r = new Random();
            double sum = 0;
            int n = (int) (totalMoney / 50);
            for (int i = 0; i < n; i++) {
                // 随机生成15以内优惠券并相加,精确到小数点后两位
                sum += (r.nextInt(1500) + 1) / 100.0;
            }
            return sum;
        }
    }
    

    饿了么实现类

    public class Eleme implements Takeaway {
        @Override
        public double getTotalMoney(List<Shop> shops) {
            double sum = 0;
            for (Shop shop : shops) {
                sum += shop.getPrice() * shop.getCount();
            }
            return sum;
        }
        @Override
        public double getDiscountMoney(double totalMoney) {
            Random r = new Random();
            double sum = 0;
            if (totalMoney > 100) {
                // 随机生成25以内优惠券并相加,精确到小数点后两位
                sum = (r.nextInt(2500) + 1)/100.0;
            }
            return sum;
        }
    }
    

    测试类main方法

    public static void main(String[] args) {
        Shop s1 = new Shop(30, 3);
        Shop s2 = new Shop(12, 3);
        Shop s3 = new Shop(13, 2);
        Shop s4 = new Shop(15, 3);
        List<Shop> list = new ArrayList<>();
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        Takeaway t1 = new Meituan();
        double totalMoney1 = t1.getTotalMoney(list);
        double discountMoney1 = t1.getDiscountMoney(totalMoney1);
        System.out.println("美团需要付钱总数:" + (totalMoney1 - discountMoney1) + ",共优惠了:" + discountMoney1);
        Takeaway t2 = new Eleme();
        double totalMoney2 = t2.getTotalMoney(list);
        double discountMoney2 = t2.getDiscountMoney(totalMoney1);
        System.out.println("饿了么需要付钱总数:" + (totalMoney2 - discountMoney2) + ",共优惠了:" + discountMoney2);
    }
    

    运行截图

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月27日
  • 已采纳回答 6月27日
  • 创建了问题 6月27日

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号