Loading

JAVA练习-继承-发红包案例

要求

老板给员工发红包,红包金额为小数点两位,并且红包随机分配金额。

User.java

package com.inherit;

public class User {
    private String username;
    private Double remainMoney;

    public User() {
    }

    public User(String username, Double remainMoney) {
        this.username = username;//角色
        this.remainMoney = remainMoney;//剩余金额
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Double getRemainMoney() {
        return remainMoney;
    }

    public void setRemainMoney(Double remainMoney) {
        this.remainMoney = remainMoney;
    }

    public void show() {
        System.out.println("username:"+username+"-----"+"remainMoney:"+remainMoney);
    }
}

Boss.java

package com.inherit;

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

public class Boss extends User{
    /**
     * 如果发送的金额大于余额,返回null
     *
     */
    public Boss() {
    }

    public Boss(String username, Double remainMoney) {
        super(username, remainMoney);
    }
    public List<Double> send(double money, int count){
        double remainMoney = super.getRemainMoney();
        List<Double> list = new ArrayList<Double>();
        if(money > remainMoney || count == 0){
            return null;
        }else{
            //设置老板新的余额
            super.setRemainMoney(remainMoney-money);
            //如果红包个数只有一个,全部给他
            if(count == 1){
                list.add(Double.parseDouble(String.format("%.2f",money)));
            }else{
                //红包个数为n,前n-1个随机分配,最后一个得到剩余全部金额
                while(count > 1){
                    //最小的红包金额0.01
                    double num = Math.random() * (money-0.01)+0.01;
                    //System.out.println("num"+num);
                    num = Double.parseDouble(String.format("%.2f",num));
                    money -= num;
                    //System.out.println(money);
                    list.add(num);
                    count--;
                }
                //最后一个红包
                list.add(Double.parseDouble(String.format("%.2f",money)));
            }
        }
        return list;
    }
}

Employee.java

package com.inherit;

import java.util.List;
import java.util.Random;

public class Employee extends User{
    public Employee(String username, Double remainMoney) {
        super(username, remainMoney);
    }

    public Employee() {
    }

    public double receive(List<Double> list){
        Random random = new Random();
        int i = random.nextInt(list.size());
        Double remainMoney = list.get(i);
        list.remove(i);
        //设置余额
        super.setRemainMoney(remainMoney);
        return remainMoney;
    }
}

Test.java

package com.inherit;

import java.util.List;
import java.util.Scanner;

public class Test {
    public static void main(String[] args){
        Boss boss = new Boss("老板", 200.00);
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入金额:");
        double money = scan.nextDouble();
        System.out.println("请输入个数:");
        int count = scan.nextInt();
        List<Double> send = boss.send(money, count);
        if(send == null){
            System.out.println("金额不足!");
            return ;
        }else{
            Employee one = new Employee("one",0.0);
            Employee two = new Employee("two",0.0);
            Employee three = new Employee("three",0.0);
            double a = one.receive(send);
            double b = two.receive(send);
            double c = three.receive(send);
//            System.out.println(a);
//            System.out.println(b);
//            System.out.println(c);
            boss.show();
            one.show();
            two.show();
            three.show();

        }
    }
}

核心语句

public List<Double> send(double money, int count){
        double remainMoney = super.getRemainMoney();
        List<Double> list = new ArrayList<Double>();
        if(money > remainMoney || count == 0){
            return null;
        }else{
            //设置老板新的余额
            super.setRemainMoney(remainMoney-money);
            //如果红包个数只有一个,全部给他
            if(count == 1){
                list.add(Double.parseDouble(String.format("%.2f",money)));
            }else{
                //红包个数为n,前n-1个随机分配,最后一个得到剩余全部金额
                while(count > 1){
                    //最小的红包金额0.01
                    double num = Math.random() * (money-0.01)+0.01;
                    //System.out.println("num"+num);
                    num = Double.parseDouble(String.format("%.2f",num));
                    money -= num;
                    //System.out.println(money);
                    list.add(num);
                    count--;
                }
                //最后一个红包
                list.add(Double.parseDouble(String.format("%.2f",money)));
            }
        }
        return list;
    }

运行结果:

posted @ 2021-06-07 20:32  IamHzc  阅读(67)  评论(0编辑  收藏  举报