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:"+name+" Money:"+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;
}
}
Manage类
import java.util.ArrayList;
public class Manager extends User{
public Manager(String name, int money) {
super(name, money);
}
public Manager() {
super();
}
//需要一个集合用来存储发红包的个数以及各个金额
public ArrayList<Integer> List(int totalmoney,int count){//发的钱
ArrayList<Integer> list = new ArrayList<>();
int leftmoney = super.getMoney();//群主总共的钱
if(leftmoney < totalmoney) return list;//群主没那么多钱
int averagemoney = totalmoney/count;//平均每个人
int modemoney = totalmoney%count;//除不尽的余数加在list最后一个
for(int i = 0 ; i < count-1 ; i++){
list.add(averagemoney);
}
super.setMoney(leftmoney - totalmoney);//群主剩的钱
list.add(modemoney+averagemoney);
return list;//返回红包集合
}
}
Member类
import java.util.ArrayList;
import java.util.Random;
public class Member extends User{
public Member() {
super();
}
public Member(String name, int money) {
super(name, money);
}
public void receive(ArrayList<Integer> list){
int random = new Random().nextInt(list.size());//随即索引
int num = list.remove(random);//将这个数去除,并且取出
int money = super.getMoney()+num;//把钱加上
super.setMoney(money);//更新前的金额
}
}
调用
import java.util.ArrayList;
import java.util.Scanner;
public class Using {
public static void main(String[] args) {
Member A= new Member("贤哥",0);
Member B = new Member("超哥",0);
Member C = new Member("小哥",0);
Manager qun = new Manager("群主",10000);
System.out.println("原来的钱");
qun.show();
A.show();
B.show();
C.show();
System.out.println("================");
System.out.println("你想发多少");
int F = new Scanner(System.in).nextInt();
ArrayList<Integer> list = qun.List(F,3);
A.receive(list);
B.receive(list);
C.receive(list);
qun.show();
A.show();
B.show();
C.show();
}
}