-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShowcase.java
106 lines (97 loc) · 2.08 KB
/
Showcase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Written by Noah Shaw
*/
import java.io.*;
import java.util.Random;
import java.util.*;
public class Showcase {
//Constants
public static final int MAX_PRIZES = 50;
public static final int MAX_SHOWCASE = 5;
public static final String DELIM = "\t";
public static final int FIELD_AMT = 2;
public static final String FILE_NAME = "./prizeList.txt";
//Instance variable
private Prize[] prizes;
private Prize[] showcase;
//Constructor
public Showcase()
{
showcase = new Prize[MAX_SHOWCASE];
prizes = new Prize[MAX_PRIZES];
readPrizeList(FILE_NAME);
populate();
printShowcase();
}
//Method to randomly populate the showcase array
public void populate()
{
Random r = new Random();
for(int i = 0; i < showcase.length; i++)
{
int index = r.nextInt(MAX_PRIZES);
if(prizes[index] != null)
{
showcase[i] = prizes[index];
}
}
}
//Method to add a prize to the prize array
public void addPrize(Prize aPrize)
{
for(int i = 0;i < prizes.length; i++)
{
if(prizes[i] == null)
{
prizes[i] = aPrize;
break;
}
}
}
//Method to print the showcase to the console
public void printShowcase()
{
for(Prize p : showcase)
{
if(p != null)
{
System.out.println(p.getName());
}
}
}
//Method that reads the file and populates the prize array
public Prize[] readPrizeList(String aFileName)
{
try
{
Scanner fileScanner = new Scanner(new File(aFileName));
while(fileScanner.hasNextLine())
{
String input = fileScanner.nextLine();
String[] splitLine = input.split(DELIM);
if(splitLine.length != FIELD_AMT)
{
continue;
}
addPrize(new Prize(splitLine[0], Double.parseDouble(splitLine[1])));
}
fileScanner.close();
return prizes;
}
catch(Exception e)
{
System.out.println(e);
return null;
}
}
//Add up all of the item's prices in the showcase
public double addShowcase()
{
double sum = 0.0;
for(Prize p : showcase)
{
sum += p.getPrice();
}
return sum;
}
}