-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.java
More file actions
199 lines (174 loc) · 4.26 KB
/
Utility.java
File metadata and controls
199 lines (174 loc) · 4.26 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* A class of static Utility methods generally dealing with validation.
*
* @author Joshua
* @version 1.0
*/
public class Utility {
/**
* Takes a String and adds a margin of the specified width onto the sides of the String.
*
* @param marginWidth
* The width in spaces of the margin
*
* @param s
* The String the margin is to be added to
*
* @return The newly formated String
* @deprecated This method ended up being unnecessary for what I wanted.
*/
public static String addMargins(int marginWidth, String s)
{
String result = "";
String margin = "" ;
String[] splits = s.split("\n", s.length());
for(int i = 0; i < marginWidth; i++)
margin += " ";
for(String split : splits)
result += margin + split + margin + "\n";
return result;
}
/**
* Checks to make sure that the String passed in actually has a meaningful value.
*
* @param s
* The String to be validated
*
* @return A boolean signifying whether the value passed in is valid.
*/
public static boolean validateString(String s) {
if(s != null){
int count = 0;
for(int i=0; i < s.length(); i++)
if(s.charAt(i) != ' ')
count ++;
return (count > 0);
}
return false;
}
/**
* Checks to see if the book id given is allowed to be deleted.
*
* @param id
* The Id of the book to be validated
*
* @return A boolean signifying whether the value passed in is valid.
*/
public static boolean validateBookDelete(int id) {
DB mydb = new DB();
mydb.updateDatabase();
boolean exists = false;
for(Book b: mydb.listBooks(1))
{
if(b.getBookId() == id && b.isAvailable())
exists = true;
}
return exists;
}
/**
* checks to make sure the int passed in is non-negative.
*
* @param num
* The int to be validated
*
* @return A boolean signifying whether the value passed in is valid.
*/
public static boolean validateInt(int num) {
return num >= 0;
}
/**
* Checks to see if the member id given is allowed to be deleted.
*
* @param id
* The Id of the member to be validated
*
* @return A boolean signifying whether the value passed in is valid.
*/
public static boolean validateMemberDelete(int id) {
DB mydb = new DB();
mydb.updateDatabase();
boolean exists = false;
for(Member m: mydb.listMembers(1))
{
if(m.getMemberId() == id && m.getBooksOut() == 0 && m.getLateFeesDue() < .001)
exists = true;
}
return exists;
}
/**
* Checks to see if the member id given exists in the database.
*
* @param id
* the Id of the member to be validated
*
* @return A boolean signifying whether the value passed in is valid.
*/
public static boolean validateMemberId(int id) {
DB mydb = new DB();
mydb.updateDatabase();
boolean exists = false;
for(Member m: mydb.listMembers(1))
{
if(m.getMemberId() == id)
exists = true;
}
return exists;
}
/**
* Checks to see if the member id and book id given are valid for a borrow.
*
* @param mId
* The Id of the member to be validated
*
* @param bId
* The Id of the book to be validated
*
* @return A boolean signifying whether the value passed in is valid.
*/
public static boolean borrowAllowed(int mId, int bId) {
DB mydb = new DB();
mydb.updateDatabase();
boolean b = false;
if(validateMemberId(mId) && validateBookId(bId))
b = mydb.isBorrowAllowed(mId, bId);
return b;
}
/**
* Checks to see if the book id given exists in the database.
*
* @param id
* The Id of the book to be validated
*
* @return A boolean signifying whether the value passed in is valid.
*/
public static boolean validateBookId(int id) {
DB mydb = new DB();
mydb.updateDatabase();
boolean exists = false;
for(Book b: mydb.listBooks(1))
{
if(b.getBookId() == id)
exists = true;
}
return exists;
}
/**
* Checks to see if the book id given is valid for a return.
*
* @param id
* The Id of the book to be validated
*
* @return A boolean signifying whether the value passed in is valid.
*/
public static boolean validateBookReturn(int id) {
DB mydb = new DB();
mydb.updateDatabase();
boolean exists = false;
for(Book b: mydb.listBooks(1))
{
if(b.getBookId() == id && !b.isAvailable())
exists = true;
}
return exists;
}
}