编程语言
首页 > 编程语言> > JAVA发红包案例(主要学习对继承的运用)

JAVA发红包案例(主要学习对继承的运用)

作者:互联网

父类:

package com.java.day10;
// 用户类:群主和成员类的父类
public class User {
    private String name;
    private int balance;

    public User() {
    }

    public User(String name, int balance) {
        this.name = name;
        this.balance = balance;
    }
    // 显示余额方法
    public void showBalance(){
        System.out.println("我叫:"+name+","+"我有多少钱:"+balance);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }
}

群主类:

package com.java.day10;

import java.util.ArrayList;
// 群主类
public class GroupOwner extends User{
    public GroupOwner() {
    }

    public GroupOwner(String name, int balance) {
        super(name, balance);
    }
    // 发红包
    public ArrayList<Integer> send(int totalMoney, int count){
        ArrayList<Integer> arrayList = new ArrayList<>();
        // 先看自己的余额是否大于所发金额
        int wallet = super.getBalance();  // 群主当前余额
        if (totalMoney > wallet) {
            System.out.println("余额不足");
            return arrayList;  // 返回空集合
        }
        // 扣钱,重新设置余额
        super.setBalance(wallet-totalMoney);

        // 拆分红包,每份多少钱
        // 平均数
        int avg = totalMoney / count;
        // 余数,余数放到最后一个红包里面
        int mod = totalMoney % count;
        for (int i = 0; i < count; i++) {
            // 如果是最后一个红包,则把平均数和余数加起来,放到最后一个数组里面
            if (i == count-1) {
                arrayList.add(avg+mod);
            }
            else {
                arrayList.add(avg);
            }
        }
        return arrayList;
    }
}

群成员类:

package com.java.day10;

import java.util.ArrayList;
import java.util.Random;
// 成员类
public class Member extends User{
    public Member() {
    }

    public Member(String name, int balance) {
        super(name, balance);
    }

    // 收红包
    public void receive(ArrayList<Integer> arrayList){
        Random random = new Random();
        // 判断集合为空
        if (arrayList.size() != 0) {
            // 随机抽取红包
            int index = random.nextInt(arrayList.size());
            // 得到集合中的红包
            int redPacket = arrayList.remove(index);
            // 当前成员本来的钱
            int money = super.getBalance();
            // 原有的钱加上抽红包的钱
            super.setBalance(money+redPacket);
        }
        else {
            System.out.println("集合为空");
        }

    }
}

主方法调用:

package com.java.day10;

import java.util.ArrayList;
// 主方法
public class MainRadPacket {
    public static void main(String[] args) {
        GroupOwner groupOwner = new GroupOwner("土豪",100);
        Member memberOne = new Member("成员1",0);
        Member memberTwo = new Member("成员2",0);
        Member memberThree = new Member("成员3",0);
        // 发之前显示余额
        groupOwner.showBalance();
        memberOne.showBalance();
        memberTwo.showBalance();
        memberThree.showBalance();
        System.out.println("===================================================");
        // 群主发红包总共20元,发3个
        ArrayList<Integer> list = groupOwner.send(20,3);
        // 接收红包
        groupOwner.showBalance();
        memberOne.receive(list);
        memberTwo.receive(list);
        memberThree.receive(list);
        // 接收后,显示余额
        memberOne.showBalance();
        memberTwo.showBalance();
        memberThree.showBalance();
    }
}

运行结果:

 

 

标签:JAVA,name,int,arrayList,Member,案例,发红包,balance,public
来源: https://www.cnblogs.com/xiamaojjie/p/14965163.html