• 发红包的案例


    package cn.hope.day09.demo09;

    /**
    * @author newcityman
    * @date 2019/7/11 - 22:57
    */
    public class User {
    private String name;
    private Integer money;

    public User() {
    }

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

    public String getName() {
    return name;
    }

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

    public Integer getMoney() {
    return money;
    }

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

    public void show(){
    System.out.println("我是:"+this.name+",我的余额是:"+this.money);
    }
    }

    package cn.hope.day09.demo09;

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

    /**
    * @author newcityman
    * @date 2019/7/11 - 23:24
    */
    public class Member extends User {
    public Member() {
    }

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

    public void receive(ArrayList<Integer> list){
    //从红包中随机抽取一个,给自己
    //随机抽取一个集合中的索引编号
    int index= new Random().nextInt(list.size());
    //根据索引,从集合中删除,并且得到被删除的红包,给自己
    int delta = list.remove(index);
    //当前成员目前本来有多少钱
    int money = super.getMoney();
    //收取红包,存进腰包
    super.setMoney(money+delta);

    }
    }
    package cn.hope.day09.demo09;

    import java.util.ArrayList;

    /**
    * @author newcityman
    * @date 2019/7/11 - 23:00
    */
    //群主的类
    public class Manager extends User {

    public Manager() {
    }

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

    public ArrayList<Integer> send(int totalMoney,int count) {
    //首先需要创建一个集合,用来存储若干个红包
    ArrayList<Integer> redList = new ArrayList<>();
    //首先看看群主有多少钱
    int leftMoney = super.getMoney();
    if (totalMoney > leftMoney) {
    System.out.println("余额不足,请充值");
    return redList;
    }
    //扣钱,其实就是重新设置余额
    super.setMoney(leftMoney - totalMoney);
    //红包需要平均拆分成count份
    int avg = totalMoney / count;
    int mod = totalMoney % count;
    //除不开的零头,包在最后一个红包当中
    //下面把红包放到集合中去
    for (int i = 0; i < count - 1; i++) {
    redList.add(avg);
    }
    //最后一个红包
    int last = avg + mod;
    redList.add(last);
    return redList;
    }
    }
    
    
    package cn.hope.day09.demo09;


    import java.util.ArrayList;

    /**
    * @author newcityman
    * @date 2019/7/11 - 23:36
    */
    public class MainRedPacket {
    public static void main(String[] args) {
    Manager m = new Manager("群主",100);
    Member one = new Member("zmy",0);
    Member two = new Member("zmk",0);
    Member three = new Member("zmh",0);

    m.show();
    one.show();
    two.show();
    three.show();
    System.out.println("========================");
    ArrayList<Integer> list = m.send(20,3);
    one.receive(list);
    two.receive(list);
    three.receive(list);

    m.show();
    one.show();
    two.show();
    three.show();





    }


    }


     
  • 相关阅读:
    使用git将本地代码上传到gitee【码云】
    .net设置实例字段在请求参数中不展示
    Springboot 操作Elasticsearch 方式一 【spring-data-elasticsearch】
    Java工具类 (3)------>WordUtils------>利用Poi根据模板生成新的word文档
    Java工具类 (2)------>TreeUtils------>树形结构生成类
    Java工具类 (1)------>IPUtils------>获取用户登录IP地址
    Idea安装MyBatisCodeHelper-Pro插件破解版以及去除mybatis的mapper.xml文件背景颜色
    Python 线程队列 LifoQueue – LIFO
    Python 线程队列 Queue – FIFO
    Python 线程障碍对象 Barrier
  • 原文地址:https://www.cnblogs.com/newcityboy/p/11180094.html
Copyright © 2020-2023  润新知