java实训任务 需求:来实现群发红包的案例 群主在群里发红包,其他的成员(3个)来抢红包 发红包规则: 群主使用自己的余额,暂时规定群主不允许抢, 红包平均分配成3个 100/3 不能整除 整数 余数 99/3 整除 成员抢随机红包 注意:发红包的金额和自己的余额比较,红包大于余额是不能发的 分析: 对象: 群主 : 自己的行为 发红包 成员 : 抢红包 共同的属性:姓名 余额-int xxx 抢了 10 元 余额10 +10 =20; 共同的行为:
Groupware.java
package cn.shichuang.test;
public class Groupware {
private String name;
private int count;
public Groupware(String name, int count) {
this.name = name;
this.count = count;
}
public void sent(int a){
this.count=this.count-a;
}
public void add(int a){
this.count=this.count+a;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
Crew.java
package cn.shichuang.test;
public class Crew extends Groupware {
public Crew(String name, int count) {
super(name, count);
}
}
Cons.java
package cn.shichuang.test;
public class Cons {
private String name;
private String bdf;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBdf() {
return bdf;
}
public void setBdf(String bdf) {
this.bdf = bdf;
}
}
Demo.java 测试类编写
package cn.shichuang.test;
import java.util.*;
public class Demo {
public static void Give(ArrayList<Cons> list,Groupware gp,Crew cr1,Crew cr2,Crew cr3){
Scanner x = new Scanner(System.in);
Random r = new Random();
Cons cs = new Cons();
System.out.println("请输入要发送的金额:");
int a = x.nextInt();
int a1 = gp.getCount();
if(a1<a){
System.out.println("余额不足,请重新选择发送的金额:");
a = x.nextInt();
}
gp.sent(a);
cs.setName(gp.getName());
String s = "发出"+Integer.toString(a);
cs.setBdf(s);
list.add(cs);
System.out.println("红包已经发出====="+cr1.getName()+" "+cr2.getName()+" "+cr3.getName()+"在线准备就绪");
int c1 = r.nextInt(a);
Cons cs1 = new Cons();
System.out.println(cr1.getName()+"抢到的金额为"+c1);
cr1.add(c1);
cs1.setName(cr1.getName());
String s1 = "抢到"+Integer.toString(c1);
cs1.setBdf(s1);
list.add(cs1);
a-=c1;
Cons cs2 = new Cons();
cs2.setName(cr1.getName());
c1 = r.nextInt(a);
cr2.add(c1);
String s2 = "抢到"+Integer.toString(c1);
System.out.println(cr2.getName()+"抢到的金额为"+c1);
cs2.setBdf(s2);
list.add(cs2);
a-=c1;
cr3.add(a);
Cons cs3 = new Cons();
System.out.println(cr3.getName()+"抢到的金额为"+a);
cs3.setName(cr3.getName());
String s3 = "抢到"+Integer.toString(a);
cs3.setBdf(s3);
list.add(cs3);
}
public static void Paly(){
System.out.println("你想看我变成秃头嘛——————————-");
}
public static void findAll(Groupware gp,Crew cr1,Crew cr2,Crew cr3){
System.out.println(gp.getName()+"的余额为:"+gp.getCount());
System.out.println(cr1.getName()+"的余额为:"+cr1.getCount());
System.out.println(cr2.getName()+"的余额为:"+cr2.getCount());
System.out.println(cr3.getName()+"的余额为:"+cr3.getCount());
}
public static void findList(ArrayList<Cons> list){
System.out.println("姓名\t出纳\t");
for(int i=0;i<list.size();i++){
Cons cs = list.get(i);
System.out.println(cs.getName()+"\t"+cs.getBdf()+"\t");
}
}
public static void hbMean(){
Scanner x = new Scanner(System.in);
ArrayList<Cons> list = new ArrayList();
Groupware gp = new Groupware("sst",1000);
Crew cr1 = new Crew("cyy",0);
Crew cr2 = new Crew("zmh",0);
Crew cr3 = new Crew("zgn",0);
while(true){
System.out.println("=========================================");
System.out.println("1 群主开始发红包");
System.out.println("2 群成员开始发红包");
System.out.println("3 查看成员钱包");
System.out.println("4 查看抢红包记录");
System.out.println("0 退出--------");
System.out.println("请选择操作:");
int wd = x.nextInt();
int b=0;
switch (wd){
case 0:
b=1;
break;
case 1:
Give(list,gp,cr1,cr2,cr3);
break;
case 2:
Paly();
break;
case 3:
findAll(gp,cr1,cr2,cr3);
break;
case 4:
findList(list);
}
if(b==1){
break;
}
}
}
public static void main(String[] args) {
hbMean();
}
}
|