Преглед на файлове

Gift association and showAll() method.

Tankernn преди 8 години
родител
ревизия
d46f1be4bd
променени са 1 файла, в които са добавени 33 реда и са изтрити 12 реда
  1. 33 12
      src/main/java/eu/tankernn/julklapp/Julspel.java

+ 33 - 12
src/main/java/eu/tankernn/julklapp/Julspel.java

@@ -1,26 +1,30 @@
 package eu.tankernn.julklapp;
+
 import java.awt.EventQueue;
+import java.util.Arrays;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
-import java.util.ArrayList;
+import java.util.Random;
 
 import javax.swing.JFrame;
+import javax.swing.JOptionPane;
 
 public class Julspel {
+	public static final String[] NAMES = {"Claes", "Lars"}, GIFTS = {};
+
+	private Random rand = new Random();
 
 	private JFrame frame;
-	private Map<String, String> associations = new HashMap<>();
+
 	private int difficulty;
-	
-	private ArrayList<String> name = new ArrayList<String>();
+
+	private Map<String, String> associations = new HashMap<>();
 
 	/**
 	 * Launch the application.
 	 */
 	public static void main(String[] args) {
-		
-
-		
 		EventQueue.invokeLater(new Runnable() {
 			public void run() {
 				try {
@@ -38,10 +42,6 @@ public class Julspel {
 	 */
 	public Julspel() {
 		initialize();
-		
-		name.add("Claes");
-		name.add("Lars");
-		name.add("");
 	}
 
 	/**
@@ -55,9 +55,30 @@ public class Julspel {
 	}
 
 	private void generateAssociations() {
+		List<String> names = Arrays.asList(NAMES), gifts = Arrays.asList(GIFTS);
 		for (int i = 0; i < difficulty; i++) {
-			
+			associations.put(names.remove(rand.nextInt(names.size())), gifts.remove(rand.nextInt(gifts.size())));
+		}
+	}
+	
+	private void showAll() {
+		StringBuilder sb = new StringBuilder();
+		sb.append("<html>");
+		sb.append("<h1>Names:</h1>");
+		
+		for (String name : NAMES) {
+			sb.append(name).append("<br>");
 		}
+		
+		sb.append("<h1>Gifts:</h1>");
+		
+		for (String gift : GIFTS) {
+			sb.append(gift).append("<br>");
+		}
+		
+		sb.append("</html>");
+		
+		JOptionPane.showMessageDialog(frame, sb);
 	}
 
 }