其他分享
首页 > 其他分享> > 发红包案例

发红包案例

作者:互联网

发红包案例

视图:

 

 

发红包的逻辑,三要素:

返回值:ArrayList<Integer>

方法名称:send

参数列表:1.总共发多少钱  int totalMoney

      2.分成多少份     int count

  public ArrayList<Integer> send(int totalMoney , int count){
    //.....    
}

 

收红包的逻辑,三要素:

返回值类型:void

方法名称:receive

参数列表:ArrayList<Integer>

  public void receive(ArrayList<Integer> list){
    //........
}

 

package com.vip.Vip_0017;
//父类
public class User {
    private String name;//姓名
    private int money;//余额,也就是当前用户拥有的钱数


    //展示一下当前用户有多少钱
    public void show(){
        System.out.println("我叫:" + name + "余额为:" + money);
    }

    public User() {
    }

    public User(String name, int money) {
        this.name = name;
        this.money = 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 com.vip.Vip_0017;

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 tota , int count){

        //需要一个集合,用来存储若干个红包的金额
        ArrayList<Integer> arrayList = new ArrayList<>();

        //首先看一下群主有多少钱
        int money = super.getMoney();
        if (tota > money){
            System.out.println("余额不足");
            return arrayList;//返回空集合
        }
        //扣钱,重新设置余额
        super.setMoney(money - tota);

        //发红包需要平均拆开成为count份
        int avg = tota / count;
        int mod = tota  % count;//余数,也就是甩下头的零头

        //除不开的零头,包在最后一个红包当中
        //下面把红包一个一个放到集合当中
        for (int i = 0; i < count - 1 ; i++) {
            arrayList.add(avg);
        }

        //最后一个红包
        int last = avg + mod;
        arrayList.add(last);

        return arrayList;
    }
}
package com.vip.Vip_0017;

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());
        //根据索引,从集合当中删除,并且得到删除的红包,给我自己
        int delta = list.remove(index);
        //当前成员自己本来有多少钱
        int money = super.getMoney();
        //加法,并且重新设置回去
        super.setMoney( money + delta);
    }

}
package com.vip.Vip_0017;

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();
        System.out.println("===============");
        //群主总共发20块钱,分成3个红包
        ArrayList<Integer> arrayList = manager.send(20,3);
        //三个普通成员收红包
        one.receive(arrayList);
        two.receive(arrayList);
        three.receive(arrayList);
        //重新打印成员属性
        manager.show();
    // one.show(); two.show(); three.show(); } }

运行结果:

 

标签:name,show,int,money,ArrayList,案例,发红包,public
来源: https://www.cnblogs.com/aqhk/p/16442634.html