为了讲明白继承和super、this关键字,群主发了20块钱群红包

本文分享自华为云社区《群主发红包带你深入了解继承和super、this关键字》 , 作者:共饮一杯无。
需求群主发随机红包或者普通红包 。某群有多名成员 , 群主给成员发普通红包 。
随机红包规则:

  1. 群主的一笔金额 , 从群主余额中扣除 , 随机分成n等份 , 让成员领取 。
  2. 成员领取红包后 , 保存到成员余额中 。
普通红包的规则:
  1. 群主的一笔金额 , 从群主余额中扣除 , 平均分成n等份 , 让成员领取 。
  2. 成员领取红包后 , 保存到成员余额中 。
案例分析案例分析 , 可以得出如下继承关系:
为了讲明白继承和super、this关键字,群主发了20块钱群红包

文章插图
 
案例代码实现定义用户类/** * 用户类 * @author zjq */public class User {/*** 姓名*/private String name;/*** 余额 , 也就是当前用户拥有的钱数*/private Integer money;public User() {}public User(String name, Integer money) {this.name = name;this.money = money;}// 展示一下当前用户有多少钱public void show() {System.out.println("我是" + name + " , 我有多少钱:" + this.fenToYuan(String.valueOf(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;}/*** 分转元* @param amount* @return*/publicString fenToYuan(String amount){NumberFormat format = NumberFormat.getInstance();try{Number number = format.parse(amount);double temp = number.doubleValue() / 100.0;format.setGroupingUsed(false);format.setMaximumFractionDigits(2);amount = format.format(temp);} catch (ParseException e){e.printStackTrace();}return amount;}}定义群主类package com.zjq.JAVAbase.base09.demo14;import org.Apache.commons.lang3.RandomUtils;import java.util.ArrayList;/** * 群主的类 * @author zjq */public class Manager extends User {/*** 收到单个红包最大值*/private static final int MAX_AMOUNT = 20000;public Manager() {}public Manager(String name, int money) {// 通过super 调用父类构造方法super(name, money);}/*** 发红包* @param totalMoney 红包总金额(单位分)* @param count 发包个数* @param type 发包类型(0、随机红包 , 1、定额红包)* @return 红包集合* @throws Exception*/public ArrayList<Integer> send(Integer totalMoney, int count,int type) throws Exception {// 首先需要一个集合 , 用来存储若干个红包的金额ArrayList<Integer> redList = new ArrayList<>(count);// 首先看一下群主自己有多少钱Integer leftMoney = super.getMoney(); // 群主当前余额if (totalMoney > leftMoney) {System.out.println("余额不足");return redList; // 返回空集合}// 扣钱 , 其实就是重新设置余额super.setMoney(leftMoney - totalMoney);if (count == 1) {redList.add(totalMoney);return redList;}switch (type) {case 0:// 默认分配1分至每一位for (int i = 0; i < count; i++) {redList.add(1);}int surplus_currency = totalMoney - redList.size(),// 剩余金额数surplus_number = redList.size();// 剩余需追加的数量for (int i = 0; i < redList.size(); i++) {// 没值可以追加了if (new Integer(0).equals(surplus_currency)) {break;}// (总数-(总包-i)*最小值) / (总包 - i) 随机安全值算法int safe_total = (int)Math.floor((totalMoney - (count - i)) / (count - i));if (new Integer(0).equals(safe_total)) {// 随机值不能为0safe_total = 1;}// 该次随机值int randomint = surplus_currency >= safe_total - 1 ? safe_total : surplus_currency + 1;// 下次可能最大能剩余值int nextMax_currency = (MAX_AMOUNT - 1) * (surplus_number - 1);// 最小的随机数剩余金额-剩余最大随机的总数(不含这一次)int minRandom = surplus_currency -nextMax_currency;if (minRandom < 0) {minRandom = 0;}// 规避一些特殊情况,每个接近2000或1时会发生boolean must = (surplus_currency - count * MAX_AMOUNT <= 2 && surplus_currency - count * MAX_AMOUNT >= 0)/*|| surplus_currency < packet_number * 2*/;// 控制安全随机值随机安全值不能大于最大限制 , 并且不能小于最小限 制if (safe_total < minRandom || safe_total > MAX_AMOUNT || must) {safe_total = MAX_AMOUNT;// 该次随机值randomint = surplus_currency >= safe_total - 1 ? safe_total : surplus_currency + 1;// 下次可能最大能剩余值nextMax_currency = (randomint - 1) * (surplus_number - 1);// 最小的随机数剩余金额-剩余最大随机的总数(不含这一次)minRandom = surplus_currency -nextMax_currency;if (minRandom < 0) {minRandom = 0;}}// 下一次最大的随机值int nextMaxRandomInt = nextMax_currency - (surplus_currency - (randomint - 1));Integer maxRandom = nextMaxRandomInt <= 0 ? nextMaxRandomInt + randomint: null;// 能随机剩余的金额- 最大随机数 >最大随机数* 剩余数量boolean canRandom = surplus_currency - (randomint - 1) > nextMax_currency ||nextMaxRandomInt > (randomint - 1)|| !new Integer(0).equals(minRandom);int addNumber; // 追加的金额if (canRandom && !new Integer(randomint).equals(minRandom+1) && !(new Integer(randomint).equals(minRandom) && new Integer(safe_total).equals(minRandom)) ) {addNumber = myRandom(minRandom, maxRandom == null ? randomint : maxRandom- 1);}else {addNumber = randomint - 1;}redList.set(i,redList.get(i) + addNumber);surplus_currency -= addNumber;surplus_number--;}break;case 1:// 定额红包校验redList = new ArrayList<>(count);for (int i = 0; i <count; i++) {//定额红包要是不能整除会有问题 , 正常实现应该是输入单个红包金额和总数直接就能计算redList.add(totalMoney/count);}break;default:throw new Exception("类型错误!");}System.out.println("我是" + this.getName() + "我发了"+fenToYuan(String.valueOf(totalMoney))+"元红包"+" , 我现在有多少钱:" + fenToYuan(String.valueOf(this.getMoney()))+"元");return redList;}/*** 生成随机金额* @param min* @param randomint* @return*/public static int myRandom(int min,int randomint) {if (min == 0) {returnRandomUtils.nextInt(0,randomint);}else {int nextInt = RandomUtils.nextInt(min,randomint - min);return nextInt + min;}}}


推荐阅读