java继承实现发红包简单实现

项目结构:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5rGq56iL5bqP54y_,size_20,color_FFFFFF,t_70,g_se,x_16

 

 

运行结果:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5rGq56iL5bqP54y_,size_20,color_FFFFFF,t_70,g_se,x_16

 

 

User.java

/*
这是个父类
 */
public class Users {
   //名字和余额
   private String name;
   private int balanceMoney;

   public String getName() {
      return name;
   }

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

   public int getBalanceMoney() {
      return balanceMoney;
   }

   public void setBalanceMoney(int balanceMoney) {
      this.balanceMoney = balanceMoney;
   }

   /**
    * 构造方法
    * @param name
    * @param balanceMoney
    */
   public Users(String name, int balanceMoney) {
      this.name = name;
      this.balanceMoney = balanceMoney;
   }

   public Users() {

   }

   @Override
   public String toString() {
      return "Users{" +
            "姓名name='" + name + '\'' +
            ", 余额balanceMoney=" + balanceMoney +
            '}';
   }
}

 

Boss.java

import java.util.ArrayList;

public class Boss extends Users {

    public ArrayList<Integer> sendRed(int totalMoney, int count) {
//        红包列表
        ArrayList<Integer> redList = new ArrayList<>();

        int curMoney = super.getBalanceMoney();
//        对现在的金额和发送的金额进行判断
        if (totalMoney > curMoney) {
            System.out.println("你没有钱了!");
            return null;
        }
//        现在的金额减去现在发红包的金额
        super.setBalanceMoney(curMoney - totalMoney);
        int avg = totalMoney / count;
        int mod = totalMoney % count;

        for (int i = 0; i < count; i++) {
//            加入列表中
            redList.add(avg);
        }
//        最后的红包把剩余的钱加上
        int last = avg + mod;
        redList.add(last);
        return redList;
    }

    /**
     *
     */
    public Boss() {
        super();

    }

    /**
     * @param name
     * @param balanceMoney
     */
    public Boss(String name, int balanceMoney) {
        super(name, balanceMoney);
    }
}

 

Member.java

import java.util.ArrayList;
import java.util.Random;
/*
继承User父类
 */
public class Members extends Users {

   public Members() {
      super();

   }

   /**
    * 重写构造方法
    */
   public Members(String name, int balanceMoney) {
      super(name, balanceMoney);
   }

   public void receive(ArrayList<Integer> list) {
      // 随机取出红包,保存索引
      int index = new Random().nextInt(list.size());
      // 根据索引,删除
      int temp = list.remove(index);
      // 获取成员余额
      int money = super.getBalanceMoney();
      // 重新写入余额,即更新
      super.setBalanceMoney(temp + money);
   }

}

 

 

Start.java

import java.util.ArrayList;

public class start {
    public static void main(String[] args) {
        Boss boss = new Boss("老板",1000);
        Members members1 =new Members("员工1",0);
        Members members2 =new Members("员工2",0);
        Members members3 =new Members("员工3",0);
        Members members4 =new Members("员工4",0);
        Members members5 =new Members("员工5",0);
        Members members6 =new Members("员工6",0);
        Members members7 =new Members("员工7",0);
        Members members8 =new Members("员工8",0);

        System.out.println("----------------当前的信息----------------------------------------------");
        System.out.println(boss.toString());
        System.out.println(members1.toString());
        System.out.println(members2.toString());
        System.out.println(members3.toString());
        System.out.println(members4.toString());
        System.out.println(members5.toString());
        System.out.println(members6.toString());
        System.out.println(members7.toString());
        System.out.println(members8.toString());

        System.out.println("----------------发红包(8个,总价21)----------------------------------------------");

        ArrayList<Integer> redList = boss.sendRed(21,8);
        members1.receive(redList);
        members2.receive(redList);
        members3.receive(redList);
        members4.receive(redList);
        members5.receive(redList);
        members6.receive(redList);
        members7.receive(redList);
        members8.receive(redList);

        System.out.println(boss.toString());
        System.out.println(members1.toString());
        System.out.println(members2.toString());
        System.out.println(members3.toString());
        System.out.println(members4.toString());
        System.out.println(members5.toString());
        System.out.println(members6.toString());
        System.out.println(members7.toString());
        System.out.println(members8.toString());


        System.out.println("----------------发红包(8个,总价200)----------------------------------------------");

         redList = boss.sendRed(200,8);
        members1.receive(redList);
        members2.receive(redList);
        members3.receive(redList);
        members4.receive(redList);
        members5.receive(redList);
        members6.receive(redList);
        members7.receive(redList);
        members8.receive(redList);

        System.out.println(boss.toString());
        System.out.println(members1.toString());
        System.out.println(members2.toString());
        System.out.println(members3.toString());
        System.out.println(members4.toString());
        System.out.println(members5.toString());
        System.out.println(members6.toString());
        System.out.println(members7.toString());
        System.out.println(members8.toString());
    }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汪程序猿

就当请我吃顿饭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值