其他分享
首页 > 其他分享> > 群主发普通红包.007

群主发普通红包.007

作者:互联网

群主发普通红包。某群有多名成员,群主给成员发普通红包。普通红包的规则:

1. 群主的一笔金额,从群主余额中扣除,平均分成n等份,让成员领取。

2. 成员领取红包后,保存到成员余额中

在这里插入图片描述User

public class User {
    private String name;
    private double money;

    public void show(){
        System.out.println("我是"+name+",我有"+money+"元");
    }

    public String getName() {
        return name;
    }

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

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public User(String name, double money) {
        this.name = name;
        this.money = money;
    }
}

QunZhu

public class QunZhu extends User{
    public QunZhu(String name,double money) {

        super(name,money);
    }

    /* 群主发红包,就是把一个整数的金额,分层若干等份。
        1.获取群主余额,是否够发红包. 不能则返回null,并提示. 能则继续.
        2.修改群主余额.
        3.拆分红包.
            3.1.如果能整除,那么就平均分。
            3.2.如果不能整除,那么就把余数分给最后一份。
      */
    public ArrayList<Double> send(int money,int count){
        double QunZhuMoney = getMoney();//群主余额
        if(money>QunZhuMoney){
            System.out.println("money:"+money+",QunZhuMoney:"+QunZhuMoney);
            System.out.println("钱不够!!!!!!!!");

           return null;
        }

        //修改群主余额

        setMoney(QunZhuMoney-money);

        //创建等份红包金额
        ArrayList<Double> list = new ArrayList<>();
        money*=100;//扩大金额,防止精度损失
        int red = money / count;
        int yue = money % count;//余数

        for (int i = 0; i < count-1; i++) {
            list.add(red/100.0);//还原金额
        }
        list.add((red+yue)/100.0);

        return list;
    }



}

Member群成员

public class Member extends User{

    public Member(String name,double money) {
        super(name,money);
    }
    public void openHongBao(ArrayList<Double> list){
        Random  r = new Random();

        int index = r.nextInt(list.size());

        Double money = list.remove(index);

        setMoney(money);
    }
}

测试类

public class TestSendRedPacket {

    public static void main(String[] args) {
        QunZhu qz = new QunZhu("群主",1000);

        ArrayList<Double> list = qz.send(1001, 5);

        // 判断,如果余额不足
        if(list == null){
            System.out.println(" 余额不足...");
            return;
        }
        Member m1 = new Member("A",0);
        Member m2 = new Member("B",0);
        Member m3 = new Member("C",0);
        Member m4 = new Member("D",0);
        Member m5 = new Member("E",0);

        m1.openHongBao(list);
        m2.openHongBao(list);
        m3.openHongBao(list);
        m4.openHongBao(list);
        m5.openHongBao(list);

        qz.show();
        m1.show();
        m2.show();
        m3.show();
        m4.show();
        m5.show();
    }
}

2021.5.16

标签:红包,name,money,list,Member,007,群主,public
来源: https://blog.csdn.net/m0_54934302/article/details/116894363