(java总结)发红包案例实现

mac2024-12-26  13

package Demo14; 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; } } package Demo14; 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; } } package Demo14; 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()); //根据索引,从集合当中删除,并且得到被删除的红包,给自己 Integer delta = list.remove(index); //当前成员自己本来有多少钱 int money = super.getMoney(); //加法,重新加回去 super.setMoney(money + delta); } } package Demo14; 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 ArrayList<Integer> redList = manager.send(20,3); one.receive(redList); two.receive(redList); three.receive(redList); manager.show();//100-20=80 // 随机分给三个人 one.show(); two.show(); three.show(); } }
最新回复(0)