Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Account {
- String userID;
- String password;
- String permissions;
- // Defining object
- public Account(){} //No argument constructor
- public Account(String userID, String password, String permissions) {
- this.userID = userID;
- this.password = password;
- this.permissions = permissions;
- }
- public static ArrayList<Account> userList = new ArrayList<Account>();
- public static void userDB() throws FileNotFoundException {
- /*q
- * While loop to scan the entire file and stop when there are no more
- * lines to process. At the end of reading a line it creates an object
- * and adds it to an arraylist for later use.
- */
- Scanner file = new Scanner(new File("userDB.txt"));
- while (file.hasNext()) {
- String userID = file.next();
- String password = file.next();
- String permissions = file.next();
- userList.add(new Account(userID, password, permissions));
- //if any blank lines caused by writing to file this will ignore them.
- }
- file.close();
- }
- @Override
- public String toString() {
- StringBuilder builder = new StringBuilder();
- builder.append("userID: ").append(userID);
- builder.append(", Password: ").append(password);
- if (permissions.equals("1")){
- builder.append(", Role: Patient");}
- if (permissions.equals("2")){
- builder.append(", Role: Doctor");}
- if (permissions.equals("3")){
- builder.append(", Role: Admin");}
- builder.append("\n");
- return builder.toString();
- }
- void saveTo(PrintWriter save) {
- save.println(userID + " " + password + " " + permissions);
- // Have to create a different method of printing to file so the appends
- // in toString don't change the layout of the original file/
- }
- private static void saveData() throws FileNotFoundException {
- final PrintWriter save = new PrintWriter("rooms.txt");
- for (Account u : userList){
- u.saveTo(save);}
- save.close();
- System.out.println("Data saved successfully.");
- }
- private static void addAccount() throws FileNotFoundException{
- userDB();
- Scanner console = new Scanner(System.in);
- System.out.println("Please enter a new user ID (must be 4 numbers long)");
- String newUserID = console.next();
- System.out.println("Please enter a new password");
- String newPassword = console.next();
- System.out.println("Please enter a role (Only type a single number: 1, 2 or 3)");
- System.out.println("1. Patient");
- System.out.println("2. Doctor");
- System.out.println("3. Admin");
- String newRole = console.next();
- userList.add(new Account(newUserID, newPassword, newRole));
- saveData();
- console.close();
- }
- public static Account login() throws FileNotFoundException {
- userDB();
- System.out.println(userList);
- Scanner console = new Scanner(System.in);
- boolean loggedIn = false;
- System.out.println("Please Login.");
- System.out.println("Username: ");
- String userID = console.next();
- System.out.println("Password: ");
- String password = console.next();
- Account user = new Account();
- for (Account u: userList){
- if (u.userID.equals(userID) && u.password.equals(password)){
- loggedIn = true;
- System.out.println("Login Successful.");
- user.setRole(u.permissions);
- console.close();
- }
- break;
- }
- if (!loggedIn){
- System.out.println("Incorrect Login.");
- }
- return user;
- }
- public void setRole(String permissions){
- this.permissions = permissions;
- }
- public String getRole(){
- return permissions;
- }
- }
Add Comment
Please, Sign In to add comment