From 25a0297ec7eb55188e3a0f44c406d23e780a9921 Mon Sep 17 00:00:00 2001 From: Max Gerbeth Date: Wed, 11 Jan 2023 10:07:11 +0100 Subject: [PATCH] implementing itemRotation() --- src/main/java/org/bitbiome/shop/Shop.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/bitbiome/shop/Shop.java b/src/main/java/org/bitbiome/shop/Shop.java index d47a2a3..21aea67 100644 --- a/src/main/java/org/bitbiome/shop/Shop.java +++ b/src/main/java/org/bitbiome/shop/Shop.java @@ -7,6 +7,8 @@ import java.io.File; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.HashSet; +import java.util.Random; public class Shop { public ArrayList allItems; @@ -14,6 +16,7 @@ public class Shop { public Shop(){ allItems = loadItems(); + currentShopItems = itemRotation(allItems, 2); } public boolean buy(){ @@ -41,10 +44,18 @@ public class Shop { return arrayList; } - public ArrayList itemRotation(ArrayList alleItems, int itemCount){ - //ToDo - - return null; + public ArrayList itemRotation(ArrayList alleItems, int itemCount){ + ArrayList arrayList = new ArrayList(); + HashSet hashSet = new HashSet<>(); + Random random = new Random(); + while (hashSet.size() < itemCount){ + int rand = random.nextInt(alleItems.size()); + if(!hashSet.contains(rand)){ + hashSet.add(rand); + arrayList.add(alleItems.get(rand)); + } + } + return arrayList; } public void getCurrentShopItems(){