【每日一题1125】抢红包

:woman_mage: 抢红包案例

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

1、群主负责发红包。
红包金额从群主余额中扣除,按成员人数平均分成n等份,以备领取。每个红包的金额为整数,如果有余数则加到最后一个红包中。

2、成员负责抢红包。
抢到的红包金额存到自己余额中。

3、抢完红包后需要进行报数,打印格式“我是XX,现在有 XX 块钱。”。

请根据描述信息,根据面向对象的知识,完成案例中所有类的定义,类之间的继承关系,以及发红包、抢红包的操作。

package com.readPackage;

/**
 * 群主和群成员父类
 */
public class Person {
    // 余额
    int remainingSum;

    public int getRemainingSum() {
        return remainingSum;
    }

    public void setRemainingSum(int remainingSum) {
        this.remainingSum = remainingSum;
    }


    public Person(int remainingSum) {
        this.remainingSum = remainingSum;
    }
}
/**
 * 群主类
 */
public class GroupHolder extends Person{
    ArrayList<GroupMember> group = new ArrayList<>();

    public GroupHolder(int remainingSum) {
        super(remainingSum);
        this.remainingSum = remainingSum;
    }


    public int getRemainingSum() {
        return remainingSum;
    }

    public void setRemainingSum(int remainingSum) {
        this.remainingSum = remainingSum;
    }

    public ArrayList<GroupMember> getGroup() {
        return group;
    }
    
    // 拉人入群
    public void giveGroup(GroupMember groupMember) {
        group.add(groupMember);
    }

    // 获取群人数
    public int getCount() {
        return group.size();
    }

    // 创建红包
    public ArrayList<RedPackage> createRedPackage(int number) {
        // 获取余额
        int remainingSum = this.getRemainingSum();
        if (number - remainingSum > 0) {
            System.out.println("余额不足");
            return null;
        }
        // 获取成员数
        int count = this.getCount();
        // 创建红包数组
        ArrayList<RedPackage> redPackages = new ArrayList<>();
        // 获取单个红包钱数
        int redSum = 0;
        if (number % count == 0) {
            redSum = number / count;
            for (int i = 0; i < count; i++) {
                RedPackage redPackage = new RedPackage(redSum);
                redPackages.add(redPackage);
            }
        } else {
            redSum = (number - number % count) / count;

            for (int i = 0; i < count - 1; i++) {
                RedPackage redPackage = new RedPackage(redSum);
                redPackages.add(redPackage);
            }
            RedPackage redPackage = new RedPackage(redSum + number % count);
            redPackages.add(redPackage);
        }
        return redPackages;
    }

    /**
     * 发红包
     */
    public ArrayList<RedPackage> sendRedPackage(int number) {
        return createRedPackage(number);
    }
}
/**
 * 群成员类
 */
public class GroupMember extends Person {
    // 昵称
    int id;
    String nickName;

    public GroupMember(int id, int remainingSum, String nickName) {
        super(remainingSum);
        this.id = id;
        this.nickName = nickName;
    }
    
    public int getRemainingSum() {
        return remainingSum;
    }

    public void setRemainingSum(int remainingSum) {
        this.remainingSum = remainingSum;
    }

    /**
     * 抢红包
     */
    public synchronized ArrayList<RedPackage> giveRedPackage(ArrayList<RedPackage> redPackages) {
        if (redPackages.size() == 0) {
            System.out.println("哎呀你手慢了");
            return null;
        }
        int remainingSum = this.getRemainingSum();
        this.setRemainingSum(remainingSum + redPackages.get(0).getNumber());
        redPackages.remove(0);
        System.out.println("我是" + this.nickName + ",现在有 " + this.remainingSum + " 块钱。");
        return redPackages;
    }
/**
 * 红包类
 */
public class RedPackage {
    // 红包金额
    private int number;

    public RedPackage(int number) {
        this.number = number;
    }

    public int getNumber() {
        return number;
    }

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

/**
 * 测试类
 */
public class TestRedPackage {
    @Test
    public void TestReadPackage() {
        // 创建群员
        GroupMember tom = new GroupMember(1, 100, "tom");
        GroupMember tony = new GroupMember(2, 200, "tony");
        GroupMember jack = new GroupMember(3, 300, "jack");
        // 创建群主
        GroupHolder groupHolder = new GroupHolder(10000);
        // 拉人入群
        groupHolder.giveGroup(tom);
        groupHolder.giveGroup(tony);
        groupHolder.giveGroup(jack);
        // 创建红包并发红包
        ArrayList<RedPackage> redPackage = groupHolder.sendRedPackage(900);
        // 模拟抢红包
        Random ran1 = new Random(3);
        for (int i = 0; i < 4; i++) {
            int i1 = ran1.nextInt(2)+1;
            for (int j = 0; j < groupHolder.getGroup().size(); j++) {
                if (groupHolder.getGroup().get(j).id == i1) {
                    redPackage = groupHolder.getGroup().get(j).giveRedPackage(redPackage);
                }
            }
        }
    }
}
1 个赞