Java教程

Java继承应用——发红包

本文主要是介绍Java继承应用——发红包,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
  • 群主发普通红包。某群有多名成员,群主给成员发普通红包。普通红包的规则:
  1. 群主的一笔金额,从群主余额中扣除,平均分成n等份。,让成员领取。
  2. 成员领取红包后,保存到成员余额中。
    请根据描述,完成案例中所有类的定义以及指定类之间的继承关系,并完成发红包的操作。
  • 父类代码:
//系统用户
public class User {
    private String username;
    private int balance;

    public User(String username, int balance) {
        this.username = username;
        this.balance = balance;
    }

    //查看余额
    public void checkBalance() {
        System.out.println(username+"余额还有:"+balance);
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }
}
  • 群主类代码:
package com.zhou.day0825.redpacket;

import java.util.ArrayList;

//群主
public class GroupOwner extends User{

    public GroupOwner(String username,int balance) {
        super(username,balance);
    }

    //发红包
    //money:发送的红包金额     count:发送红包的个数
    public ArrayList<Integer> sengRedPacket(int money,int count) {
        //ArrayList只能指定存放引用类型的数字
        ArrayList<Integer> redPacket = null;
        //群主发红包 1.判断红包金额是否大于当前余额
        //           2.红包发出去去,充值当前余额
        if (money>getBalance()) {
            System.out.println("抱歉,余额不足");
            return redPacket;
        }
            redPacket = new ArrayList<Integer>();
            int avg = money/count;//将红包平均分成count份
            int mod = money%count;//判断是否能够整除
            for (int i = 0;i<count-1;i++) {
                redPacket.add(avg);
            }
            redPacket.add(avg+mod);
        setBalance(getBalance()-money);//充值当前账户余额
        return redPacket;
    }
}

  • 群员类代码:
import java.util.ArrayList;
import java.util.Random;

//群成员
public class Member extends User{

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

    //收红包
    public void receiveRedPacket(ArrayList<Integer> list) {
        //抢一个红包少一个
        Random rd = new Random();
        int index = rd.nextInt(list.size());
        Integer m = list.get(index);
        setBalance(getBalance()+m);
        //红包被人抢走 需要从当前容器中把该红包移除
        list.remove(index);
    }
}

  • 测试类代码:
package com.zhou.day0825.redpacket;

//测试类
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        GroupOwner group = new GroupOwner("刘老师",100);
        group.checkBalance();
        ArrayList<Integer> list = group.sengRedPacket(10,3);
        /*for (int i = 0;i<list.size();i++) {
            System.out.println(list.get(i));
        }
        group.checkBalance();*/

        Member m1 = new Member("小周",1000);
        Member m2 = new Member("小景",1000);
        Member m3 = new Member("小宋",1000);
        m1.receiveRedPacket(list);
        m2.receiveRedPacket(list);
        m3.receiveRedPacket(list);

        m1.checkBalance();
        m2.checkBalance();
        m3.checkBalance();
    }
}

这篇关于Java继承应用——发红包的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!