Java – red packet instance – inheritance exercise


Case study:

Insert picture description here

Case diagram:

Divided into three categories, a parent class put the amount and name in the parent class. Then create a new group member and a new main class. The group main class has the method of sending the red packet, and the common member class has the method of receiving the red packet. In other words, only group leaders can send red packets, and ordinary members can only receive red packets.

Java - red packet instance - inheritance exercise

Code implementation:


package cn.itcast.day09.demo14;

public class User {
    private String name;//Name
    private int money;//Balance

    public User() {

    public User(String name, int money) { = name; = money;
    //Show balance
    public void show(){
       System.out.println("My name is"+name+"How much money do I have?"+money);
    public String getName() {
        return name;

    public void setName(String name) { = name;

    public int getMoney() {
        return money;

    public void setMoney(int money) { = money;

Group main class:

 package cn.itcast.day09.demo14;

import cn.itcast.day09.demo04.Arraylis;

import java.util.ArrayList;

public class Manager extends User {
    public  Manager(){

    public Manager(String name, int money) {
        super(name, money);
    //Method of sending red envelope
    public ArrayList<Integer> send(int totalMoney,int count){
        //First, a collection is needed to store several red envelope amounts
        ArrayList<Integer> redList=new ArrayList<>();

        //First of all, see how much money the group leader has
        int LeftMoney=super.getMoney();//Get the current balance of group leader
        //Judge whether the group owner's balance is sufficient
            System.out.println("Insufficient balance");
            return redList;//Returns an empty collection

        //Deduct money and update balance.
        //Formula: latest balance = last balance - issued money
        //Red packets need to be split into count shares on average
        int avg=totalMoney/count;
        int mod=totalMoney %count;//The remainder, that is, the fraction

        //Let's put the red envelopes into the collection one by one
        for (int i = 0; i < count-1; i++) {
            //Except for the small change that cannot be opened, hold it in the last red envelope
        int last=(avg+mod);
        //Except for the small change that cannot be opened, hold it in the last red envelope
        return  redList;

General member class:

 package cn.itcast.day09.demo14;

import java.util.ArrayList;
import java.util.Random;
//Ordinary members
public class Member extends User{
    public Member() {

    public Member(String name, int money) {
        super(name, money);
    //Methods of receiving red envelopes
    public void recive(ArrayList<Integer> list){
        //Choose one of the red envelopes and give it to yourself
        //Get random numbers from a list set
        int index=new Random().nextInt(list.size());
        //According to the index, delete from the collection, and get the deleted red packet to yourself.
        int delta=list.remove(index);
        //How much money would the current members have
        int money=super.getMoney();
        //Set balance from New


 package cn.itcast.day09.demo14;

import java.util.ArrayList;

public class MainRedPacket {
    public static void main(String[] args) {
        Manager manager=new Manager("Group leader",100);
        Member one=new Member("Member a",0);
        Member two=new Member("Member B",0);
        Member three=new Member("Member C",0);;;;;
        ArrayList<Integer> redList=manager.send(20,3);
        //Three ordinary members receive red envelopes
        //Latest balance of group leader;
        //Member's latest balance;;;


Java - red packet instance - inheritance exercise