java 红包案例 小白 不是最好方案 添加成员需要new 还得调收红包的方法 不调不会收
//父类 群类
public class Group {
private String name;//名字
private String position;//职位
private double balance;//余额
static int memberNum;//人数
//构造方法
public Group() {
}
public Group(String name, String position, double balance) {
this.name = name;
this.position = position;
this.balance = balance;
}
//getXxx() setXxx()
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
//群主类 继承群类
import java.util.ArrayList;
import java.util.Random;
public class GroupOwner extends Group {
static ArrayList<Double> arrPart = new ArrayList<>();//把发出来的随机红包金额 赋给arrPart数组
//构造方法
public GroupOwner() {
}
public GroupOwner(String name, String position, double balance) {
this.setName(name);
this.setPosition(position);
this.setBalance(balance);
}
//发红包方法 参数有钱和要发几份
public void giveRedEnvelopes(double money, int numPeople) {
//发的钱从群主余额扣除
setBalance(getBalance() - money);
//初始化
arrPart = new ArrayList<>();
double aceilingName = 0;//不能超出传来的money总数
for (int i = 0; i < numPeople; i++) {//用份数当循环条件 可以人少份数多
Random random = new Random();
//随机多少钱 随机条件是 发的钱数/份数*2 公平点(随机)
double nextInt = random.nextDouble() * (money / numPeople*2);
if (aceilingName >= money) {//发的份数的总钱数大于总钱数 退出
break;
} else if (i == numPeople - 1) {//最后一份 把剩下的全拿走
arrPart.add(money - aceilingName);
} else {//其他随机
arrPart.add(nextInt);
}
aceilingName += nextInt;//叠加 发出去的份数 和钱数
}
System.out.println(getName() + "群主发了:" + money);
for (int i = 0; i < arrPart.size(); i++) {//遍历 随机红包金额的钱 数组 展示红包
if (i == arrPart.size()) {//防止越界
System.out.println("第" + i + "个红包为:" + arrPart.get(i));
break;
} else {
System.out.println("第" + (i + 1) + "个红包为:" + arrPart.get(i));//索引对应红包数,索引从0开始所以要+1
}
}
}
}
//成员类
import java.util.ArrayList;
import java.util.Random;
public class Member extends Group {
static ArrayList<Double> max = new ArrayList<>();//这个数组用来存储接收的红包钱数判断谁是手气王
static ArrayList<String> name = new ArrayList<>();//这个数组用来存储名字的
//构造方法
public Member() {
}
public Member(String name, String position, double balance) {
Group.memberNum++;//调一次全参构造群人数就加一
this.setName(name);
this.setPosition(position);
this.setBalance(balance);
}
//收红包方法
public void receiveLuckyMoney() {
double remove = 0;
//遍历 群主发的红包数组
for (int i = 0; i < GroupOwner.arrPart.size(); i++) {
//随即索引 取出 对应的红包
int nextInt = new Random().nextInt(GroupOwner.arrPart.size());
//取出 删除 返回值为:获取的钱数
remove = GroupOwner.arrPart.remove(nextInt);
//调一次就把获得的余额 和姓名 传给对应的数组
max.add(remove);
name.add(getName());
//领取后把钱加到自己的余额中
setBalance(getBalance() + remove);
System.out.println(getName() + "领取了:" + remove + "元" + "。现在余额为:" + getBalance() + "元");
break;
}
}
}
//测试 群 类
import java.util.Scanner;
public class TestGroup {
public static void main(String[] args) {
//构造群主
GroupOwner groupOwner = new GroupOwner("张三", "群主", 5000);
//构造成员
Member member01 = new Member("李四", "成员", 500);
Member member02 = new Member("王五", "成员", 700);
Member member03 = new Member("赵六", "成员", 888);
Member member04 = new Member("田七", "成员", 999);
System.out.println("群里人数为:" + Group.memberNum);
//发多少钱红包和个数
Scanner scanner = new Scanner(System.in);
System.out.println("输入发多少钱:");
double money = scanner.nextDouble();
System.out.println("发几份:");
int peopleNum = scanner.nextInt();
groupOwner.giveRedEnvelopes(money, peopleNum);
//收红包
member01.receiveLuckyMoney();
member02.receiveLuckyMoney();
member03.receiveLuckyMoney();
member04.receiveLuckyMoney();
//手气王
double temp = 0;//max 获得的红包钱数的中间变量
String maxName;//name 名字要随着红包变化而变化 就可以实现一一对应
//排序 降序 第一个索引就为最大值 名字和钱数一起变 所以就可以一一对应起来了
for (int i = 0; i < Member.max.size(); i++) {
for (int j = 0; j < Member.max.size() - 1; j++) {
if (Member.max.get(j) < Member.max.get(j + 1)) {
temp = Member.max.get(j);
Member.max.set(j, Member.max.get(j + 1));
Member.max.set(j + 1, temp);
//随着余额索引变化 而变化
maxName = Member.name.get(j);
Member.name.set(j, Member.name.get(j + 1));
Member.name.set(j + 1, maxName);
}
}
}
System.out.println("手气王为:" + Member.name.get(0) + " 抽取了:" + Member.max.get(0) + "元");
}
}