-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBowlerFile.java
109 lines (95 loc) · 2.75 KB
/
BowlerFile.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
107
108
109
/* BowlerFile.java
*
* Version:
* $Id$
*
* Revisions:
* $Log: BowlerFile.java,v $
* Revision 1.5 2003/02/02 17:36:45 ???
* Updated comments to match javadoc format.
*
* Revision 1.4 2003/02/02 16:29:52 ???
* Added ControlDeskEvent and ControlDeskObserver. Updated Queue to allow access to Vector so that contents could be viewed without destroying. Implemented observer model for most of ControlDesk.
*
*
*/
/**
* Class for interfacing with Bowler database
*
*/
import java.util.*;
import java.io.*;
class BowlerFile implements BowlerDatabase {
/** The location of the bowelr database */
private static String BOWLER_DAT = "BOWLERS.DAT";
private BowlerFile() {}
private static class LazyHolder {
static final BowlerFile INSTANCE = new BowlerFile();
}
public static BowlerFile getInstance() {
return LazyHolder.INSTANCE;
}
/**
* Retrieves bowler information from the database and returns a Bowler objects with populated fields.
*
* @param nickName the nickName of the bolwer to retrieve
*
* @return a Bowler object
*
*/
private static List<String[]> readBowlersFromFile() throws IOException {
List<String[]> bowlers = new ArrayList<>();
try (BufferedReader in = new BufferedReader(new FileReader(BOWLER_DAT))) {
String data;
while ((data = in.readLine()) != null) {
// File format is nick\tfname\te-mail
String[] bowler = data.split("\t");
bowlers.add(bowler);
}
}
return bowlers;
}
@Override
public Bowler getBowlerInfo(String nickName) throws IOException {
for (String[] bowler : readBowlersFromFile()) {
if (nickName.equals(bowler[0])) {
return new Bowler(bowler[0], bowler[1], bowler[2]);
}
}
return null;
}
/**
* Stores a Bowler in the database
*
* @param nickName the NickName of the Bowler
* @param fullName the FullName of the Bowler
* @param email the E-mail Address of the Bowler
*
*/
@Override
public void putBowlerInfo(
String nickName,
String fullName,
String email)
throws IOException, FileNotFoundException {
String data = nickName + "\t" + fullName + "\t" + email + "\n";
RandomAccessFile out = new RandomAccessFile(BOWLER_DAT, "rw");
out.skipBytes((int) out.length());
out.writeBytes(data);
out.close();
}
/**
* Retrieves a list of nicknames in the bowler database
*
* @return a Vector of Strings
*
*/
@Override
public List<String> getBowlers() throws IOException {
List<String> allBowlers = new ArrayList<>();
for (String[] bowler : readBowlersFromFile()) {
allBowlers.add(bowler[0]);
}
return allBowlers;
}
}