java继承实现发红包案例
程序员文章站
2022-11-30 15:24:17
发红包案例群住发普通红包,某群有多个成员,群住给成员发红包,普通红包的规则:群主的一笔金额,从余额中扣除,平分成n等分,让成员领取。成员领取红包后,保存到成员余额中发红包的逻辑:返回值类型:ArrayList方法名称:send参数列表:总共多少钱:int totalMoney分成多少份:int countpublic ArrayList send(int totalMoney, int count){}收红包的逻辑:返回类型:void方法名称:recieve参数名...
发红包案例
群住发普通红包,某群有多个成员,群住给成员发红包,普通红包的规则:
- 群主的一笔金额,从余额中扣除,平分成n等分,让成员领取。
- 成员领取红包后,保存到成员余额中
发红包的逻辑:
- 返回值类型:ArrayList
- 方法名称:send
- 参数列表:
- 总共多少钱:int totalMoney
- 分成多少份:int count
- public ArrayList send(int totalMoney, int count){}
收红包的逻辑:
- 返回类型:void
- 方法名称:recieve
- 参数名称:ArrayList
- public void recieve(Arraylist list){}
父类
User类拥有name和money属性,以及一个展示的方法
package demo.demo08;
public class User {
private String name;
private int money;
public void setName(String name) {
this.name = name;
}
public void setMoney(int money) {
this.money = money;
}
public String getName() {
return name;
}
public void show(){
System.out.println("My name is:"+name+",My money is:"+money);
}
public int getMoney() {
return money;
}
public User(String name, int money) {
this.name = name;
this.money = money;
}
public User() {
}
}
子类Manager
继承父类,拥有自己的方法send
package demo.demo08;
import java.util.ArrayList;
public class Manager extends User {
public Manager(String name, int money) {
super(name, money);
}
public Manager() {
}
public ArrayList<Integer> send(int totalMoney, int count) {
ArrayList<Integer> list = new ArrayList<Integer>();
int leftMoney = super.getMoney();
if(leftMoney<totalMoney){
System.out.println("余额不足!");
return list;
}
super.setMoney(leftMoney-totalMoney);
int avg = totalMoney / count;
int mod = totalMoney % count;
for (int i = 0; i < count-1; i++) {
list.add(avg);
}
int last = avg + mod;
list.add(last);
return list;
}
}
子类Member
继承父类,拥有自己的方法recieve
package demo.demo08;
import java.util.ArrayList;
import java.util.Random;
public class Member extends User {
public Member(String name, int money) {
super(name, money);
}
public Member() {
}
public void recieve(ArrayList<Integer> list){
int index = new Random().nextInt(list.size());
int delete = list.remove(index);
int money = super.getMoney();
super.setMoney(money+delete);
}
}
业务类发收红包的实现
package demo.demo08;
import java.util.ArrayList;
/*
群住发普通红包,某群有多个成员,群住给成员发红包,普通红包的规则:
- 群主的一笔金额,从余额中扣除,平分成n等分,让成员领取。
- 成员领取红包后,保存到成员余额中
发红包的逻辑:
- 返回值类型:ArrayList<Integer>
- 方法名称:send
- 参数列表:
- 总共多少钱:int totalMoney
- 分成多少份:int count
- public ArrayList<Integer> send(int totalMoney, int count){}
收红包的逻辑:
- 返回类型:void
- 方法名称:recieve
- 参数名称:ArrayList<Integet>
- public void recieve(Arraylist<Integer> list){}
*/
public class LuckMoney {
public static void main(String[] args) {
Manager hero = new Manager("hero", 100);
Member mps = new Member("mps", 0);
Member mps1 = new Member("mps1", 0);
Member mps2 = new Member("mps2", 0);
Member mps3 = new Member("mps3", 0);
Member mps4 = new Member("mps4", 0);
Member mps5 = new Member("mps5", 0);
System.out.println("++++++++++++++++");
hero.show();
mps.show();
mps1.show();
mps2.show();
mps3.show();
mps4.show();
mps5.show();
ArrayList<Integer> send = hero.send(20, 6);
mps.recieve(send);
mps1.recieve(send);
mps2.recieve(send);
mps3.recieve(send);
mps4.recieve(send);
mps5.recieve(send);
System.out.println("++++++++++++++++");
hero.show();
mps.show();
mps1.show();
mps2.show();
mps3.show();
mps4.show();
mps5.show();
}
}
本文地址:https://blog.csdn.net/heromps/article/details/107272303