-
Notifications
You must be signed in to change notification settings - Fork 0
/
OwingsSodNotZod[UserAccounts]
200 lines (164 loc) · 6.37 KB
/
OwingsSodNotZod[UserAccounts]
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
200
//AUTHOR: [Richard Owings Jr.]
//COURSE: CPT187
//PURPOSE: [Provides the place to where usernames and passwords can be stored. Allows the user to create a login and
//PURPOSE: password for a user account, and will notify them if there is already a user account created.]
//STARTDATE: [12/03/2020]
package edu.cpt187.owings.exercise6;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
public class UserAccounts
{ //START of UserAccounts Class
//Declaration & Initialization of CONSTANTS and Variables for UserAccounts Class
private final int NOT_FOUND = -1; //Fixed constant for NOT_FOUND
private final int RESET_VALUE = 0; //Fixed constant for RESET_VALUE
private final int MAXIMUM_RECORDS = 50; //Fixed constant for MAXIMUM_RECORDS
private String[] userNames = new String[MAXIMUM_RECORDS]; //String array for userNames set to maximum records
private String[] passwords = new String[MAXIMUM_RECORDS]; //String array for passwords set to maximum records
private String masterFileName = ""; //String variable for masterFileName
private int recordCount = 0; //Integer variable for recordCount
private int searchedIndex = 0; //Integer variable for searchedIndex
//Declaration of UserAccounts
public UserAccounts(String borrowedFileName)
{ //START of UserAccounts
//assigns UserAccounts to borrowedFileName
masterFileName = borrowedFileName;
} //END of UserAccounts
//set method
//this method will assign a new value to setUserAccountArrays
public void setUserAccountArrays()
{ //START of setUserAccountArrays
//TRY/CATCH STRUCTURE for setUserAccountArrays
try
{ //START of try
//reset record count
recordCount = RESET_VALUE;
//this instantiation statement will attempt to open the file
Scanner infile = new Scanner (new FileInputStream(masterFileName));
//while loop to look for the next element in the array
while(infile.hasNext() == true && recordCount < MAXIMUM_RECORDS)
{ //START of while loop for field assignment
//assigns next String field to userNames array element
userNames[recordCount] = infile.next();
//assigns next String field to itemNames array element
passwords[recordCount] = infile.next();
//increment the recordCount
recordCount++;
} //END of while loop for field assignment
//Close the Scanner/File
infile.close();
}//END of try
catch(IOException ex)
{ //START of catch
//if the file is not found or opened, catch is executed and will see the flag to "not found"
recordCount = NOT_FOUND;
} //END of catch
} //END of setUserAccountArrays
//set method
//this method will assign a new value to searchIndex
public void setSearchedIndex(String borrowedUserName)
{ //START of setSearchedIndex
//assigns getSeqSearch(borrowedUsername) to searchedIndex
searchedIndex = getSeqSearch(borrowedUserName);
} //END of setSearchedIndex
//set method
//this overloaded method will assign a new value to searchedIndex
public void setSearchedIndex(String borrowedUserName, String borrowedPassword)
{ //START of overloaded method of setSearchedIndex
//assigns getSeqSearch(borrowedUsername) to searchedIndex
searchedIndex = getSeqSearch(borrowedUserName);
//IF conditional statement for searchedIndex & borrowedPassword
if(searchedIndex >= RESET_VALUE && getPasswordMatch(borrowedPassword) == false)
{ //START of if statement for searchedIndex & borrowedPassword
//assigns NOT_FOUND to searchedIndex
searchedIndex = NOT_FOUND;
} //END of if statement for searchedIndex & borrowedPassword
} //END of setSearchedIndex
public void setWriteOneRecord(String borrowedUserName, String borrowedPassword)
{ //START of setWriteOneRecord
//TRY/CATCH STRUCTURE for setWriteOneRecord
try
{ //START of try
//this instantiation statement will attempt to open the file
PrintWriter filePW = new PrintWriter (new FileWriter(masterFileName, true));
//this printf statement writes the borrowed values to the file, separated by tabs
filePW.printf("%n%1s\t%1s", borrowedUserName, borrowedPassword);
//increment recordCount
recordCount++;
//Close the PrintWrite/File
filePW.close();
} //END of TRY
catch(IOException ex)
{ //START of CATCH
//set recordCount to "not found"
recordCount = NOT_FOUND;
} //END OF CATCH
} //END of setWriteOneRecord
//get method
//this method will return getSeqSearch
public int getSeqSearch(String borrowedBorrowedUserName)
{//START of getSeqSearch
//Declare & Initialize localIndex & localFound
int localIndex = 0;
int localFound = NOT_FOUND;
//while loop for localIndex < recordCount
while (localIndex < recordCount)
{ //START of while loop for localIndex < recordCount
//if conditional statement
if(borrowedBorrowedUserName.equalsIgnoreCase(userNames[localIndex]))
{ //START of if statement for borrowedBorrowedSubject
//assigns localFound to localIndex
localFound = localIndex;
//assigns localIndex to recordCount
localIndex = recordCount;
}
else
{
//increments localIndex
localIndex++;
}//END of if statement for borrowedBorrowedUserName
}//END of while loop for localIndex < recordCount
//
// //increment recordCount
// recordCount++;
//returns localFound
return localFound;
}//END of getSeqSearch
//get method
//this method will return getPasswordMatch
public boolean getPasswordMatch(String borrowedBorrowedPassword)
{ //START of getPasswordMatch
//returns the value the password if it matches whats stored
return borrowedBorrowedPassword.equals(passwords[searchedIndex]);
} //END of getPasswordMatch
//get method
//this method will return getFileName
public String getFileName()
{ //START of getFileName
//returns file name
return masterFileName;
} //END of getFileName
//get method
//this method returns the max records
public int getMaximumRecords()
{ //START of getMaxRecords
//returns maxRecords
return MAXIMUM_RECORDS;
}//END of getMaxRecords
//get method
//this method returns the record count
public int getRecordCount()
{ //START of getRecordCount
//return recordCount
return recordCount;
}//END of getRecordCount
//get method
//this method returns the searched index
public int getSearchedIndex()
{ //START of getSearchedIndex
//returns SearchedIndex
return searchedIndex;
}//END of getSearchedIndex
} //END of UserAccounts Class