Recap: Pay Phone
Put do-while, the ternary operator, switch expressions, chars and the Math class to work together in one program
Mission: Pay Phone
Tommy doesn't trust cell phones, so he makes his calls from a pay phone. Write the phone's brain.
Store the number he dials in a String named dialed and the length of the call in an int named minutes. The number can have dashes or spaces in it, like 305-555-0142.
- Keep only the digits of
dialed. A real number has exactly 10 digits. If it doesn't, printInvalid numberand nothing else - The first 3 digits are the area code. Write a method that takes the area code and gives back where the call goes, with a switch expression:
305or786:Vice City212:Liberty City415:San Fierro- anything else:
Long distance
- A minute costs 1 coin in Vice City, 3 coins in Liberty City or San Fierro, and 5 coins for long distance. Even a call of 0 minutes is charged as 1 minute
- Print
Calling <place>, thenCost: N coins, orCost: 1 coinwhen it is exactly one. Pick the word with the ternary operator
Example for dialed = "305-555-0142" and minutes = 3
Calling Vice City
Cost: 3 coins
Example for dialed = "212 555 0199" and minutes = 0
Calling Liberty City
Cost: 3 coins
Example for dialed = "786-555-0100" and minutes = 1
Calling Vice City
Cost: 1 coin
Example for dialed = "555-0142" and minutes = 2
Invalid number