Java实现群主发手气红包小demo

Java实现群主发手气红包小demo

木来 木来

用户类

package com.shihsf.day2;

public class User {
    //用户姓名    private String name;
    //用户余额    private double leftMoney;


    public User(){

    }
    public void show(){
        System.out.println("姓名为:"+name+"——"+"余额为:"+leftMoney);
    }

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

    public String getName() {
        return name;
    }

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

    public double getLeftMoney() {
        return leftMoney;
    }

    public void setLeftMoney(double leftMoney) {
        this.leftMoney = leftMoney;
    }
}

群主类

package com.shihsf.day2;

import com.shihsf.com.shihsf.utils.RandomCustom;

import java.util.ArrayList;

/** * 群主类 */public class Manager extends User {


    //无参数构造方法    public Manager() {
    }

    //有参构造方法    public Manager(String name, double leftMoney) {
        super(name, leftMoney);
    }

    //群主发手气红包方法    public ArrayList<Double> send(double totalMoney, int count) {
        //创建红包list集合        ArrayList<Double> arrayList = new ArrayList<>();
        //首先获取群主有多少钱        double leftMoney = super.getLeftMoney();
        //判断群主的余额是否能够发红包        if (leftMoney < totalMoney) {
            System.out.println("群主金额不足!");
            return null;
        }

        //扣除群主红包金额        super.setLeftMoney(leftMoney - totalMoney);

        //获取红包平均值,并进行格式化为0.0,例如10.4元        double avg = RandomCustom.FormatDoubleRandom("0.0", totalMoney / count);
        //创建一个红包临时变量,存储剩余红包金额        double totalMoneyTemp = RandomCustom.FormatDoubleRandom("0.0", totalMoney);


        for (int i = 0; i < count - 1; i++) {
            //获取指定范围的,低于平均值的红包金额            double randomMoney = RandomCustom.createDoubleRandom(avg);
            //获取剩余红包金额            totalMoneyTemp -= randomMoney;

            arrayList.add(randomMoney);
        }

        arrayList.add(totalMoneyTemp);


        return arrayList;
    }


}

群成员类

package com.shihsf.day2;

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

public class Member extends User {

    public Member() {
    }

    public Member(String name, int leftMoney) {
        super(name, leftMoney);
    }


    public void reserve(ArrayList<Double> arrayList) {


        //随机获得一个红包索引        int index = new Random().nextInt(arrayList.size());
        //获取红包金额        Double delta = arrayList.remove(index);
        //计算群成员金额总数        super.setLeftMoney(super.getLeftMoney() + delta);


    }


}

工具类

package com.shihsf.com.shihsf.utils;

import java.text.DecimalFormat;

public class RandomCustom {


    //创建指定范围的浮点随机数    public static double createDoubleRandom(double max) {
        //根据最大范围生成范围浮点随机数        double randomNum = Math.random() * max;

        return FormatDoubleRandom("0.00", randomNum);
    }

    //对浮点随机数进行指定格式的格式化    public static double FormatDoubleRandom(String style, double num) {
        DecimalFormat df = new DecimalFormat();
        // 将格式应用于格式化器        df.applyPattern(style);
        //System.out.println("采用style: " + style + "格式化之后: " + df.format(data));        return Double.parseDouble(df.format(num));
    }


}

测试类

package com.shihsf.day2;

import java.util.ArrayList;
import java.util.Scanner;

public class Test {


    public static void main(String[] args) {


        //创建群主对象        Manager manager = new Manager("群主", 500);
        Member member1 = new Member("成员A", 0);
        Member member2 = new Member("成员B", 0);
        Member member3 = new Member("成员c", 0);
        Member member4 = new Member("成员d", 0);
        Member member5 = new Member("成员e", 0);

        manager.show();
        member1.show();
        member2.show();
        member3.show();
        member4.show();
        member5.show();

        System.out.println("===============群主开始发红包================");
        System.out.println("想发多少钱红包?");
        double totalMoney=new Scanner(System.in).nextDouble();
        ArrayList<Double> redList = manager.send(totalMoney, 5);
        System.out.println("群主发送红包金额:"+totalMoney);
        System.out.println("===============成员开始抢红包================");
        if (redList != null) {
            ArrayList<Member> members = new ArrayList<>();
            members.add(member1);
            members.add(member2);
            members.add(member3);
            members.add(member4);
            members.add(member5);


            for (int i = 0; i < members.size(); i++) {
                members.get(i).reserve(redList);
                members.get(i).show();
            }

        }


    }


}
0 条评论