人员类
成员属性:名字
成员属性:余额
package todayHw.hw1;
public class Person {
private String name;
private double balance;
public Person() {
}
public Person(String name, double balance) {
this.name = name;
this.balance = balance;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
群主类
成员方法:发红包
package todayHw.hw1;
import java.util.ArrayList;
import java.util.Random;
public class Owner extends Person {
public Owner() {
}
public Owner(String name, double balance) {
super(name, balance);
}
/**
* 发普通红包
*
* @param al 红包队列
* @param num 人数
* @param money 每个多少钱
* @return al 红包队列
*/
public ArrayList<Double> money(ArrayList<Double> al, int num, double money) {
// 群主发普通红包
double totalMoney = num * money;
if (totalMoney > getBalance()) {
System.out.println("余额不足,囊中羞涩");
return null;
} else {
// 将红包装入红包队列中
for (int i = 0; i < num; i++) {
al.add(money);
}
System.out.println("红包容器");
for (Double aDouble : al) {
System.out.print(aDouble + " ");
}
setBalance(getBalance() - totalMoney);
System.out.println("群主的余额还剩: " + this.getBalance());
return al;
}
}
/**
* @param al 红包容器队列
* @param num 人数
* @param money 总钱数
* @return al 容器
*/
public ArrayList<Integer> money2(ArrayList<Integer> al, int num, double money) {
// 群主发手气红包
if (money > getBalance()) {
System.out.println("余额不足,囊中羞涩");
return null;
} else {
// 将钱随机装入红包队列中
// 第一个红包是100 的随机数 [1,100)
int v1 = (int)(Math.random() * (money-1))+1;
al.add(v1);
// 经过第一个随机后,还剩v2
int v2 = (int) money - v1;
// 第二个红包是 前面余额的随机数
int v3 = (int)(Math.random() * (v2-1))+1;
al.add(v3);
int v4 = v2 - v3;
al.add(v4);
// 打印红包容器
System.out.println("红包容器");
for (Integer integer : al) {
System.out.print(integer + " ");
}
// 群主的余额减去此处发的红包
setBalance(getBalance() - money);
System.out.println("群主的余额还剩: " + this.getBalance());
FindMax(al);
return al;
}
}
// 比较最大值
public void FindMax(ArrayList<Integer> al) {
int max = 0;
int index = 0;
for (int i = 0; i < al.size(); i++) {
if (max < al.get(i)) {
max = al.get(i);
index = i;
}
}
System.out.println("红包最大值是: " + max);
System.out.println("第" + (index + 1) + "个人的手气最佳");
}
}
成员类:
成员方法:收红包
package todayHw.hw1;
import java.util.ArrayList;
import java.util.Iterator;
public class member extends Person {
public member(String name, double balance) {
super(name, balance);
}
public void money(ArrayList<Double> al) {
// 用迭代器从容器队列中取元素,如果没有元素就不能取
// 创建一个迭代器对象
Iterator<Double> iterator = al.iterator();
// 如果容器队列中有元素,则 hasNext() 为 true
//while (iterator.hasNext()) {
// 容器中存储的是 Double 包装类数据,直接转换为 double 基本数据类型
Double temp = iterator.next();
// 取过值后,移除该元素
iterator.remove();
System.out.println("从红包中收到 " + temp + " 元钱");
// 包装类自动拆箱
setBalance(this.getBalance() + temp);
System.out.println("成员姓名:" + getName() + " 余额: " + getBalance());
// }
}
public void money2(ArrayList<Integer> al) {
Iterator<Integer> iterator = al.iterator();
while (iterator.hasNext()) {
double temp = iterator.next();
iterator.remove();
System.out.println("从红包中收到 " + temp + " 元钱");
setBalance(this.getBalance() + temp);
System.out.println("成员姓名:" + getName() + " 余额: " + getBalance());
}
}
}
测试类
package todayHw.hw1;
import java.util.ArrayList;
/**
* 综合 案例 : 群主发普通红包
* 群内有1个群主,多名成员 ,发普通红包均分
* 1.群主的一笔金额,从群主的余额总扣除,平均分成 N 等份,让成员领取
* 2.成员领取红包后,保存在成员余额中
*
*
* 拓展拼手气红包
* 1,输出手气最佳的成员
* 2.输出最后每位成员的余额
*/
public class mainProgram {
public static void main(String[] args) {
// 创建一个红包队列,保存群主发的红包钱,每个元素,就是一个红包
ArrayList<Double> al = new ArrayList<>();
// 创建一个红包队列,保存群主发的红包钱,每个元素,就是一个红包
ArrayList<Integer> al2 = new ArrayList<>();
// 群主属性有姓名和余额
Owner owner = new Owner("群主", 1000);
member m1 = new member("成员1",0);
member m2 = new member("成员2",0);
member m3 = new member("成员3",0);
System.out.println("发普通红包");
// 总共发3个,每个100 块钱,总共300块钱
// 此处是为了避免除不尽的情况
owner.money(al,3,100);
// 成员从队列中取红包,每取一个,容器元素减一
m1.money(al);
m2.money(al);
m3.money(al);
System.out.println("随机红包");
// 发三个红包,总共100块钱,每个人分到的都是整数
owner.money2(al2,3,100);
m1.money2(al2);
m2.money2(al2);
m3.money2(al2);
}
}