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
);
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();
one
.show();
two
.show();
three
.show();
ArrayList
<Integer> redList
= manager
.send(20,3);
one
.receive(redList
);
two
.receive(redList
);
three
.receive(redList
);
manager
.show();
one
.show();
two
.show();
three
.show();
}
}
转载请注明原文地址: https://mac.8miu.com/read-499088.html