使用Java实现发红包的功能。

  结构:

    package redPocket    

      User.java      

      Manager.java

      Member.java

      Demo.java

  思路:

    Manager和Member类继承User,Manager类实现群主发红包的功能,Member类实现群员收红包的功能。Demo测试代码。

  代码:

    

package redPocket;

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

    public User() {
    }

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

    public String getName() {
        return name;
    }

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

    public float getMoney() {
        return money;
    }

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

    @Override
    public String toString() {
        return "name:" + this.name + "; " + "money:" + this.money + "; ";
    }
}
User.java
package redPocket;

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

public class Manager extends User {

    public Manager() {
    }

    public Manager(String name, float money) {
        super(name, money);
    }

    public ArrayList<Float> send(float totalMoney, int count, boolean isRandom) {
        if (!hasEnoughMoney(totalMoney)) {
            System.out.println("You do not have enough money!");
            return null;
        }

        ArrayList<Float> redPockets = new ArrayList<>();
        this.setMoney(this.getMoney() - totalMoney);

        if (isRandom) {
            Random random = new Random();
            ArrayList<Float> percentage = new ArrayList<>();
            // 将所有人抽到的0.0~1.0之间的比率存储到percentage中
            for (int i = 0; i < count; i++) {
                percentage.add(random.nextFloat());
            }
            // 计算percentage总和,再计算每个比率的占比
            float totalPercentage = sum(percentage);
            // 将这个占比乘以totalMoney算出每个人抽到了多少
            for (int i = 0; i < count; i++) {
                redPockets.add(percentage.get(i) / totalPercentage * totalMoney);
            }
        } else {
            float average = totalMoney / count;
            for (int i = 0; i < count; i++) {
                redPockets.add(average);
            }
        }

        return redPockets;
    }

    private boolean hasEnoughMoney(float totalMoney) {
        if (this.getMoney() >= totalMoney) {
            return true;
        }
        return false;
    }

    public float sum(ArrayList<Float> percentage) {
        float totalPercentage = 0.0F;
        for (int i = 0; i < percentage.size(); i++) {
            totalPercentage += percentage.get(i);
        }
        return totalPercentage;
    }
}
Manager.java
package redPocket;

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

public class Member extends User {

    public Member() {
    }

    public Member(String name, float money) {
        super(name, money);
    }

    public void receive(ArrayList<Float> redPockets) {
        // 在列表中随机抽取一位
        int index = new Random().nextInt(redPockets.size());
        float money = redPockets.get(index);
        // 将被抽到的索引剔除
        redPockets.remove(index);
        System.out.println(this.getName() + "抽到了" + money);
        this.setMoney(this.getMoney() + money);
    }
}
Member.java
package redPocket;

import java.util.ArrayList;

public class Demo {
    public static void main(String[] args) {
        Manager m = new Manager("Manager", 1000.0F);
        Member m1 = new Member("Member1", 100.0F);
        Member m2 = new Member("Member2", 45.56F);

        ArrayList<Float> redPockets = m.send(888.88F, 8, true);

        System.out.println(redPockets);

        m1.receive(redPockets);
        m2.receive(redPockets);

        System.out.println(redPockets);

        System.out.println(m);
        System.out.println(m1);
        System.out.println(m2);
    }
}
Demo.java
01-10 03:24