Summary HW6 Account Management In HW4 you kept track of mult
Summary
HW6: Account Management
In HW4, you kept track of multiple usernames and its associated password using arrays. However, usernames and passwords are typically part of a “User” object which in turn may be part of an “Account” object. Accounts also typically require a certain level of security beyond the typical encryption mechanisms. This assignment goes deeper into the concept of OOP as you create objects that requires proper rules and scope for correct usage.
Aside: As with Item objects from HW5, Account and User data is typically stored in databases.
Skills Expected
? All the skills from previous Assignment(s)
? Accessors/Mutators
? Overriding methods: equals and toString
Assignment Description
You will write three Class objects and a Driver for each class (i.e. submit six .java files):
? User
? Account
? AccountList
Note: All properties MUST be private
Submission Requirement: The Driver Class
? Each Class designed MUST be submitted with a corresponding “Driver” Class
? The Driver Class should have a main() that demonstrates, at minimum
o Calling the appropriate constructor to create the appropriate instance
o Everyproperty(instancevariables)canbesetandgetcorrectly(whereallowed) ? Every public method can be called successfully (and return the correct result)
Class Design: User
The User class is intended to be an abstract and simplified representation of a user
Class Properties
? First Name (String)
? Last Name (String)
? Username (String)
? Password (String)
Class Invariant
? First and Last Name must not be empty
? Username must be at least four characters long
? Password must be at least four characters long (is this a good invariant?)
Class Components
? A constructor that sets the initial user data (first name, last name, username, password)
? A getter/setter for each properties set out above
? A toString() method
? An equals() method
Class Design: Account
The Account class is intended to be an abstract and simplified representation of an account
Class Properties
? User (User)
? Balance (double) – represents how much money the user has in the account
Class Invariant
? Must be a valid account
? Balance must not be negative
Class Components
? A constructor that sets the initial User instance and balance amount
? A Getter but not a Setter for the each properties set out above (why?)
? A public method to add to the balance
? A public method to withdraw from the balance
? A toString() method
? An equals() method
Class Design: AccountList
The AccountList class is intended to be an abstract and simplified representation of a list of accounts.
Class Properties
? Accounts (an array of Account objects – or ArrayList) o No getters or setters* (do you know why?)
Class Invariant
? Can’t have multiple accounts with the same username
Class Components
? A public method that adds new accounts
? A public (boolean) method that determines whether an account with a given username exists in
the list
Grading Criteria
? User class object
o [2 points] Implements all required properties
? [2 points] Implements appropriate getters/setters
o [2points]Allinvariantsproperlyenforced
o [2points]Requiredclasscomponentsproperlyimplemented o [2points]Driverclassrequirementsaremet
? Account class object
o [2 points] Implements all required properties
? [1 point] Implements appropriate getters
o [2 points] All invariants properly enforced
o [2points]Requiredclasscomponentsproperlyimplemented o [2points]Driverclassrequirementsaremet
? AccountList class object
o [2 points] Implements all required properties
o [2 points] All invariants properly enforced
o [2points]Requiredclasscomponentsproperlyimplemented o [2points]Driverclassrequirementsaremet
? [1 point] Method JavaDoc: description, @param, @return
? [1 Point] Descriptive variable names
? [1 Point] Appropriate class header comment block
output
User
account
accountlist
Bluel: Terminal Window - HW6 Options toString: Name: Doe, John0 getUsername: joe0 getPassword: 1234 Changing firstName to Jane Changing lastName to Dae Changing username to jodie Changing password to supersecret toString: Name:Dae, Jane getUsername: jodie getPas3word: supersecret Comparison with itself: trueSolution
public class User {
private String firstName;
private String lastName;
private String userName;
private String Password;
/**
* @param firstName
* @param lastName
* @param userName
* @param password
*/
public User(String firstName, String lastName, String userName,
String password) {
try {
setFirstName(firstName);
setLastName(lastName);
setUserName(userName);
setPassword(password);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName
* the firstName to set
* @throws NoSuchFieldException
*/
public void setFirstName(String firstName) throws NoSuchFieldException {
if (firstName.length() == 0)
throw new NoSuchFieldException(\"first Name should not empty\");
else
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName
* the lastName to set
* @throws NoSuchFieldException
*/
public void setLastName(String lastName) throws NoSuchFieldException {
if (lastName.length() == 0)
throw new NoSuchFieldException(\"last Name should not empty\");
else
this.lastName = lastName;
}
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName
* the userName to set
* @throws NoSuchFieldException
*/
public void setUserName(String userName) throws NoSuchFieldException {
if (userName.length() < 4)
throw new NoSuchFieldException(
\"user name should contain atleas 4 characters\");
else
this.userName = userName;
}
/**
* @return the password
*/
public String getPassword() {
return Password;
}
/**
* @param password
* the password to set
* @throws NoSuchFieldException
*/
public void setPassword(String password) throws NoSuchFieldException {
if (password.length() < 4)
throw new NoSuchFieldException(
\"password should contain atleas 4 characters\");
else
this.Password = password;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((Password == null) ? 0 : Password.hashCode());
result = prime * result
+ ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result
+ ((lastName == null) ? 0 : lastName.hashCode());
result = prime * result
+ ((userName == null) ? 0 : userName.hashCode());
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (Password == null) {
if (other.Password != null)
return false;
} else if (!Password.equals(other.Password))
return false;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
if (userName == null) {
if (other.userName != null)
return false;
} else if (!userName.equals(other.userName))
return false;
return true;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return \"Name=\" + firstName + \", \" + lastName + \", userName=\" + userName;
}
}
public class Account {
private User user;
private double balance;
/**
* @param user
* @param balance
*/
public Account(User user, double balance) {
try {
setUser(user);
setBalance(balance);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @return the user
*/
public User getUser() {
return user;
}
/**
* @param user
* the user to set
*/
private void setUser(User user) {
this.user = user;
}
/**
* @return the balance
*/
public double getBalance() {
return balance;
}
/**
* @param balance
* the balance to set
* @throws NoSuchFieldException
*/
private void setBalance(double balance) throws NoSuchFieldException {
if (balance < 0)
throw new NoSuchFieldException(\"balance cannot be negative\");
else
this.balance = balance;
}
/**
* @param amount
* @throws NoSuchFieldException
*/
public void diposit(double amount) throws NoSuchFieldException {
if (amount < 0)
throw new NoSuchFieldException(\"amount cannot be negative\");
else
this.balance += amount;
}
/**
* @param amount
* @throws NoSuchFieldException
*/
public void withdraw(double amount) throws NoSuchFieldException {
if (amount < 0)
throw new NoSuchFieldException(\"amount cannot be negative\");
else
this.balance -= amount;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(balance);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((user == null) ? 0 : user.hashCode());
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Account other = (Account) obj;
if (Double.doubleToLongBits(balance) != Double
.doubleToLongBits(other.balance))
return false;
if (user == null) {
if (other.user != null)
return false;
} else if (!user.equals(other.user))
return false;
return true;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return \"Account [user=\" + user + \", balance=\" + balance + \"]\ \";
}
}
import java.util.ArrayList;
import java.util.List;
public class AccountList {
List<Account> accounts;
/**
*
*/
public AccountList() {
// TODO Auto-generated constructor stub
accounts = new ArrayList<Account>();
}
/**
* @param account
*/
public void addAccount(Account account) {
accounts.add(account);
}
/**
* @param username
* @return
*/
public boolean checkAccount(String username) {
for (int i = 0; i < accounts.size(); i++) {
if (accounts.get(i).getUser().getUserName().equals(username))
return true;
}
return false;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((accounts == null) ? 0 : accounts.hashCode());
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AccountList other = (AccountList) obj;
if (accounts == null) {
if (other.accounts != null)
return false;
} else if (!accounts.equals(other.accounts))
return false;
return true;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return \"AccountList [accounts=\" + accounts + \"]\";
}
}
public class TestUser {
public static void main(String[] args) throws NoSuchFieldException {
User user1 = new User(\"John0\", \"Doe\", \"joe0\", \"1234\");
System.out.println(\"getUserName:\" + user1.getUserName());
System.out.println(\"getPassword:\" + user1.getPassword());
System.out.println(\"changing firstname to Jane\");
user1.setFirstName(\"Jane\");
System.out.println(\"changing lastname to Dae\");
user1.setFirstName(\"Dae\");
System.out.println(\"changing username to jodie\");
user1.setUserName(\"jodie\");
System.out.println(\"changing password to supersecret\");
user1.setPassword(\"supersecret\");
System.out.println(\"getUserName:\" + user1.getUserName());
System.out.println(\"getPassword:\" + user1.getPassword());
System.out.println(\"Comparison with itself :\" + user1.equals(user1));
}
}
OUTPUT 1 :
getUserName:joe0
getPassword:1234
changing firstname to Jane
changing lastname to Dae
changing username to jodie
changing password to supersecret
getUserName:jodie
getPassword:supersecret
Comparison with itself :true
public class TestAccount {
public static void main(String[] args) {
try {
User user1 = new User(\"John0\", \"Doe\", \"joe0\", \"1234\");
Account account = new Account(user1, 100);
System.out.println(\"toString :\" + account.toString());
System.out.println(\"getUser() toString :\"
+ account.getUser().toString());
System.out.println(\"getBalance():\" + account.getBalance());
System.out.println(\"Deposit 50\");
account.diposit(50);
System.out.println(\"toString :\" + account.toString());
System.out.println(\"withdraw 110\");
account.withdraw(110);
System.out.println(\"toString :\" + account.toString());
System.out.println(\"Comparison with itself :\"
+ account.equals(account));
} catch (Exception e) {
// TODO: handle exception
}
}
}
OUTPUT 2:
toString :Account [user=Name=John0, Doe, userName=joe0, balance=100.0]
getUser() toString :Name=John0, Doe, userName=joe0
getBalance():100.0
Deposit 50
toString :Account [user=Name=John0, Doe, userName=joe0, balance=150.0]
withdraw 110
toString :Account [user=Name=John0, Doe, userName=joe0, balance=40.0]
Comparison with itself :true
public class TestAccountList {
public static void main(String[] args) {
AccountList accountList = new AccountList();
User user1 = new User(\"John0\", \"Doe\", \"joe0\", \"1234\");
User user2 = new User(\"John1\", \"Doe\", \"joe0\", \"1234\");
User user3 = new User(\"John2\", \"Doe\", \"joe0\", \"1234\");
User user4 = new User(\"John3\", \"Doe\", \"joe0\", \"1234\");
User user5 = new User(\"John4\", \"Doe\", \"joe0\", \"1234\");
accountList.addAccount(new Account(user1, 100));
accountList.addAccount(new Account(user2, 200));
accountList.addAccount(new Account(user3, 300));
accountList.addAccount(new Account(user4, 400));
accountList.addAccount(new Account(user5, 500));
System.out.println(accountList);
System.out.println(\"Comparison with itself :\"
+ accountList.equals(accountList));
}
}
OUTPUT 3:
AccountList [accounts=[Account [user=Name=John0, Doe, userName=joe0, balance=100.0]
, Account [user=Name=John1, Doe, userName=joe0, balance=200.0]
, Account [user=Name=John2, Doe, userName=joe0, balance=300.0]
, Account [user=Name=John3, Doe, userName=joe0, balance=400.0]
, Account [user=Name=John4, Doe, userName=joe0, balance=500.0]
]]
Comparison with itself :true










