Guest User

Account Java

a guest
Mar 28th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.PrintWriter;
  4. import java.util.ArrayList;
  5. import java.util.Scanner;
  6.  
  7.  
  8. public class Account {
  9.    
  10.     String userID;
  11.     String password;
  12.     String permissions;
  13.  
  14.  
  15.     // Defining object
  16.     public Account(){} //No argument constructor
  17.     public Account(String userID, String password, String permissions) {
  18.         this.userID = userID;
  19.         this.password = password;
  20.         this.permissions = permissions;
  21.     }
  22.    
  23.     public static ArrayList<Account> userList = new ArrayList<Account>();
  24.    
  25.     public static void userDB() throws FileNotFoundException {
  26.         /*q
  27.          * While loop to scan the entire file and stop when there are no more
  28.          * lines to process. At the end of reading a line it creates an object
  29.          * and adds it to an arraylist for later use.
  30.          */
  31.         Scanner file = new Scanner(new File("userDB.txt"));
  32.         while (file.hasNext()) {
  33.             String userID = file.next();
  34.             String password = file.next();
  35.             String permissions = file.next();
  36.             userList.add(new Account(userID, password, permissions));
  37.                 //if any blank lines caused by writing to file this will ignore them.
  38.         }
  39.         file.close();
  40.     }
  41.  
  42.     @Override
  43.     public String toString() {
  44.         StringBuilder builder = new StringBuilder();
  45.         builder.append("userID: ").append(userID);
  46.         builder.append(", Password: ").append(password);
  47.         if (permissions.equals("1")){
  48.             builder.append(", Role: Patient");}
  49.         if (permissions.equals("2")){
  50.             builder.append(", Role: Doctor");}
  51.         if (permissions.equals("3")){
  52.             builder.append(", Role: Admin");}
  53.         builder.append("\n");
  54.         return builder.toString();
  55.     }
  56.    
  57.     void saveTo(PrintWriter save) {
  58.         save.println(userID + " " + password + " " + permissions);
  59.         // Have to create a different method of printing to file so the appends
  60.         // in toString don't change the layout of the original file/
  61.     }
  62.    
  63.     private static void saveData() throws FileNotFoundException {
  64.         final PrintWriter save = new PrintWriter("rooms.txt");
  65.         for (Account u : userList){
  66.             u.saveTo(save);}
  67.         save.close();
  68.         System.out.println("Data saved successfully.");
  69.     }
  70.    
  71.     private static void addAccount() throws FileNotFoundException{
  72.         userDB();
  73.         Scanner console = new Scanner(System.in);
  74.         System.out.println("Please enter a new user ID (must be 4 numbers long)");
  75.         String newUserID = console.next();
  76.         System.out.println("Please enter a new password");
  77.         String newPassword = console.next();
  78.         System.out.println("Please enter a role (Only type a single number: 1, 2 or 3)");
  79.         System.out.println("1. Patient");
  80.         System.out.println("2. Doctor");
  81.         System.out.println("3. Admin");
  82.         String newRole = console.next();
  83.         userList.add(new Account(newUserID, newPassword, newRole));
  84.         saveData();
  85.         console.close();
  86.     }
  87.    
  88.    
  89.     public static Account login() throws FileNotFoundException {
  90.         userDB();
  91.         System.out.println(userList);
  92.         Scanner console = new Scanner(System.in);
  93.  
  94.  
  95.             boolean loggedIn = false;
  96.             System.out.println("Please Login.");
  97.             System.out.println("Username: ");
  98.             String userID = console.next();
  99.             System.out.println("Password: ");
  100.             String password = console.next();
  101.             Account user = new Account();
  102.            
  103.                 for (Account u: userList){
  104.                     if (u.userID.equals(userID) && u.password.equals(password)){
  105.                         loggedIn = true;
  106.                         System.out.println("Login Successful.");
  107.                         user.setRole(u.permissions);
  108.                        
  109.                         console.close();
  110.                         }
  111.                     break;
  112.                     }
  113.                 if (!loggedIn){
  114.                         System.out.println("Incorrect Login.");
  115.                     }
  116.                
  117.                    return user;
  118.     }
  119.    
  120.     public void setRole(String permissions){
  121.         this.permissions = permissions;
  122.     }
  123.    
  124.     public String getRole(){
  125.         return permissions;
  126.     }
  127.  
  128. }
Add Comment
Please, Sign In to add comment