From 48d8a7bf72138ca1d89616559beec9e1a1c7e372 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Tue, 3 Nov 2020 14:41:23 +0700 Subject: [PATCH] Finished Assignment 10 --- Assignment10/bin/SchoolLottery.class | Bin 0 -> 2156 bytes Assignment10/bin/SchoolLotteryFromFile.class | Bin 0 -> 2234 bytes Assignment10/src/SchoolLottery.java | 43 +++++++++++++++++ Assignment10/src/SchoolLotteryFromFile.java | 46 +++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 Assignment10/bin/SchoolLottery.class create mode 100644 Assignment10/bin/SchoolLotteryFromFile.class create mode 100644 Assignment10/src/SchoolLottery.java create mode 100644 Assignment10/src/SchoolLotteryFromFile.java diff --git a/Assignment10/bin/SchoolLottery.class b/Assignment10/bin/SchoolLottery.class new file mode 100644 index 0000000000000000000000000000000000000000..84ff7dec45cba8ea97f1552a5b61eca7f2a2c271 GIT binary patch literal 2156 zcmZ`)TXz#x7~LlgophM=5-7B`zyN|tN*e{OND4{|NF=>TFjx?EnhfpGWF|}|h2jNJ zyglf<{sEq4Ny@ToxqS97=|cCJnUvmqIg>g0ZhP$=8r-l}_9fsTST zZyR32H3d@J*^C93e%Jzo+4I>m^lKlpY|A?%(3VUU1)>v9*;LVnxP}g>3c3V#ZIe+& zC*mCtNNCuBc7d+EWt&rt>YV9bG3F|)B9V7WMx|)DmV6H{quzo=6kYiZuvy1Wqg*a{ zjWU7O74!@AyvSN88MbY@9T>nq1%n$HDlFGMv#Mb~4hTdndyOH>$>i)>!y{JHsAg64 z;h@BHNT4qnaIwiGRg@WC)^G$z$%oS*@gd!`oFQecU@;^G`e!PpQ8)GTHkH#oM>p-V zZpoA?(m1AIs72jIZw0WZCwoYCR!+lvxI_)u6r1=7 zCL)`Y}u$O>jPT$7cyo3|TArLJ-!KGJXls(@OvN{d%5*}3%d8-ooIWc9~4 zY-={ZS`Rv(^CKW~l-6bzIYOKDr*XXC8KuQZqvns4r0b@Jc`VSYZr!67Cpj0xEp|^j zoz|xuy=J(cRkCUZXG`auf=Y`uzmUs@U3RJ}77>?+qoIb|Us>z3?&O7V!Ki0-~>CZ-SrDt!@I61xuJpx zbgde4zV{@rZ>1$wl+?3)fv+^|mHbBycb=tXtb$lv(iy($5e{)Il+{v49TCpq8onC>KUPb5Z3c8=7=U3l)7tc;V&JOyu zql-~?^G*wV(2G5Mqh_smVt{s0-u<+SzS$>uX$5+|?er5IPQ21YiqBPKW)rWi;KZYd z7;NH=#9K{dn>f?N_)rt)ryk>C`Zx3pH*xW5Xg>T5c{(I6w=CD1Yv!---OAO8IB z({vNpC7L!!G^2>~j1xOQ1;o>fZk#|5P9lLUEzS|&4c>l6WDj`v1E>8*^!upO0kRPF zPJE4z@d?q#@erTFAiF0~#2iY@Rc4khM4lt7pid~6{F|YLoH3q~_-7$emn7OI%&~!A z&?$Mc$kSttu40i=R43)%#|E0Pn{fZcM zAkRbGdxyEBj;up-DMS;)1nv=opxqGBe@-+ReptR(L&FdMKiDgdvw6P6{SZZtW)Vr~ rDg?`Ykf(+ZH}_7Gb);BF+RwWua3Fh+=on*nenYOgLGUf#Blzw=N;~PQ literal 0 HcmV?d00001 diff --git a/Assignment10/bin/SchoolLotteryFromFile.class b/Assignment10/bin/SchoolLotteryFromFile.class new file mode 100644 index 0000000000000000000000000000000000000000..076a20db4e0ccee76b5897ac80907a122285a24c GIT binary patch literal 2234 zcmZuyYf}?v6nAAH@yDBv#1bS{+x2@5t~mLb1J!WytjSKc5MHgKzlkfCy==0mZgq%bQ$P? zu3?A3&Mh+P=tfru1X2dJVY|SNyzR(BP+O4RthG=jid5b$S=Bkqv(hJMd(v2*dQjs@J7c7C62Yvn1By6rg9Q-LN8ypA^*qG8`<2-fyg5|`;@Hk&mI zu34uOcFC?=tTK~p8uIHz!_K;9Ic2w|V;WuRamBz@yh+NAT=u6NR!bs1m6=kRiUwx! z7E@HDDk3ho5RbJ;;yN|gRqk0bz7xVU?S`4eJVPk}%A1=8-cc583oQ`PouD?c-uGgP zHR6S^RBLUtWCT@pauk;VYwQPsJ?Wb*P~w(X%|rDx(6-)k{V1&yo4mFUH zxW(RXL=CA%&4|D3>!={DLxLUD0jBDW3n0FfQ&}YH20jYwQmLlKgG${nuvb;SL}1`H z?y%z*twtd}5n6Raom(+54qeARe4^oFfq_<31=pW+1E)N(T#|L)b{zwsLMPBrxv(^5 zJ*!0L6P8!uXxO=h6tpw0J%P4HiHBNLjRm2sf>8#eotWOH?3@z!X;;w=ypmL^3G{C{ z-$oP(4jt0uUjfsq*~wpxUu{St!FNtwK0CQ`k*5TId$OxAvTNv`Uq#O^=zSEP-pOZo z7^j2xbnM`rU0gGwJJ^jq{9?(YFOK&E&$V%FBKtFXhnm>;Bl>^Bz#5Lsr=DNMF)n4A zINrnykK*N_$2iT6)Y$(!@zrzJ6^M2y5dEZ}pmY+#A@txdQpj;*PBZ$L%rPDFS$HbW6#3~+iL#ATQv+H90QMw=wcckv#<4RTDs zj}ORYlz%){XpUmQKl8t_SDd783n;}b{-Cdl#o09o!Y$|5Am`UmnNQiPSXxDO<{>(V zhMRDk@c83Dc=8w3Uw(*Wf#~l(Vw@xson-k(B1h1VaP=s`A0wDF_cJ(!EC==R7U*a3 z8SXQ85-0FE#TPuk4`1L*LO(@RU*T)URL}>I_y-3x2n|wuf-baa_=cZq;hrkRZ+WN< G-~9*RR08(^ literal 0 HcmV?d00001 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