标签:get system 并且 pack packet setname void alt manager
public class User {
private String name;
private int money;
public User() {
}
public User(String name, int money) {
this.name = name;
this.money = money;
}
//展示一下用户有多少钱
public void show(){
System.out.println("我叫:"+ name + ",我有多少钱:"+ money);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
import java.util.ArrayList;
//群主的类
public class Manager extends User {
public Manager() {
}
public Manager(String name, int 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;
}
}
import java.util.ArrayList;
import java.util.Random;
//普通成员
public class Member extends User{
public Member() {
}
public Member(String name, int money) {
super(name, money);
}
public void receive(ArrayList<Integer> list) {
//从多个红包中随便抽取一个给我自己
//随机获取一个集合当中的索引编号
int index = new Random().nextInt(list.size());
//根据索引,从集合当中删除,并且得到被删除的红包,给自己
int detal = list.remove(index);
//自己本来有多少钱
int money = super.getMoney();
//加法,并且重新设置回去
super.setMoney(money + detal);
}
}
import java.util.ArrayList;
public class MainRedPacket {
public static void main(String[] args) {
Manager manager = new Manager("群主",100);
Member one = new Member("成员A",0);
Member two = new Member("成员B",0);
Member three = new Member("成员C",0);
manager.show();//100
one.show();//0
two.show();//0
three.show();//0
//群众总共发20快钱,分成3个红包
ArrayList<Integer> redList = manager.send(20,3);
//3个成员收红包
one.receive(redList);
two.receive(redList);
three.receive(redList);
manager.show();//80
one.show();//6
two.show();//8
three.show();//6
}
}
标签:get system 并且 pack packet setname void alt manager
原文地址:https://www.cnblogs.com/deer-cen/p/12236872.html