diff --git a/Assignment10/bin/SchoolLottery.class b/Assignment10/bin/SchoolLottery.class new file mode 100644 index 0000000..84ff7de Binary files /dev/null and b/Assignment10/bin/SchoolLottery.class differ diff --git a/Assignment10/bin/SchoolLotteryFromFile.class b/Assignment10/bin/SchoolLotteryFromFile.class new file mode 100644 index 0000000..076a20d Binary files /dev/null and b/Assignment10/bin/SchoolLotteryFromFile.class differ diff --git a/Assignment10/src/SchoolLottery.java b/Assignment10/src/SchoolLottery.java new file mode 100644 index 0000000..3589ca1 --- /dev/null +++ b/Assignment10/src/SchoolLottery.java @@ -0,0 +1,43 @@ +import java.util.*; + +public class SchoolLottery{ + private ArrayList entries; // holds Student references + public SchoolLottery(){ + entries = new ArrayList<>(); + } + + public void addStudents(){ + // prompts for student names + // adds students to entries list + // allow duplicate entries + Scanner input = new Scanner(System.in); + int studentNum = 0; + System.out.println("Please Enter to end input"); + System.out.print("Name" + ++studentNum + ": "); + String name = input.nextLine(); + while (!name.equals("")){ // signals end of data + entries.add(name); + System.out.println(name + " entered in the lottery."); + System.out.print("\nName" + ++studentNum + ": "); + name = input.nextLine(); + } + pickWinner(); + } + + public void pickWinner(){ + // chooses a random entry and displays winners name + int numEntries = entries.size(); // size of ArrayList + if(numEntries == 0) + System.out.println("*** No participants ***"); + else{ + Random random = new Random(); + String winner = entries.get(random.nextInt(numEntries)); + System.out.print("\n*** The winner is " + winner + " ***"); + } + } + + public static void main(String[] args){ + SchoolLottery lottery = new SchoolLottery(); + lottery.addStudents(); + } +} \ No newline at end of file diff --git a/Assignment10/src/SchoolLotteryFromFile.java b/Assignment10/src/SchoolLotteryFromFile.java new file mode 100644 index 0000000..e6fea94 --- /dev/null +++ b/Assignment10/src/SchoolLotteryFromFile.java @@ -0,0 +1,46 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Random; +import java.util.Scanner; + +public class SchoolLotteryFromFile{ + private ArrayList entries; // holds Student references + public SchoolLotteryFromFile(){ + entries = new ArrayList<>(); + } + + public void addStudents(String name){ + if(entries.contains(name)) { + System.out.println("Duplicate Detected!, You think you're so smart? "+name); + return; + } + System.out.println(name + " entered in the lottery."); + entries.add(name); + } + + public void pickWinner(){ + // chooses a random entry and displays winners name + int numEntries = entries.size(); // size of ArrayList + if(numEntries == 0) + System.out.println("*** No participants ***"); + else{ + Random random = new Random(); + String winner = entries.get(random.nextInt(numEntries)); + System.out.print("\n*** The winner is " + winner + " ***"); + } + } + + public static void main(String[] args){ + SchoolLotteryFromFile lottery = new SchoolLotteryFromFile(); + Scanner sc; + try { + sc = new Scanner(new File("studentList.txt")); + while(sc.hasNextLine())lottery.addStudents(sc.nextLine()); + lottery.pickWinner(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + } +} \ No newline at end of file