Java使用HashMap映射實(shí)現(xiàn)消費(fèi)抽獎(jiǎng)功能
本文實(shí)例為大家分享了Java實(shí)現(xiàn)消費(fèi)抽獎(jiǎng)功能的具體代碼,供大家參考,具體內(nèi)容如下
要求如下:
1、定義獎(jiǎng)項(xiàng)類Awards,包含成員變量String類型的name(獎(jiǎng)項(xiàng)名稱)和int類型的count(獎(jiǎng)項(xiàng)數(shù)量)。
2、定義抽獎(jiǎng)?lì)怐rawReward,包含成員變量Map<Integer, Awards> 類型的rwdPool(獎(jiǎng)池對象)。該類實(shí)現(xiàn)功能如下:a) 構(gòu)造方法中對獎(jiǎng)池對象初始化,本實(shí)驗(yàn)要求提供不少于4類獎(jiǎng)品,每類獎(jiǎng)品數(shù)量為有限個(gè),每類獎(jiǎng)品對應(yīng)唯一的鍵值索引(抽獎(jiǎng)號)。b) 實(shí)現(xiàn)抽獎(jiǎng)方法draward,由抽獎(jiǎng)號在獎(jiǎng)池中抽獎(jiǎng),并根據(jù)當(dāng)前獎(jiǎng)池的情況作出對應(yīng)的邏輯處理;c) 利用迭代器Iterator實(shí)現(xiàn)顯示獎(jiǎng)池獎(jiǎng)項(xiàng)情況的方法showPool。
3、編寫測試類,實(shí)現(xiàn)下圖效果:

實(shí)現(xiàn)代碼:
import java.util.Random;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
class Awards {
private String name;
private int count;
public Awards() {
}
public Awards(String name, int count) {
this.name = name;
this.count = count;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
class DrawReward {
private Map<Integer,Awards> rwdPool=null;
public DrawReward(){
this.rwdPool=new HashMap<Integer,Awards>();
rwdPool.put(0,new Awards("陽光普照獎(jiǎng):家庭大禮包",100));
rwdPool.put(1,new Awards("一等獎(jiǎng):華為Mate X",4));
rwdPool.put(2,new Awards("二等獎(jiǎng):格力吸塵器",6));
rwdPool.put(3,new Awards("特等獎(jiǎng):¥5000",1));
}
public boolean hasAward(int rdkey){
Awards awards = this.rwdPool.get(rdkey);
if(awards.getCount()==0) return false;
else return true;
}
public void draward(int rdKey) {
Awards aw = this.rwdPool.get(rdKey);
System.out.println("抽獎(jiǎng)結(jié)果:"+aw.getName());
aw.setCount(aw.getCount()-1);
}
public void showPool(){
Iterator<Awards> it;
it = rwdPool.values().iterator();
while(it.hasNext()){
Awards aw = it.next();
System.out.println(aw.getName()+";剩余獎(jiǎng)項(xiàng)數(shù)量:"+aw.getCount());
}
}
}
public class MainClass {
public static void main(String[] args) {
DrawReward draw = new DrawReward();
for(int i=0;i<10;i++){
Random rd = new Random();
int rdKey = rd.nextInt(4);
if(draw.hasAward(rdKey)) {
draw.draward(rdKey);
} else {
i--;
}
}
draw.showPool();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Apache POI在Java中實(shí)現(xiàn)Excel單元格的合并
在日常工作中,Excel是一個(gè)不可或缺的工具,尤其是在處理大量數(shù)據(jù)時(shí),本文將介紹如何使用 Apache POI 庫在 Java 中實(shí)現(xiàn) Excel 單元格的合并,需要的可以了解下2025-03-03
SpringBoot報(bào)錯(cuò)Invalid?bound?statement?(not?found)問題排查和解決方案
這篇文章主要介紹了SpringBoot報(bào)錯(cuò)Invalid?bound?statement?(not?found)問題排查和解決方案,文中通過圖文結(jié)合的方式講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-03-03
解決IDEA報(bào)錯(cuò)Caused by: org.springframework.boot.web.se
遇到IDEA啟動報(bào)錯(cuò),可嘗試以下方法:打開項(xiàng)目設(shè)置(Ctrl+Shift+Alt+S),將JDK版本修改為1.8;或者檢查TomCat依賴,若有問題可嘗試刪除,此外,確保每次拉取項(xiàng)目后,maven地址設(shè)置為本地,并且JDK版本設(shè)置為1.8,以上為個(gè)人解決經(jīng)驗(yàn),希望對大家有所幫助2024-09-09
IDEA配置JRebel實(shí)現(xiàn)熱部署的方法
這篇文章主要介紹了IDEA配置JRebel實(shí)現(xiàn)熱部署的方法,本文給大家介紹的非常想詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
詳解springboot接口如何優(yōu)雅的接收時(shí)間類型參數(shù)
這篇文章主要為大家詳細(xì)介紹了springboot的接口如何優(yōu)雅的接收時(shí)間類型參數(shù),文中為大家整理了三種常見的方法,希望對大家有一定的幫助2023-09-09

