CSU Banner

CS 150, Spring 2018

Programming Assignment - P5

While Loops Plus Error Handling

Due - March 27, 2018 at 6:00pm

Late - March 28, 2018 at 8:00am


Objectives of this Assignment

  1. To write a program that utilizes while loops combined with if statements, and
  2. to practice writing code that handles potential user error.

Instructions

Create a Java program called P5 with a class named P5, which after created should contain a file named P5.java in the src folder. Please complete the code required for Lab 9 first if you have not already done so. All of the Java code you write should be contained in the main method and most of the remaining code for this assignment will be inside the while loop inside main(). For this assignment, you must follow the directions below exactly:

    Part One
  1. Initialize four additional variables of type double to serve the following purposes: withdrawal amount, principal, interest rate, and future investment amount. Initialize the interest rate variable to 0.025 and the rest to 0.0.
  2. Initialize two additional variables of type int to serve the following purposes: number of times per year the investment is compounded and number of years the investment grows. Initialize the variable related to compounding in each year to 2. The other should be initialized to 0.
  3. Add three additional menu prompts after option 1. After the addition, your menu prompt should match the following:
    0: Exit ATM
    1: Make a Deposit
    2: Make a Withdrawal
    3: Display Balance
    4: Calculate Compound Interest
    Please select a menu option:
  4. Next before the if conditional for menu option 1, insert an inner while loop that handles the situation of a user entering a value below zero OR above 4.
  5. Begin this while loop by printing a blank line (for better readability).
  6. Then print the following two statements to the console:
    That is not a valid menu option.
    Please select a menu option:
  7. Before the end of the inner while loop, write a statement that assigns an integer value via console input (i.e. use your Scanner object) to your variable that holds the user's menu option. This will complete the inner while loop.

  8. Part Two
  9. After the if conditionals for menu options 0 and 1, start by printing a blank line inside the if conditional for menu option 2.
  10. Then print the following prompt to the console:
    Enter the amount to withdraw:
  11. Assign the value entered to your associated variable.
  12. Next write an inner while loop inside the if conditional for menu option 2.
  13. This while loop will check to make sure the user is not trying to withdrawal more than the account balance. You may do this verification with the following in the while parentheses:
    accountBalance - withdrawalAmount < 0.0
  14. Inside the while loop print the following two prompts and afterward write a statement that assigns the new value to your withdrawal amount variable.
    Withdrawal amount cannot be more than account balance.
    Enter the amount to withdraw:
  15. After this inner while loop, update the value in your account balance variable.
  16. Lastly, print the new account balance to the console to two decimal places after the prompt below and including a new line, and also follow with a blank line.
    New account balance is: $
  17. For menu option 3, simply print the account balance to the console to two decimal places after the prompt below.
    Current account balance: $
  18. Additionally, the if conditional for menu option 3 should contain, in some form, three new lines. One option is to print a blank line, then a printf statement with a new line character, and then another blank line.

  19. Part Three
  20. The if conditional for menu option 4 will again begin with a blank line.
  21. Following this, print the following prompt:
    Enter principal investment amount:
  22. Assign the value entered to your associated variable.
  23. Next, write an inner while loop to handle the situation of a user entering a negative value. The following two prompts should be printed in this situation:
    Principal amount cannot be negative.
    Enter principal investment amount:
  24. After the two prompts, write another statement to assign a value entered to the principal variable.
  25. Outside of the previous while loop, print the following:
    Enter number of years to grow investment:
  26. Assign the value entered to your associated variable.
  27. Write another inner while loop to handle the situation of a user entering a negative value. The following two prompts should be printed in this situation:
    Growth period cannot be negative.
    Enter number of years to grow investment:
  28. After the two prompts, write another statement to assign a value entered to the variable associated with years of investment growth.
  29. Next, print a blank line to the console.
  30. The last part of this assignment involves calculating the future investment amount and occurs outside of the previous inner while loop. You will do so with the following equation:
    Compound Interest Formula
    You will need to base your calculation off of the names you have given your own variables. Utilizing Math.pow() is recommended for this calculation.
  31. Lastly, inside the if conditional for menu option 4, print a concatenated statement that matches the following:
    In 10 years your principal will grow to $1123.65
    The values above in blue are example values, but your future value for the given principal should be printed with only two values after the decimal point.
  32. After your printf statement, print another blank line to the console.
  33. And finally, as good practice, close your Scanner object at the end of your code in main.

Sample Output (One Run With Multiple Options Selected)

Test your program repeatedly and with different options. Your program should match the spacing in the example given below. User input is shown in blue.

Welcome to the CS150 Simple ATM!
0: Exit ATM
1: Make a Deposit
2: Make a Withdrawal
3: Display Balance
4: Calculate Compound Interest
Please select a menu option: 1 

Enter the amount to deposit: 44.577 
New account balance is: $44.58

0: Exit ATM
1: Make a Deposit
2: Make a Withdrawal
3: Display Balance
4: Calculate Compound Interest
Please select a menu option: 2 

Enter the amount to withdraw: 65 
Withdrawal amount cannot be more than account balance.
Enter the amount to withdraw: 20.11 
New account balance is: $24.47

0: Exit ATM
1: Make a Deposit
2: Make a Withdrawal
3: Display Balance
4: Calculate Compound Interest
Please select a menu option: 6 

That is not a valid menu option.
Please select a menu option: 3 

Current account balance: $24.47

0: Exit ATM
1: Make a Deposit
2: Make a Withdrawal
3: Display Balance
4: Calculate Compound Interest
Please select a menu option: 4 

Enter principal investment amount: 1100 
Enter number of years to grow investment: 5 

In 5 years your principal will grow to $1245.50

0: Exit ATM
1: Make a Deposit
2: Make a Withdrawal
3: Display Balance
4: Calculate Compound Interest
Please select a menu option: 4 

Enter principal investment amount: -99 
Principal amount cannot be negative.
Enter principal investment amount: 99 
Enter number of years to grow investment: -5 
Growth period cannot be negative. 
Enter number of years to grow investment: 5 

In 5 years your principal will grow to $112.09

0: Exit ATM
1: Make a Deposit
2: Make a Withdrawal
3: Display Balance
4: Calculate Compound Interest
Please select a menu option: 0 

Thank you for using the CS150 Simple ATM. Goodbye.

Specifications

Grading Criteria


Submit P5.java to Checkin.


© 2018 CS150 Colorado State University. All Rights Reserved.
CS Banner
CS Building