I'm trying to get better at java by testing myself with different small projects as opposed to reading about them in a book. I've made a simple login/register system to try get my head around inheritance and abstract classes. All my code is below. Feel free to critique the code and tell me what you would do better. My reason for doing this is I just want to see how other people would approach this sort of project and hopefully I'll learn something.
//RegLogSysDriver.java
public class RegLogSysDriver {
public static void main(String[] args) {
RegLogSystem system = new RegLogSystem();
system.run();
}
}
//RegLogSystem.java
import java.util.Scanner;
public class RegLogSystem {
//Instance variables
Scanner input = new Scanner(System.in);
String regOrLogInput;
String name, dateOfBirth, email, password;
//Methods
public void run() {
boolean keepGoing = true;
while(keepGoing==true) {
System.out.println("Do you wish to (l)ogin or (r)egister");
regOrLogInput = input.nextLine();
if(regOrLogInput.contentEquals("l")) {
System.out.println("Enter email:\n");
email = input.nextLine();
System.out.println("Enter password:\n");
password = input.nextLine();
LoginForm lForm = new LoginForm(email, password);
lForm.executeForm();
}else if(regOrLogInput.contentEquals("r")) {
System.out.println("Enter name:\n");
name = input.nextLine();
System.out.println("Enter date of birth e.g. (03/04/2000):\n");
dateOfBirth = input.nextLine();
System.out.println("Enter email:\n");
email = input.nextLine();
System.out.println("Enter password:\n");
password = input.nextLine();
RegisterForm rForm = new RegisterForm(name, dateOfBirth, email, password);
rForm.executeForm();
}
System.out.println("Do you wish to shutdown the system, (y)es or (n)o?");
String inputText = input.nextLine();
if(inputText.contentEquals("y")) {
keepGoing = false;
}
}
}
}
//LoginForm.java
public class LoginForm extends Form{
public LoginForm(String email, String password) {
super(email, password);
}
@Override
public void executeForm() {
if(getDataBase().isUserRegistered(getEmail(), getPassword())) {
User newUser = getDataBase().getUser(getEmail(), getPassword());
WelcomePage welPage = new WelcomePage(newUser);
welPage.outputMessage();
welPage.logoutMessage();
}else {
//put error handling here in time.
System.out.println("User is not registered");
}
}
}
//RegisterForm.java
public class RegisterForm extends Form{
private String name;
private String dateOfBirth;
public RegisterForm(String name, String dateOfBirth, String email, String password) {
super(email, password);
this.name = name;
this.dateOfBirth = dateOfBirth;
}
public String getName() {
return this.name;
}
public String getDateOfBirth() {
return this.dateOfBirth;
}
@Override
public void executeForm() {
User newUser = new User(getName(), getDateOfBirth(), getEmail(), getPassword());
getDataBase().addUserToDatabase(newUser);
WelcomePage welPage = new WelcomePage(newUser);
welPage.outputMessage();
welPage.logoutMessage();
}
}
//Form.java
public abstract class Form {
private String emailEntry;
private String passwordEntry;
private Database dataBase;
protected Form(String emailEntry, String passwordEntry) {
this.dataBase = new Database();//both forms are going to need access to the same database
this.emailEntry = emailEntry;
this.passwordEntry = passwordEntry;
}
public String getEmail() {
return this.emailEntry;
}
public String getPassword() {
return this.passwordEntry;
}
public Database getDataBase() {
return this.dataBase;
}
public abstract void executeForm();
}
//User.java
public class User {
private String name;
private String dateOfBirth;
private String email;
private String password;
public User(String name, String dateOfBirth, String email, String password) {
this.name = name;
this.dateOfBirth = dateOfBirth;
this.email = email;
this.password = password;
}
public User() {
}
public String getEmail() {
return this.email;
}
public String getPassword() {
return this.password;
}
public String getName() {
return this.name;
}
public String getDateOfBirth() {
return this.dateOfBirth;
}
}
//Database.java
import java.util.ArrayList;
public class Database {
private static ArrayList<User> dataBase = new ArrayList<User>();
static {
dataBase.add(new User("Tom", "12/02/09", "[email protected]", "pass"));
dataBase.add(new User("Bryan", "01/01/01", "[email protected]", "password"));
dataBase.add(new User("Tarence", "15/22/20", "[email protected]", "passwordismypassword"));
}
public boolean isUserRegistered(String email, String password) {
boolean returnVal = false;
for(User regedUser : dataBase) {
if(regedUser.getEmail().contentEquals(email)&®edUser.getPassword().contentEquals(password)) {
returnVal = true;
}
}
return returnVal;
}
public User getUser(String email, String password) {
User returnVal = new User();
for(User regedUser : dataBase) {
if(regedUser.getEmail().contentEquals(email)&®edUser.getPassword().contentEquals(password)) {
returnVal = regedUser;
}
}
return returnVal;
}
public void addUserToDatabase(User newUser) {
dataBase.add(newUser);
}
}
//WelcomePage.java
import java.util.Scanner;
public class WelcomePage {
private User loggedInUser;
private Scanner input = new Scanner(System.in);
public WelcomePage(User loggingInUser) {
this.loggedInUser = loggingInUser;
}
public void outputMessage() {
System.out.println("Hi "+loggedInUser.getName());
}
public void logoutMessage() {
boolean keepGoing = true;
while(keepGoing==true) {
System.out.println("Do you wish to logout, (y)es or (n)o");
String inputText = input.nextLine();
if(inputText.contentEquals("y")) {
keepGoing=false;
}
}
}
}