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();
}
}