第六次作业(二)

时间:2021-12-15 15:33:35

测试类继承Thread,在控制台模拟了整个抽奖流程,包括抽奖的等待,等等...

Prize类和Person类设置人数和奖品数,并有相应的构造器。

防止溢出那里直接用List.size()方法就可以了。。。

代码如下,其余实现思想在代码中用备注指出:

package com.cumin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class DrawPrize {
    private static ArrayList<Prize> prizes;
    private static ArrayList<Person> people;

    public ArrayList<Prize> dealPrize() {
        if (prizes == null) {
            prizes = new ArrayList<>();
            for(int i=1;i<=Prize.numofprizes;i++){
                prizes.add(new Prize("奖品", i));
            }
            Collections.shuffle(prizes);
        }
        return prizes;
    }
    public ArrayList<Person> dealPerson() {
        if (people==null) {
            people = new ArrayList<>();
            for(int i=1;i<=Person.numofpeople;i++){
                people.add(new Person("人物", i));
            }
            Collections.shuffle(people);

        }
        return people;
    }
    public static void drawPrize(){
        Random rd = new Random();
        int index1 =rd.nextInt(Prize.numofprizes--); //防止溢出
        Prize prize =prizes.get(index1);
        int index2 =rd.nextInt(Person.numofpeople--);
        Person p =people.get(index2);
        System.out.println("获奖人为:"+p.getName()+" 奖品为:" +prize.getName());
        //参与过后不可抽奖
        prizes.remove(index1);
        people.remove(index2);
        
    }
}
package com.cumin;

public class Prize {
    //奖品总数
    public static int numofprizes=100;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    //设置奖品编号
    public Prize(String name, int i) {
        this.name = name + i + " ";

    }
}
package com.cumin;

public class Person {
    //抽奖人数
    public static int numofpeople=100;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public Person(String name, int i) {
        this.name = name + i + " ";

    }
}
package com.cumin;

public class Test extends Thread{
    //抽奖流程
    public void Draw() throws Exception{
        System.out.println(".....抽奖即将开始,请耐心等待.....");
        try {
            sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //创建人物和奖品
        DrawPrize dp= new DrawPrize();
        dp.dealPrize();
        dp.dealPerson();
        System.out.println(".....抽奖开始.....");
        System.out.println(".....正在紧张的抽奖中.....");
        try {
            sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(".....一等奖有一名.....");
        DrawPrize.drawPrize();
        System.out.println(".....正在紧张的抽奖中.....");
        //抽奖中。。。
        try {
            sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(".....二等奖有五名.....");
        for(int i=0;i<5;i++){
            DrawPrize.drawPrize();
            sleep(500);
        }
        System.out.println(".....正在紧张的抽奖中.....");
        try {
            sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        System.out.println(".....三等奖有十名.....");
        for(int i=0;i<10;i++){
            DrawPrize.drawPrize();
            sleep(500);
        }
        System.out.println(".....抽奖结束.....");
    }
    
    public static void main(String args[]) throws Exception{
        new Test().Draw();
    }
}

 

第六次作业(二)第六次作业(二)