-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add money order feature for Davis lesson 17 with better organized code and all test passing #617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
feat: add money order feature for Davis lesson 17 with better organized code and all test passing #617
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1065d42
feat: add saving account for Bank ATM HW (incomplete)
DavisD251 46beb05
feat: added additional code to get requirement 1 (saving account) pas…
DavisD251 108b1d9
chore:altered code to be more efficient
DavisD251 004854d
Merge branch 'code-differently:main' into Work17
DavisD251 d018398
feat: add money order feature for Davis lesson 17 with better organiz…
DavisD251 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAccount.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; | ||
import java.util.Set; | ||
|
||
/** Represents a account. */ | ||
abstract class BankAccount { | ||
|
||
protected static Set<Customer> owners = null; | ||
protected final String accountNumber; | ||
protected double balance; | ||
protected boolean isActive; | ||
|
||
/** | ||
* Creates a new Bank account. | ||
* | ||
* @param accountNumber The account number. | ||
* @param owners The owners of the account. | ||
* @param initialBalance The initial balance of the account. | ||
*/ | ||
public BankAccount(String accountNumber, Set<Customer> owners, double initialBalance) { | ||
this.accountNumber = accountNumber; | ||
this.owners = owners; | ||
this.balance = initialBalance; | ||
this.isActive = true; | ||
} | ||
|
||
/** | ||
* Gets the account number. | ||
* | ||
* @return The account number. | ||
*/ | ||
public String getAccountNumber() { | ||
return accountNumber; | ||
} | ||
|
||
/** | ||
* Gets the owners of the account. | ||
* | ||
* @return The owners of the account. | ||
*/ | ||
public Set<Customer> getOwners() { | ||
return owners; | ||
} | ||
|
||
/** | ||
* Deposits funds into the account. | ||
* | ||
* @param amount The amount to deposit. | ||
*/ | ||
public void deposit(double amount) throws IllegalStateException { | ||
if (isClosed()) { | ||
throw new IllegalStateException("Cannot deposit to a closed account"); | ||
} | ||
if (amount <= 0) { | ||
throw new IllegalArgumentException("Deposit amount must be positive"); | ||
} | ||
balance += amount; | ||
} | ||
|
||
/** | ||
* Withdraws funds from the account. | ||
* | ||
* @param amount | ||
* @throws InsufficientFundsException | ||
*/ | ||
public void withdraw(double amount) throws InsufficientFundsException { | ||
if (isClosed()) { | ||
throw new IllegalStateException("Cannot withdraw from a closed account"); | ||
} | ||
if (amount <= 0) { | ||
throw new IllegalStateException("Withdrawal amount must be positive"); | ||
} | ||
if (balance < amount) { | ||
throw new InsufficientFundsException("Account does not have enough funds for withdrawal"); | ||
} | ||
balance -= amount; | ||
} | ||
|
||
/** | ||
* Gets the balance of the account. | ||
* | ||
* @return The balance of the account. | ||
*/ | ||
public double getBalance() { | ||
return balance; | ||
} | ||
|
||
/** Closes the account. */ | ||
public void closeAccount() throws IllegalStateException { | ||
if (balance > 0) { | ||
throw new IllegalStateException("Cannot close account with a positive balance"); | ||
} | ||
isActive = false; | ||
} | ||
|
||
/** | ||
* Checks if the account is closed. | ||
* | ||
* @return True if the account is closed, otherwise false. | ||
*/ | ||
public boolean isClosed() { | ||
return !isActive; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return accountNumber.hashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof BankAccount other) { | ||
return accountNumber.equals(other.accountNumber); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() | ||
+ "{accountNumber='" | ||
+ accountNumber | ||
+ '\'' | ||
+ ", balance=" | ||
+ balance | ||
+ ", isActive=" | ||
+ isActive | ||
+ '}'; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 3 additions & 116 deletions
119
lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/CheckingAccount.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,18 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; | ||
import java.util.Set; | ||
|
||
/** Represents a checking account. */ | ||
public class CheckingAccount { | ||
|
||
private final Set<Customer> owners; | ||
private final String accountNumber; | ||
private double balance; | ||
private boolean isActive; | ||
public class CheckingAccount extends BankAccount { | ||
|
||
/** | ||
* Creates a new checking account. | ||
* | ||
* @param accountNumber The account number. | ||
* @param owners The owners of the account. | ||
* @param initialBalance The initial balance of the account. | ||
* @return | ||
*/ | ||
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) { | ||
this.accountNumber = accountNumber; | ||
this.owners = owners; | ||
this.balance = initialBalance; | ||
isActive = true; | ||
} | ||
|
||
/** | ||
* Gets the account number. | ||
* | ||
* @return The account number. | ||
*/ | ||
public String getAccountNumber() { | ||
return accountNumber; | ||
} | ||
|
||
/** | ||
* Gets the owners of the account. | ||
* | ||
* @return The owners of the account. | ||
*/ | ||
public Set<Customer> getOwners() { | ||
return owners; | ||
} | ||
|
||
/** | ||
* Deposits funds into the account. | ||
* | ||
* @param amount The amount to deposit. | ||
*/ | ||
public void deposit(double amount) throws IllegalStateException { | ||
if (isClosed()) { | ||
throw new IllegalStateException("Cannot deposit to a closed account"); | ||
} | ||
if (amount <= 0) { | ||
throw new IllegalArgumentException("Deposit amount must be positive"); | ||
} | ||
balance += amount; | ||
} | ||
|
||
/** | ||
* Withdraws funds from the account. | ||
* | ||
* @param amount | ||
* @throws InsufficientFundsException | ||
*/ | ||
public void withdraw(double amount) throws InsufficientFundsException { | ||
if (isClosed()) { | ||
throw new IllegalStateException("Cannot withdraw from a closed account"); | ||
} | ||
if (amount <= 0) { | ||
throw new IllegalStateException("Withdrawal amount must be positive"); | ||
} | ||
if (balance < amount) { | ||
throw new InsufficientFundsException("Account does not have enough funds for withdrawal"); | ||
} | ||
balance -= amount; | ||
} | ||
|
||
/** | ||
* Gets the balance of the account. | ||
* | ||
* @return The balance of the account. | ||
*/ | ||
public double getBalance() { | ||
return balance; | ||
} | ||
|
||
/** Closes the account. */ | ||
public void closeAccount() throws IllegalStateException { | ||
if (balance > 0) { | ||
throw new IllegalStateException("Cannot close account with a positive balance"); | ||
} | ||
isActive = false; | ||
} | ||
|
||
/** | ||
* Checks if the account is closed. | ||
* | ||
* @return True if the account is closed, otherwise false. | ||
*/ | ||
public boolean isClosed() { | ||
return !isActive; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return accountNumber.hashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof CheckingAccount other) { | ||
return accountNumber.equals(other.accountNumber); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CheckingAccount{" | ||
+ "accountNumber='" | ||
+ accountNumber | ||
+ '\'' | ||
+ ", balance=" | ||
+ balance | ||
+ ", isActive=" | ||
+ isActive | ||
+ '}'; | ||
super(accountNumber, owners, initialBalance); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line looks off, this member variable shouldn't be static and should not be set to null