• java基础---22. 发红包案例


    1 题目

    2 思路

    3 代码事项

    • 用户类
    public class User {
        private String name;
        private int money;
    
        public User() {
        }
    
        public User(String name, int money) {
            this.name = name;
            this.money = money;
        }
    
        //展示一下用户有多少钱
        public void show(){
            System.out.println("我叫:"+ name + ",我有多少钱:"+ 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;
        }
    }
    
    • 子类:群主类
    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 totalMoney, int count) {
            //首先需要一个集合,用来存储若干红包的金额
            ArrayList<Integer> redList = new ArrayList<>();
    
            //首先看下群组自己有多少钱
            int leftMoney = super.getMoney();//群主当余额
            if(totalMoney > leftMoney) {
                System.out.println("余额不足");
                return redList;
            }
            //扣钱,其实就是重新设置余额
            super.setMoney(leftMoney - totalMoney);
    
            //发红包需要平均拆分成为count份
            int avg = totalMoney / count;
            int mod = totalMoney % count;//余额,也就是甩下的零头
    
            //除不开的零头,包在最后一个红包中
            //下面把红包一个一个放到集合当中
            for(int i = 0; i < count -1; i++) {
                redList.add(avg);
            }
            //最后一个红包
            int last = avg + mod;
            redList.add(last);
            return redList;
        }
    }
    
    • 子类:普通群成员
    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 detal = list.remove(index);
            //自己本来有多少钱
            int money = super.getMoney();
            //加法,并且重新设置回去
            super.setMoney(money + detal);
        }
    }
    
    
    • 调用
    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();//100
            one.show();//0
            two.show();//0
            three.show();//0
    
            //群众总共发20快钱,分成3个红包
            ArrayList<Integer> redList = manager.send(20,3);
            //3个成员收红包
            one.receive(redList);
            two.receive(redList);
            three.receive(redList);
    
            manager.show();//80
            one.show();//6
            two.show();//8
            three.show();//6
    
        }
    }
    
    

    4 若不写成继承的关系则有

    • Manger类
    public class Manager{
        //1
        private String name;
        private int money;
    
        //2
        public Manager(){
    
        }
        public Manager(String name, int money){
            this.name = name;
            this.money = money;
        }
    
        //3
        public void setName(String name){
            this.name = name;
        }
        public String getName(){
            return name;
        }
        public void setMoney(int money){
            this.money = money;
        }
        public int getMoney(){
            return money;
        }
    
        //4
        public void show(){
            System.out.println("我叫:"+name+",我有多少钱:"+money);
        }
        public ArrayList<Integer> send(int totalMoney, int count){
            ArrayList<Integer> redList = new ArrayList<>();
            int leftMoney = getMoney();
            if(totalMoney > leftMoney){
                System.out.println("余额不足");
                return redList;
            }
            setMoney(leftMoney - totalMoney);
            int avg = totalMoney / count;
            int mod = totalMoney % count;
    
            for(int i = 0; i < count-1; i++){
                redList.add(avg);
            }
            int last = avg + mod;
            redList.add(last);
            return redList;
        }
    }
    
    
    • Member类
    import java.util.ArrayList;
    import java.util.Random;
    
    public class Member{
        //1
        private String name;
        private int money;
    
        //2
        public Member(){
    
        }
        public Member(String name, int money){
            this.name = name;
            this.money = money;
        }
    
        //3
        public void setName(String name){
            this.name = name;
        }
        public String getName(){
            return name;
        }
        public void setMoney(int money){
            this.money = money;
        }
        public int getMoney(){
            return money;
        }
    
        //4
        public void show(){
            System.out.println("我叫:"+name+",我有多少钱:"+money);
        }
        public void receive(ArrayList<Integer> list){
            int index = new Random().nextInt(list.size());
            int detal = list.remove(index);
            int money = getMoney();
            setMoney(money + detal);
        }
    }
    
    • 调用
      import java.util.ArrayList;

    public class MainRedPacket {
    public static void main(String[] args) {
    Manager manager = new Manager("群主",100);

        Member meb1 = new Member("成员A",0);
        Member meb2 = new Member("成员B",0);
        Member meb3 = new Member("成员C",0);
    
        ArrayList<Integer> redList = manager.send(20,3);
        meb1.receive(redList);
        meb2.receive(redList);
        meb3.receive(redList);
    
        manager.show();
        meb1.show();//6
        meb2.show();//8
        meb3.show();//6
    
    
    }
    

    }
    ···

  • 相关阅读:
    SQL Server 查看物理页存储
    Sql Server 孤立用户解决办法
    大文件下载
    UITableViewCell
    xib文件的使用
    UIToolbar
    UIView常见的属性和方法
    iOS的三种多线程技术:
    网络处理文件上传、获取文件MIMEType、其他HTTP请求方式
    转场动画过渡效果
  • 原文地址:https://www.cnblogs.com/deer-cen/p/12236872.html
Copyright © 2020-2023  润新知