diff --git a/assignment-02/H071191008/DataSource.java b/assignment-02/H071191008/DataSource.java new file mode 100644 index 0000000..edaa317 --- /dev/null +++ b/assignment-02/H071191008/DataSource.java @@ -0,0 +1,87 @@ +package OOP.Assignment02; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.HashMap; +import java.util.Map; + +public class DataSource { + + private static DataSource dataSource; + private Map userMap = new HashMap<>(); + private Map userDetailMap = new HashMap<>(); + + private DataSource() { + try{ + putUserDetail(); + putUser(); + } catch(Exception e) { + e.printStackTrace(); + } + } + + public static DataSource getInstance() { + if(dataSource == null){ + dataSource = new DataSource(); + } + return dataSource; + } + + private void putUserDetail() throws Exception{ + // This Method Reads data from the UserDetail.txt file, then saves the data + // into UserDetailMap + BufferedReader br = new BufferedReader(new FileReader("UserDetail.txt")); + String data[]; + while(br.ready()){ + data = br.readLine().split(";"); + //inputs user detail into user detail Map if the data starts with a valid id + if(isInteger(data[0])){ + userDetailMap.put(Integer.valueOf(data[0]), + new UserDetail(Integer.valueOf(data[0]), data[1], data[2], data[3])); + + } + } + data = null; + br.close(); + } + + private void putUser() throws Exception{ + // This Method Reads data from the User.txt file, then saves the data + // into UserMap + BufferedReader br = new BufferedReader(new FileReader("User.txt")); + String data[]; + while(br.ready()){ + data = br.readLine().split(";"); + // inputs user into userMap if the data starts with a valid id + if(isInteger(data[0])){ + userMap.put(data[1], new User(Integer.valueOf(data[0]), data[1], data[2], + userDetailMap.get(Integer.valueOf(data[0])))); + } + } + data = null; + br.close(); + } + + public User getUser(String key) { + return userMap.get(key); + } + public UserDetail getUserDetail(int key) { + return userDetailMap.get(key); + } + + public static boolean isInteger(String s) { + //This method checks if a string is an integer + boolean isValidInteger = false; + try { + Integer.parseInt(s); + // s is a valid integer + + isValidInteger = true; + } catch (NumberFormatException ex) { + // s is not an integer + } + + return isValidInteger; + } + +} \ No newline at end of file diff --git a/assignment-02/H071191008/Login.java b/assignment-02/H071191008/Login.java new file mode 100644 index 0000000..a8c57b1 --- /dev/null +++ b/assignment-02/H071191008/Login.java @@ -0,0 +1,58 @@ +package OOP.Assignment02; + +import java.util.NoSuchElementException; + +public class Login { + + private static Login login; + private User user; + private UserDetail userDetail; + private DataSource dataSource; + + private Login() { + dataSource = DataSource.getInstance(); + } + + public static Login getInstance() { + if(login == null){ + login = new Login(); + } + return login; + } + public void auth(String userName, String password) throws NoSuchElementException { + user = dataSource.getUser(userName); + try { + if(user.verifyPassword(password)){ + userDetail = user.getUserDetail(); + } else { + System.out.println("Invalid Password"); + } + } catch (Exception e) { + throw new NoSuchElementException("No such Username : " + userName); + } + } + + public void status() { + try{ + System.out.printf("Name\t\t: %s\n", userDetail.getName()); + System.out.printf("Email\t\t: %s\n", userDetail.getEmail()); + System.out.printf("Date of Birth\t: %s\n", userDetail.getDateOfBirth()); + System.out.printf("Username\t: %s\n", user.getUsername()); + System.out.printf("Password\t: %s\n", user.getPassword()); + } catch (Exception e) { + System.out.println("Not Logged in"); + } + } + + public void logout() { + if(user != null){ + user.logout(); + user = null; + userDetail = null; + } else { + System.out.println("Not logged in"); + } + + } + +} \ No newline at end of file diff --git a/assignment-02/H071191008/Main.java b/assignment-02/H071191008/Main.java new file mode 100644 index 0000000..83f3024 --- /dev/null +++ b/assignment-02/H071191008/Main.java @@ -0,0 +1,9 @@ +package OOP.Assignment02; + +import java.io.IOException; + +class Main { + public static void main(String[] args) throws IOException { + + } +} \ No newline at end of file diff --git a/assignment-02/H071191008/User.java b/assignment-02/H071191008/User.java new file mode 100644 index 0000000..a1131db --- /dev/null +++ b/assignment-02/H071191008/User.java @@ -0,0 +1,80 @@ +package OOP.Assignment02; + +public class User { + // This class contains the login details of a user + private int id; + private String userName; + private String password; + private UserDetail userDetail; + private boolean authenticated = false; // prevents access to vital information + // without authentication + + public User(int id, String userName, String password, UserDetail userDetail) { + this.id = id; + this.userName = userName; + this.password = password; + this.userDetail = userDetail; + } + + public UserDetail geUserDetail() { + if (authenticated) { + return userDetail; + } else { + System.out.println("Not Authenticated"); + return null; + } + } + + public int getId() { + if (authenticated) { + return id; + } else { + System.out.println("Not Authenticated"); + return 0; + } + } + + public boolean verifyPassword(String password) { + //this method authenticates the user if the password is correct + if(this.password.equals(password)) { + //if password is correct then authenticate the user + authenticated = true; + return true; + } else { + //if the password is incorrect then don't authenticate the user + return false; + } + } + + public String getUsername() { + if (authenticated) { + return userName; + } else { + System.out.println("Not Authenticated"); + return null; + } + } + + public UserDetail getUserDetail() { + if (authenticated) { + return userDetail; + } else { + System.out.println("Not Authenticated"); + return null; + } + } + + public String getPassword() { + if (authenticated) { + return password; + } else { + System.out.println("Not Authenticated"); + return null; + } + } + + public void logout() { + authenticated = false; + } + +} \ No newline at end of file diff --git a/assignment-02/H071191008/UserDetail.java b/assignment-02/H071191008/UserDetail.java new file mode 100644 index 0000000..dae95c1 --- /dev/null +++ b/assignment-02/H071191008/UserDetail.java @@ -0,0 +1,32 @@ +package OOP.Assignment02; + +public class UserDetail { + + private Integer id; + private String name; + private String dateOfBirth; + private String email; + + public UserDetail(Integer id, String name, String dateOfBirth, String email) { + this.id = id; + this.name = name; + this.dateOfBirth = dateOfBirth; + this.email = email; + } + + public Integer getId() { + return id; + } + + public String getName() { + return name; + } + + public String getDateOfBirth() { + return dateOfBirth; + } + + public String getEmail() { + return email; + } +} \ No newline at end of file