Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PROJECT As a programmer intern at the First National Bank of Parkville, you\'re

ID: 3599949 • Letter: P

Question

PROJECT As a programmer intern at the First National Bank of Parkville, you're asked to write a program to help the bank tellers dispense money to the customers. Your program should promp the user to enter a money value or q to quit. For each entered money value, your program should tell the teller how much to dispense in paper money and how much to dispense in coins. You may assume that the user enters each value using the form "Sdollars.cents", with these rules: The S and the. are optional. dollars- a sequence of 0 or more digits with optional comma separators. cents-a sequence of 0 or more digits. There must be at least one digit in either dollars or cents No input validation is necessary. EXTRA CREDIT (2 pts): Add input validation to your program. See the extra credit sample session for details. Hint: As explained in the book, use the Character class'sisDigit method. Sample session: MONEY READER For money inputs, use the form "$dollars.cents", with these rules: There must be at least one digit. The $, ., and cents axe optional. dollars a sequence of 1 or more digits with optional comma separators. cents a sequence of 2 digits. nter a money value or enter q to quit: $1,000,123.40 Money dispenser: 1,000, 123 in paper money, 40 cents in coins Enter a money value or enter q to quit: 12345 Money dispenser: 12,345 in paper money, 0 cents in coins Enter a money value or enter q to quit: $0.04 Money dispenser: 0 in paper money, 4 cents in coins Enter a money value or enter q to quit: 0 Money dispenser: 0 in paper money, 0 cents in coins Enter a money value or enter q to quit: MONEY READER For money inputs, use the form "sdollars.cents", with these rules: There must be at least one digit. The $, ., and cents are optional. dollars a sequence of 1 or more digits with optional comma separators. cents a sequence of 2 digits. Enter a money value or enter q to quit: 1234.0 Invalid entry. Enter a money value or enter q to quit: 2.123

Explanation / Answer

import java.io.*;
import java.util.*;

public class DemoMoney{
   public static void main(String [] args){

      String inp;
      String str1,str2;
      Scanner sc = new Scanner(System.in);
      while(true){
          str1 = "";
          str2 = "";
          System.out.print("Enter a money value or q to quit :");
          inp = sc.next();
          if (inp.charAt(0) == 'q')
             break;
          int count = 0;
          int valid = 1;
          int found = 0;
          int found1 = 0;
          for (int i = 0; i<inp.length(); i++){
             if (inp.charAt(0) != '$'){
                 valid = 0;
                 break;
             }
             if (found == 1){
                if (!Character.isDigit(inp.charAt(i))){
                   valid = 0;
                   break;
                }
             }
             if (Character.isDigit(inp.charAt(i))){
                 count++;
             }
             if (Character.isDigit(inp.charAt(i)) || inp.charAt(i) == '.' || inp.charAt(i) == ',' || inp.charAt(i) == '$' ){
                if (inp.charAt(i) == '.')
                    found = 1;
                continue;
             }
             else {
                 valid = 0;
                 break;
             }
          }
          if (!Character.isDigit(inp.charAt(inp.length()-1))){
                valid = 0;
          }
          if (found == 0){
             valid = 0;
          }
          if (valid == 0){
             System.out.println("Invalid entry");
             continue;
          }
          if (count == 0){
              System.out.println("There should be atleast one digit in the input");
              continue;
          }
          found = 0;
          if (count > 0 && valid == 1){
             for (int i = 0; i<inp.length(); i++){
                if (found == 0){
                   if (inp.charAt(i) != '$' && inp.charAt(i) != '.'){
                      str1 = str1 + inp.charAt(i);
                   }
                  if (inp.charAt(i) == '.')
                     found = 1;
                }
                else {
                  
                      str2 = str2 + inp.charAt(i);
                  
                     
                }
             }
             if (!str2.equals(""))
                System.out.println("Money Dispenser : " + str1 + " in paper money, " + Integer.parseInt(str2) + " cents in coin");
             else
                System.out.println("Money Dispenser : " + str1 + " in paper money, " + Integer.parseInt(str2) + " cents in coin");
          }
      }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote