diff --git a/PRAKTIKUM/Run.java b/PRAKTIKUM/Run.java new file mode 100644 index 0000000..c6522f0 --- /dev/null +++ b/PRAKTIKUM/Run.java @@ -0,0 +1,58 @@ +import java.util.*; +public class Run{ + public static void main(String[] args) { + Map facultyMap = new HashMap<>(); + Map majorMap = new HashMap<>(); + facultyMap.put("Nama Fakultas Lain", "A"); + facultyMap.put("Nama Fakultas Lain", "B"); + facultyMap.put("Nama Fakultas Lain", "C"); + facultyMap.put("Nama Fakultas Lain", "D"); + facultyMap.put("Nama Fakultas Lain", "E"); + facultyMap.put("Nama Fakultas Lain", "F"); + facultyMap.put("Nama Fakultas Lain", "G"); + facultyMap.put("MIPA", "H"); + majorMap.put("Prodi Lain", "01"); + majorMap.put("Prodi Lain", "02"); + majorMap.put("Prodi Lain", "03"); + majorMap.put("Prodi Lain", "04"); + majorMap.put("Prodi Lain", "05"); + majorMap.put("Prodi Lain", "06"); + majorMap.put("Ilmu Komputer", "07"); + + Student student1 = new Student(); + Student student2 = new Student(); + Student student3 = new Student(); + + student1.setFirstName("Muh. Taufiq"); + student1.setLastName("Arifin"); + student1.setRegisterYear(2017); + student1.setFaculty("MIPA"); + student1.setDepartment("Matematika"); + student1.setMajor("Ilmu Komputer"); + student1.setId(facultyMap, majorMap); + student1.setEmail(facultyMap); + + student2.setFirstName("IKSORA"); + student2.setLastName(""); + student2.setRegisterYear(2017); + student2.setFaculty("MIPA"); + student2.setDepartment("Matematika"); + student2.setMajor("Ilmu Komputer"); + student2.setId(facultyMap, majorMap); + student2.setEmail(facultyMap); + + student3.setFirstName("Ayu Farah Diba"); + student3.setLastName("Hamzah"); + student3.setRegisterYear(2017); + student3.setFaculty("MIPA"); + student3.setDepartment("Matematika"); + student3.setMajor("Ilmu Komputer"); + student3.setId(facultyMap, majorMap); + student3.setEmail(facultyMap); + + student1.description(); + student2.description(); + student3.description(); + + } +} \ No newline at end of file diff --git a/PRAKTIKUM/Student.java b/PRAKTIKUM/Student.java new file mode 100644 index 0000000..deed0ef --- /dev/null +++ b/PRAKTIKUM/Student.java @@ -0,0 +1,77 @@ +import java.util.Map; +import java.util.Random; +public class Student{ + private String id; + private String firstName; + private String lastName; + + private String email; + private int registerYear; + private String faculty; + private String departemen; + private String major; + + public void setId(MapfacultyMap, MapmajorMap){ + Random randoms = new Random(); + this.id = facultyMap.get(faculty) + majorMap.get(major) + "1" + String.valueOf(registerYear).substring(2) + "1" + String.format("%03d", randoms.nextInt(061)); + } + public String getId(){ + return id; + } + public void setFirstName(String firstName){ + + this.firstName = firstName; + } + public void setLastName(String lastName){ + this.lastName = lastName; + } + public String getFullName(){ + String fullName[] = (firstName.toLowerCase() + " " + lastName.toLowerCase()).split(" "); + String Name = ""; + for (int i = 0; i < fullName.length;i++) { + Name+= fullName[i].substring(0,1).toUpperCase()+ fullName[i].substring(1) + " "; + } + return Name; + } + public void setEmail(MapfacultyMap){ + String fullName[] = (firstName.toLowerCase() + " " + lastName.toLowerCase()).split(" "); + String Name = ""; + for (int i = 0; i < fullName.length -1;i++) { + Name+= fullName[i].substring(0,1); + } + this.email = (fullName[fullName.length - 1]+ Name + String.valueOf(registerYear).substring(2) + facultyMap.get(faculty) + "@gmail.com").toLowerCase(); + } + public String getEmail(String email){ + return email; + } + public void setRegisterYear(Integer registerYear){ + this.registerYear = registerYear; + } + public void setFaculty(String faculty){ + this.faculty = faculty; + } + public String getFaculty(String faculty){ + return faculty; + } + public void setDepartment(String departemen){ + this.departemen = departemen; + } + public String getDepartment(String departemen){ + return departemen; + } + public void setMajor(String major){ + this.major = major; + } + public String getMajor(){ + return major; + } + public void description() { + System.out.println("Nama : " + getFullName() ); + System.out.println("NIM : " + getId()); + System.out.println("Email Mahasiswa : " + getEmail(email)); + System.out.println("Fakultas : " + getFaculty(faculty)); + System.out.println("Departemen : " + getDepartment(departemen)); + System.out.println("Program Studi : " + getMajor()); + System.out.println(); + } +} \ No newline at end of file diff --git a/assignment-02/DataSource.java b/assignment-02/DataSource.java new file mode 100644 index 0000000..4fe76f3 --- /dev/null +++ b/assignment-02/DataSource.java @@ -0,0 +1,86 @@ +import java.util.HashMap; +import java.util.Map; +import java.io.FileReader; +import java.io.BufferedReader; + + +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/Login.java b/assignment-02/Login.java new file mode 100644 index 0000000..150ec5a --- /dev/null +++ b/assignment-02/Login.java @@ -0,0 +1,55 @@ +import java.util.*; +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/Main.java b/assignment-02/Main.java new file mode 100644 index 0000000..973180f --- /dev/null +++ b/assignment-02/Main.java @@ -0,0 +1,15 @@ +public class Main { + public static void main(String[] args) throws Exception{ + Login login = Login.getInstance(); + + login.auth("naim", "Naim"); + login.status(); + login.logout(); + + System.out.println(""); + + login.auth("Farha", "parhan"); + login.status(); + login.logout(); + } +} \ No newline at end of file diff --git a/assignment-02/User.java b/assignment-02/User.java new file mode 100644 index 0000000..1a9497e --- /dev/null +++ b/assignment-02/User.java @@ -0,0 +1,78 @@ +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/User.txt b/assignment-02/User.txt new file mode 100644 index 0000000..70d291f --- /dev/null +++ b/assignment-02/User.txt @@ -0,0 +1,5 @@ +1;fitrh;12345678 +2;arzk;qwerty +3;ken;asdf +4;naim;Naim +5;farhan;Parhan \ No newline at end of file diff --git a/assignment-02/UserDetail.java b/assignment-02/UserDetail.java new file mode 100644 index 0000000..d07db4c --- /dev/null +++ b/assignment-02/UserDetail.java @@ -0,0 +1,29 @@ +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 diff --git a/assignment-02/UserDetail.txt b/assignment-02/UserDetail.txt new file mode 100644 index 0000000..908948c --- /dev/null +++ b/assignment-02/UserDetail.txt @@ -0,0 +1,5 @@ +1;Muhammad Fitrah;04-09-1998;fitrahm17h@student.unhas.ac.id +2;Muhammad Arizki;00-00-0000;arizkim17h@student.unhas.ac.id +3;Kennedy;00-02-2000;kennedy17h@student.unhas.ac.id +4;Muhammad Muflihun Naim;00-00-0000;naimmm17h@student.unhas.ac.id +5;Farhan Ramdhani;00-00-0000;ramdhanif17h@student.unhas.ac.id \ No newline at end of file