You are on page 1of 2

Lab 1.

3 – Electronic Piggy Bank


Learning Goals

1) Develop your ability to use if, else if, and else statements to control program flow.
2) Develop your ability to use while statements to control program flow.
3) Develop your ability to use equals() for strings.

Background

Your little brother, Timmy, just got a piggy bank. He starts to use it, but becomes tired of
always adding up how much he is putting in the piggy bank (so that he know how much he
has).. He asks you for help.

You tell Timmy – “I’m still learning, but I am sure that I can write you a program to count how
much you deposit each time. I will just need you to keep track of the total value after each
time you use the program.”

Your Task

Write an electronic piggy bank program that begins by asking the user:
How much money is the piggy bank so far?

Then, present the user with the following menu:


(p) – Deposit a penny
(n) – Deposit a nickel
(d) – Deposit a dime
(q) – Deposit a quarter
(e) – Exit program and give balance

Each time the user presses “p”, “n”, “d”, or “q”, add the value of the coin to the total balance,
display the new total balance, and prompt the user to enter their next selection without
redrawing the menu.

When the user presses “e”, display the final balance, thank the user, and exit.

If the user presses an invalid key, print “Invalid option.”

Grading

This assignment is worth 100 points.


20 pts – Program correctly loops until “e” is pressed.
20 pts – Program correctly tracks and prints the running total value of coins, including
the final balance after e is pressed.
20 pts – Program successfully uses an if/else if/else block to process the user’s input.
20 pts – Text displayed to user is well formatted, including all required elements.
20 pts – Money printouts always have exactly 2 decimal placements.
Sample Run

Welcome to the Electronic Piggy Bank!


How much money is currently in the bank?
4.28

MENU:
(p) - Deposit a penny.
(n) - Deposit a nickel.
(d) - Deposit a dime.
(q) - Deposit a quarter.
(e) - Exit program.
Please press a letter to select an option:
p

New balance: $4.29


Please press a letter to select an option:
d

New balance: $4.39


Please press a letter to select an option:
d

New balance: $4.49


Please press a letter to select an option:
n

New balance: $4.54


Please press a letter to select an option:
g

Invalid option.
Please press a letter to select an option:
e

Final balance: $4.54


Goodbye

You might also like