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
- To write a program that utilizes while loops combined with if statements, and
- 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
- 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.
- 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.
- 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:
- 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.
- Begin this while loop by printing a blank line (for better readability).
- Then print the following two statements to the console:
That is not a valid menu option.
Please select a menu option:
- 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.
Part Two
- After the if conditionals for menu options 0 and 1, start by printing a blank line inside the if conditional for menu option 2.
- Then print the following prompt to the console:
Enter the amount to withdraw:
- Assign the value entered to your associated variable.
- Next write an inner while loop inside the if conditional for menu option 2.
- 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
- 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:
- After this inner while loop, update the value in your account balance variable.
- 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: $
- For menu option 3, simply print the account balance to the console to two decimal places after the prompt below.
Current account balance: $
- 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.
Part Three
- The if conditional for menu option 4 will again begin with a blank line.
- Following this, print the following prompt:
Enter principal investment amount:
- Assign the value entered to your associated variable.
- 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:
- After the two prompts, write another statement to assign a value entered to the principal variable.
- Outside of the previous while loop, print the following:
Enter number of years to grow investment:
- Assign the value entered to your associated variable.
- 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:
- After the two prompts, write another statement to assign a value entered to the variable associated with years of investment growth.
- Next, print a blank line to the console.
- 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:
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.
- 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.
- After your printf statement, print another blank line to the console.
- 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
- Work on your own, as always.
- The name of the source code file must be exactly
P5.java
- Name the file exactly - upper and lower case matters!
- Comments at the top as shown above.
- Assignments should be implemented using Eclipse.
- Assignments should be implemented using Java, version 1.8.
- Make sure your code runs on machines in the CSB 120 lab.
- Turn in through the Checkin tab.
- Read the syllabus for the late policy.
- We will be checking programs for plagiarism, so please don't copy from anyone else.
Grading Criteria
- 100 points for perfect submission.
- 0 points for no submission, will not compile, submitted class file, etc.
- Preliminary Tests
Preliminary testing tests lines 1-5 .
- testCompile: checks that program compiles. (0 points)
- lineTests: verify correct output based on String method used.
- test1:
- test2:
- test3:
- test4:
- test5:
- Final Tests
Final testing tests lines 6-8.
- test8:
- test9:
- test10:
- Final grading includes the preliminary tests.
Submit P5.java to Checkin.
© 2018 CS150 Colorado State University. All Rights Reserved.