Created
April 9, 2014 04:00
-
-
Save electrocucaracha/10224925 to your computer and use it in GitHub Desktop.
This program helps to choose a volunteer from a list of students.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ParticipantSelector { | |
private static final String FILE_NAME = "students.txt"; | |
private static BufferedReader br; | |
private static String option = "l"; | |
private static String getNextOption() { | |
String result = ""; | |
System.out.println("Enter an option:"); | |
try { | |
result = br.readLine(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return "".equals(result.trim()) ? option : result.trim().toLowerCase(); | |
} | |
private static List<String> getParticipants() { | |
List<String> result = new ArrayList<String>(); | |
String line; | |
try { | |
br = new BufferedReader(new FileReader(FILE_NAME)); | |
while ((line = br.readLine()) != null) { | |
result.add(line); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
br.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return result; | |
} | |
public static void main(String[] args) throws FileNotFoundException { | |
List<String> list = getParticipants(); | |
String currentParticipant; | |
br = new BufferedReader(new InputStreamReader(System.in)); | |
do { | |
option = getNextOption(); | |
switch (option) { | |
case "n": | |
currentParticipant = list.remove((int) (Math.random() * (list | |
.size()))); | |
System.out.println(currentParticipant); | |
break; | |
case "l": | |
System.out.println(list); | |
break; | |
case "q": | |
return; | |
} | |
} while (!"q".equalsIgnoreCase(option) && !list.isEmpty()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment