In: Computer Science
Java Programming
For this assignment, you should modify only the User.java file. Make sure that the InsufficientFundsException.java file is in the same folder as your User.java File. To test this program you will need to create your own "main" function which can import the User, create an instance and call its methods. One of the methods will throw an exception in a particular circumstance. This is a good reference for how throwing exceptions works in java. I would encourage you to do more research on exceptions in java for a deeper understanding.
For this assignment, to throw the Exceptions you will use "throw new InsufficientFundsException();"
Follow the TODOs and the documentation in the User.java file to complete this assignment. When ready to test, submit the User.java file to GradeScope. (As always eclipse users ensure that you have no package label at the top of your file)
I would suggest having at least both constructors, and getUserName() completed before submitting to GradeScope for the first time.
********
public class User {
// Class Attributes
private String username;
private double balance;
/***
* Default Constructor
* Should set all values to their defaults
* Strings should be set to "unknown"
* balance should start at $0
*/
public User() {
// TODO 1 IMPLEMENT THIS
METHOD
}
/***
* Constructor
* the class attributes should be set to the values
passed in by the method parameters
*
* The remaining attributes should be set to their
defaults:
* balance should start at $0
*
* @param: username
*/
public User(String username) {
// TODO 2 IMPLEMENT THIS
METHOD
}
/***
*
* @return the username
*/
public String getUserName() {
// TODO 3 IMPLEMENT THIS
METHOD
return "";
}
/***
*
* @return current balance amount as double
*/
public double getBalance() {
// TODO 4 IMPLEMENT THIS
METHOD
return 0.0;
}
/***
*
* @return current balance amount as formatted string
with $ and 2 decimal point
* precision
*/
public String getFormattedBalance() {
// TODO 5 IMPLEMENT THIS
METHOD
return "";
}
/***
* This method should add a deposit amount to the
balance
*
* @param deposit
*/
public void deposit(double deposit) {
// TODO 6 IMPLEMENT THIS
METHOD
}
/***
* This method should detract a withdrawal from the
balance
*
* @param withdrawal
*
* @throws InsufficientFundsException if the withdrawal
will put the current
* balance below $0
*/
public void withdrawal(double withdrawal) throws
InsufficientFundsException {
// TODO 7 IMPLEMENT THIS
METHOD
}
/***
*
* Given a rate and a number of months, calculate the
projected balance.
*
* Example Balance = $100
* Months = 4
* Rate = 0.01 (1%)
*
* After 1 Month Balance = $101 (100 + (100 *
0.01))
* After 2 Months Balance = $102.01 (101 + (101 *
0.01))
* After 3 Months Balance = $103.0301 (102.01 + (102.01
* 0.01))
* After 4 Months Balance = $104.060401 (103.0301 +
(103.0301 * 0.01))
*
* return 104.060401
*
* @param rate (Will always be positive)
* @param months (Will always be greater than 1)
* @return estimated balance after "months" months of
cumulative interest at
* rate "rate"
*/
public double projectedBalance(double rate, int
months) {
// TODO 8 IMPLEMENT THIS
METHOD
return 0.0;
}
}
---------------------------------------------------------------------------------------------------------------------
public class InsufficientFundsException extends RuntimeException
{
/**
*
*/
private static final long serialVersionUID =
7873326070461717954L;
public InsufficientFundsException() {
// TODO Auto-generated constructor
stub
super("Insufficient Funds in
Account");
}
}
Explanation:I have completed all the TODO methods as mentioned in the question,main() function is there and User class is tested by calling all the methods of the user class and also InsufficientFundsException is also thrown,you can see everything in the ouput attached with the answer.I have used DecimalFormatter to format the output upto two decimal places,if you want it can be modified to String.valueOf() also.I have used eclipse to write the code,so while uploading to the GradScore please make sure that no package is mentioned.I have modified my answer please comment if you need any modification.
//code starts
//InsufficientFundsException class
public class InsufficientFundsException extends RuntimeException
{
/**
*
*/
private static final long serialVersionUID =
7873326070461717954L;
public InsufficientFundsException() {
// TODO Auto-generated constructor
stub
super("Insufficient Funds in
Account");
}
}
//User class
import java.text.DecimalFormat;
public class User {
// Class Attributes
private String username;
private double balance;
/***
* Default Constructor Should set all values to their
defaults Strings should be
* set to "unknown" balance should start at $0
*/
public User() {
username = "unknown";
balance = 0;
}
/***
* Constructor the class attributes should be set to
the values passed in by the
* method parameters
*
* The remaining attributes should be set to their
defaults: balance should
* start at $0
*
* @param: username
*/
public User(String username) {
this.username = username;
balance = 0;
}
/***
*
* @return the username
*/
public String getUserName() {
return username;
}
/***
*
* @return current balance amount as double
*/
public double getBalance() {
return balance;
}
/***
*
* @return current balance amount as formatted string
with $ and 2 decimal point
* precision
*/
public String getFormattedBalance() {
DecimalFormat decimalFormat = new
DecimalFormat("##.00");
return
decimalFormat.format(balance);
}
/***
* This method should add a deposit amount to the
balance
*
* @param deposit
*/
public void deposit(double deposit) {
balance += deposit;
}
/***
* This method should detract a withdrawal from the
balance
*
* @param withdrawal
*
* @throws InsufficientFundsException if the withdrawal
will put the current
* balance below $0
*/
public void withdrawal(double withdrawal) throws
InsufficientFundsException {
if ((balance - withdrawal) <
0)
throw new
InsufficientFundsException();
else
balance -=
withdrawal;
}
/***
*
* Given a rate and a number of months, calculate the
projected balance.
*
* Example Balance = $100 Months = 4 Rate = 0.01
(1%)
*
* After 1 Month Balance = $101 (100 + (100 * 0.01))
After 2 Months Balance =
* $102.01 (101 + (101 * 0.01)) After 3 Months Balance
= $103.0301 (102.01 +
* (102.01 * 0.01)) After 4 Months Balance =
$104.060401 (103.0301 + (103.0301 *
* 0.01))
*
* return 104.060401
*
* @param rate (Will always be positive)
* @param months (Will always be greater than 1)
* @return estimated balance after "months" months of
cumulative interest at
* rate "rate"
*/
public double projectedBalance(double rate, int
months) {
for (int i = 1; i <= months;
i++) {
balance =
balance + ((balance * rate));
}
return balance;
}
public static void main(String[] args) {
User user = new User("Chris
Martin");
// display the username
System.out.println("Name :" +
user.getUserName());
// display the initial
balance
System.out.println("Balance :" +
user.getFormattedBalance());
// deposit money into account
user.deposit(5000);
// display the balance
System.out.println("Balance :" +
user.getFormattedBalance());
// withdraw some amount
user.withdrawal(4900);
// display the balance
System.out.println("Balance :" +
user.getFormattedBalance());
// display the projected
balance
System.out.println("The projected
balance :" + user.projectedBalance(0.01, 4));
// withdraw amount greater than
balance
user.withdrawal(6000);
}
}
Output: