Context: You will write an email system in two parts (exercise B02 and B03). Thi
ID: 3912247 • Letter: C
Question
Context: You will write an email system in two parts (exercise B02 and B03). This email system will have a collection of users, and these users can send emails between each other. In this first exercise, you will implement the email system with the functionality to add and remove users.
Important: You don't know how many users will sign up, so your collection is NOT fixed sized. There's no limit to how many users can sign up.
You need to: Create a User class. This class will have the following attributes: full name, username and password. All this information is passed in the constructor. - -
You must implement accessors and mutators.
Your User class must have a login method that receives an input username and an input password, compares them with the valid username and password of the user, and returns true if they match, and false otherwise.
Create an EmailSystem class. This class will have a dynamicsized collection of users. This collection will be initially empty.
You must implement a registerUser method that receives the user full name, username and password. Your method will create a User object and add it to the list of users in the system. You must implement a listUsers method that will iterate through the list of users in the system, and print their usernames in different lines.
Note: Use the main method of the EmailSystem class to thoroughly test your program. In this main method, you can hard code the values to test your implementation. That is, you don't have to ask the user for values using the Scanner or JavaFX dialogs. Your attributes should all be private attributes.
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
User.java
-----
public class User {
private String fullName;
private String username;
private String password;
public User(String fullName, String username, String password){
this.fullName = fullName;
this.username = username;
this.password = password;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean login(String uname, String pwd){
if(uname.equals(username) && pwd.equals(password))
return true;
else
return false;
}
}
EmailSystem.java
--------
public class EmailSystem {
private User[] users;
private int numUsers;
public EmailSystem(){
users = new User[3];//initially to hold upto 3 users
numUsers = 0;
}
public void registerUser(String fullName, String username, String password){
User u = new User(fullName, username, password);
if(numUsers == users.length){ // array full, expand it to double its capacity
User[] temp = new User[2 * users.length]; //double the capacity
for(int i = 0; i < numUsers; i++)
temp[i] = users[i];
users = temp;
}
users[numUsers] = u;
numUsers++;
}
public void listUsers(){
System.out.println("There are " + numUsers + " registered users. The usernames are");
for(int i = 0; i < numUsers; i++)
System.out.println(users[i].getUsername());
System.out.println();
}
public static void main(String[] args) {
EmailSystem es = new EmailSystem();
System.out.println("Registering 3 users");
es.registerUser("John Smith", "jsmith", "hello123");
es.registerUser("Alice", "alice", "apple344");
es.registerUser("Michael Jackson", "mjackson", "thriller");
es.listUsers();
System.out.println("Registering 2 users");
es.registerUser("Christina", "chris", "test123");
es.registerUser("Bob", "bob", "xyz123");
es.listUsers();
}
}
output
------
Registering 3 users
There are 3 registered users. The usernames are
jsmith
alice
mjackson
Registering 2 users
There are 5 registered users. The usernames are
jsmith
alice
mjackson
chris
bob
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.