package com.cst242.finalproject.server.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * This class implements the {@code FileIOInterface} refer to that for
 * documentation.
 *
 * @author James DeCarlo
 */
public class FileIO implements FileIOInterface {

    @Override
    public boolean register(String loginId, int hashedPassword, String firstName, String lastName, String screenName) {
        File file = new File("userlist.dat");
        if (file.exists()) {
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
                
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
                
                
                int accountNumber = 1000;
                
                while (objectInputStream.available() > 0) {
                    User user = (User) objectInputStream.readObject();
                    if (user.getAccountNumber() > accountNumber) {
                        accountNumber = user.getAccountNumber();
                    }  
                }
                
                accountNumber++;
                User user = new User(accountNumber, loginId, hashedPassword, firstName, lastName, screenName);
                
                //Writing the User object to the file.
                objectOutputStream.writeObject(user);
                
                fileInputStream.close();
                objectInputStream.close();
                fileOutputStream.close();
                objectOutputStream.close();
            }
            catch(Exception e){
                return false;
            }

        }

        return true;
    }

    @Override
    public boolean deregister(int accountNumber) {
        return false;
    }

    @Override
    public User loginUser(String loginId, int hashedPassword) {
        User user = new User(1001, "JohnDoe", 6969, "John", "Doe", "SomeGuy");

        return user;
    }

    @Override
    public boolean updateUser(int accountNumber, int hashedPassword, String firstName, String lastName, String screenName) {
        return false;
    }

}

@@ -574,42 +574,8 @@
 );%0D%0A
-        if (file.exists()) %7B%0D%0A    
     
@@ -593,28 +593,24 @@
             
-    
 FileInputStr
@@ -651,36 +651,32 @@
 tStream(file);%0D%0A
-    
             Obje
@@ -750,38 +750,18 @@
 ream);%0D%0A
-                %0D%0A    
+%0D%0A
         
@@ -821,36 +821,32 @@
 tStream(file);%0D%0A
-    
             Obje
@@ -928,48 +928,10 @@
 );%0D%0A
-                %0D%0A                %0D%0A    
+%0D%0A
     
@@ -965,36 +965,18 @@
  1000;%0D%0A
-                %0D%0A  
+%0D%0A
         
@@ -979,18 +979,16 @@
         
-  
 while (o
@@ -1020,28 +1020,24 @@
 e() %3E 0) %7B%0D%0A
-    
             
@@ -1108,20 +1108,16 @@
         
-    
 if (user
@@ -1168,36 +1168,32 @@
                 
- 
-   
 accountNumber = 
@@ -1238,21 +1238,11 @@
     
-    %7D  %0D%0A    
+%7D%0D%0A
     
@@ -1252,38 +1252,18 @@
      %7D%0D%0A
-                %0D%0A    
+%0D%0A
         
@@ -1284,20 +1284,16 @@
 ber++;%0D%0A
-    
         
@@ -1392,38 +1392,18 @@
 Name);%0D%0A
-                %0D%0A    
+%0D%0A
         
@@ -1450,36 +1450,32 @@
 e.%0D%0A            
-    
 objectOutputStre
@@ -1497,32 +1497,16 @@
 user);%0D%0A
-                
 %0D%0A      
@@ -1507,28 +1507,24 @@
             
-    
 fileInputStr
@@ -1529,36 +1529,32 @@
 tream.close();%0D%0A
-    
             obje
@@ -1585,28 +1585,24 @@
             
-   
- 
 fileOutputSt
@@ -1608,36 +1608,32 @@
 tream.close();%0D%0A
-    
             obje
@@ -1661,33 +1661,32 @@
 );%0D%0A            
-%7D
 %0D%0A            ca
@@ -1683,17 +1683,16 @@
     
-   
+%7D
  catch
+ 
 (Exc
@@ -1700,23 +1700,20 @@
 ption e)
+ 
 %7B%0D%0A
-    
         
@@ -1731,33 +1731,16 @@
 false;%0D%0A
-            %7D%0D%0A%0D%0A
         
@@ -1779,107 +1779,8 @@
 %0D%0A%0D%0A
-    @Override%0D%0A    public boolean deregister(int accountNumber) %7B%0D%0A        return false;%0D%0A    %7D%0D%0A%0D%0A
     

test commit made change to the FILE IO