Inheritance - a simple example of the development of distributed red packets

Package Penalty for cn.learn.chouxiang.practice03; 

public  class the Users {
     Private String name; // Name 
    Private  Double Money; // balance 

    public  void Show () { 
        System.out.println (String.format ( "My name is:% s, I left:%. 2F \ n-",. the this .name, the this .money)); 
    } 

    public the Users () { 
    } 

    public the Users (String name, Double Money) {
         the this .name = name;
         the this .money = Money; 
    } 

    public String getName() {
        return name;
    }

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

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }
}
Package cn.learn.chouxiang.practice03; 

Import of java.util.ArrayList;
 Import java.util.Random; 

public  class Manager the extends the Users {
     public Manager () { 
    } 

    public Manager (String name, Double Money) {
         Super (name, Money );   // if new main out, the sub-class of the parent class, and the other will inherit independent open 
    } 

    public the ArrayList <Double> Send ( int totalMoney, int COUNT) { 

        // plurality of envelopes with the first set amount is stored 
        the ArrayList <Double> = readlist new new the ArrayList <> ();

        // first look at the balance of the main group 
        Double lefMoney = Super .getMoney ();   // main group before 

        IF (totalMoney> lefMoney) { 
            System.out.println ( "broke!" );
             Return readlist; 
        } 
        // deducting 
        the else {
             Super .setMoney (lefMoney- totalMoney);
             Double leftSum = totalMoney;
             for ( int I = 0; I <COUNT; I ++ ) {
                 Double AVG = ( Double ) leftSum / ( new newThe Random (. 3) .nextDouble () + 2.4 ); 
                leftSum - = AVG;
                 // the envelopes into a collection 
                readList.add (AVG);
                 // last process red single 
                IF (COUNT-I ==. 1 ) 
                    readList.add (leftSum); 
            } 
            System.out.println ( "red:" + readList + "total:" + . readList.stream () mapToDouble (Double :: doubleValue in) .sum ());
             return readlist; 
        } 

    } 
}
Package cn.learn.chouxiang.practice03; 

Import of java.util.ArrayList;
 Import java.util.Random; 

public  class Member the extends the Users {
     public Member () { 
    } 

    public Member (String name, Double Money) {
         Super (name, Money ); 
    } 

    // close envelopes 
    public  void recieve the (the ArrayList <Double> List) {
         // from a set of randomly placed red
         // first index value obtained red 
        int index = new new . the random () the nextInt (list.size () );
         //Delete the index value of the red envelope, and put money in the balance 
        Double Money = list.remove (index);
         Super .setMoney (Money + Super .getMoney ()); 


    } 
}
package cn.learn.chouxiang.practice03;

import java.util.ArrayList;

public class Enter {
    public static void main(String[] args) {
        Manager qunzhu=new Manager("群主",55.0);
        Member  qunyuan1=new Member("群员A",0.0);
        Member  qunyuan2=new Member("群员B",0.0);
        Member  qunyuan3=new Member("群员C",0.0);
        Member  qunyuan4=new Member("群员D",0.0);
        Member  qunyuan5=new Member("群员E",0.0);
        Member  qunyuan6=new Member("群员F",0.0);

        //发红包
        ArrayList<Double> list=qunzhu.send(30,6);

        //收红包
        qunyuan1.recieve(list);
        qunyuan2.recieve(list);
        qunyuan3.recieve(list);
        qunyuan4.recieve(list);
        qunyuan5.recieve(list);
        qunyuan6.recieve(list);

        qunzhu.show();
        qunyuan1.show();
        qunyuan2.show();
        qunyuan3.show();
        qunyuan4.show();
        qunyuan5.show();
        qunyuan6.show();

    }
}

// Summary: formatted output, familiar with the random number generation, the use of ArrayList

Guess you like

Origin www.cnblogs.com/huxiaobai/p/11456394.html