Skip to content

Instantly share code, notes, and snippets.

@SijmenHuizenga
Created February 15, 2016 10:07
Show Gist options
  • Save SijmenHuizenga/3798a9484642fd243d0d to your computer and use it in GitHub Desktop.
Save SijmenHuizenga/3798a9484642fd243d0d to your computer and use it in GitHub Desktop.
Lottotrekking
package it.sijmen;
import java.util.ArrayList;
import java.util.Random;
/**
* Created by Sijmen on 15-2-2016.
*/
public class GlazenBol {
private int capaciteit;
private ArrayList<Lottobal> ballen;
private Random random;
public GlazenBol(int capaciteit) {
this.capaciteit = capaciteit;
this.ballen = new ArrayList<>(this.capaciteit);
this.random = new Random();
}
public void verzamelAlleBallen(){
for(int i = 1; i <= capaciteit; i++)
this.ballen.add(new Lottobal(i));
}
public Lottobal schepBal(){
int index = random.nextInt(ballen.size());
Lottobal bal = ballen.get(index);
ballen.remove(index);
return bal;
}
}
package it.sijmen;
/**
* Created by Sijmen on 15-2-2016.
*/
public class Lottobal implements Comparable<Lottobal>{
private int balnummer;
public Lottobal(int balnummer) {
this.balnummer = balnummer;
}
public int getBalnummer() {
return balnummer;
}
@Override
public int compareTo(Lottobal andereBal) {
return Integer.compare(this.balnummer, andereBal.getBalnummer());
}
@Override
public String toString() {
return balnummer + "";
}
}
package it.sijmen;
/**
* Created by Sijmen on 15-2-2016.
*/
public class Lottomachine {
private Scorebord bord;
private GlazenBol bol;
private boolean uitslag;
public Lottomachine() {
this.bord = new Scorebord();
this.bol = new GlazenBol(99);
}
public void voerTrekkingUit(){
bol.verzamelAlleBallen();
bord.maakLeeg();
for(int i = 0; i < 6; i++)
bord.plaatsBal(bol.schepBal());
bord.plaatsBonusbal(bol.schepBal());
bord.sorteerBallen();
}
public String getUitslag() {
return bord.toString();
}
}
package it.sijmen;
public class Main {
public static void main(String[] args) {
Lottomachine lottomachine = new Lottomachine();
lottomachine.voerTrekkingUit();
System.out.println(lottomachine.getUitslag());
}
}
package it.sijmen;
import java.util.ArrayList;
import java.util.Collections;
/**
* Created by Sijmen on 15-2-2016.
*/
public class Scorebord {
private Lottobal bonusBal;
private ArrayList<Lottobal> ballen;
public Scorebord() {
this.bonusBal = null;
this.ballen = new ArrayList<>();
}
public void plaatsBal(Lottobal bal){
this.ballen.add(bal);
}
public void plaatsBonusbal(Lottobal bal){
if(this.bonusBal != null)
throw new IllegalStateException("Er is al een bonusbal geplaats");
this.bonusBal = bal;
}
public void maakLeeg(){
this.bonusBal = null;
this.ballen.clear();
}
public void sorteerBallen(){
Collections.sort(ballen);
}
@Override
public String toString() {
return "Scorebord{" +
"ballen=" + ballen +
", bonusBal=" + bonusBal +
'}';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment