Question

In: Computer Science

When the user enters 4 at the menu prompt, your program will access all objects in...

When the user enters 4 at the menu prompt, your program will access all objects in the array to calculate and display only the largest number of coin denomination, and the total number of coins for the largest denomination. If there are more than one coin denominations having equal amount, you will need to list down all of them. After processing the output for menu option 4, the menu is re-displayed.

Menu option 4 should return as follows.

The largest number of coin denomination is:
$1
5 cent
The total number of $1 coin is: 2
The total number of 5 cent coin is: 2

i'm stuck with the returnDenomination() method

===

// import Scanner class to take user input

import java.util.Scanner;

// Client class

public class Client {

// for display colored terminal output

public static final String ANSI_RESET = "\u001B[0m";

public static final String ANSI_YELLOW = "\u001B[33m";

public static final String ANSI_GREEN = "\u001B[32m";

// scanner object

static Scanner userInput = new Scanner(System.in);

public static void main(String[] args) {

// declare and initialise variables

char yesNo = ' ';

String name;

boolean newPerson;

int menuInput;

int coinAmount;

int personNum = 0;

// declar array of object, Change type

Change person[] = new Change[10];

// for the purpose of testing the program by tutor, hard-coded data

personNum = 9;

person[0] = new Change("Abel", 10);

person[1] = new Change("Belle", 25);

person[2] = new Change("Chanel", 235);

person[3] = new Change("Desmond", 240);

person[4] = new Change("Elaine", 55);

person[5] = new Change("Farah", 60);

person[6] = new Change("Gabriel", 170);

person[7] = new Change("Hazel", 285);

person[8] = new Change("Iris", 190);

// display recommendation msg

System.out.print(ANSI_YELLOW);

System.out.print("\n[System Msg] ");

System.out.print(ANSI_RESET);

System.out.println("Recommendation: Please enter at least 9 records to test the program.");

// loop while userInput is not 'N' for new person

do {

newPerson = true;

System.out.println("\nCurrent record: " + personNum + "/9");

// userInput for name & coinAmount

System.out.print("\nPlease enter the name of the person: ");

name = userInput.nextLine();

System.out.print("Please enter coin value for the person (range 5 to 95, a multiple of 5): ");

coinAmount = userInput.nextInt();

userInput.nextLine();

// verify if useInput amount is valid (range 5 to 95, multiple of 5)

if(coinAmount < 5 || coinAmount > 95 || (coinAmount % 5) !=0) {

System.out.print(ANSI_YELLOW);

System.out.print("\n[System Msg] ");

System.out.print(ANSI_RESET);

System.out.println("Incorrect coin value. Must be in the range 5 to 95, multiple of 5");

continue;

}

// verify person in record

for (int i = 0; i < personNum; i++) {

// update total amount if name matches exisiting record

if (person[i].getName().equalsIgnoreCase(name)) {

person[i].setAmount(person[i].getAmount() + coinAmount);

newPerson = false;

break;

}

}

// add to record if the person is new

if (newPerson)

person[personNum++] = new Change(name, coinAmount);

// break when number of person in record greater/equals 9

if (personNum >= 9)

break;

System.out.print("\nDo you have more person to enter (Y/N): ");

yesNo = Character.toUpperCase(userInput.nextLine().charAt(0));

// verify only Y or N userInput

while (yesNo != 'Y' && yesNo != 'N') {

System.out.print(ANSI_YELLOW);

System.out.print("\n[System Msg] ");

System.out.print(ANSI_RESET);

System.out.print("Invalid! Do you have more person to enter (Y/N): ");

yesNo = Character.toUpperCase(userInput.nextLine().charAt(0));

}

} while(yesNo != 'N'); // end of newPerson do-while loop

// loop until userInput 5 to exit

do {

// display menu

System.out.println("\n1. Enter a name and display change to be given for each denomination");

System.out.println("2. Find the name with the smallest amount and display change to be given for each denomination");

System.out.println("3. Find the name with the largest amount and display change to be given for each denomination");

System.out.println("4. Calculate and display the largest number of coin denomination, and the total number of the coin");

System.out.println("5. Exit");

// wait for userInput

System.out.print("\nWhat would you like to do? Enter your choice: ");

menuInput = userInput.nextInt();

userInput.nextLine();

// display according to userInput

switch (menuInput) {

// display changes based on name of person entered

case 1:

returnIndividual(person, personNum);

break;

// display person with smallest amount and changes

case 2:

returnSmallest(person, personNum);

break;

// display person with largest amount and changes

case 3:

returnLargest(person, personNum);

break;

// display total of largest numer of coin denomination

case 4:

returnDenomination(person, personNum);

break;

// display farewell message and exit program

case 5:

System.out.print(ANSI_YELLOW);

System.out.print("\n[System Msg] ");

System.out.print(ANSI_RESET);

System.out.println("Have a good day ahead. Bye!\n");

System.exit(0);

break;

// default for no case match

default:

System.out.print(ANSI_YELLOW);

System.out.print("\n[System Msg] ");

System.out.print(ANSI_RESET);

System.out.println("Invalid! Please enter your choice again: \n");

}

} while (menuInput != 5); // end of menu do-while loop

} // end of main()

// method to find individual person by name and display denomination

public static void returnIndividual(Change person[], int personNum) {

// declare variable to store array of denomination

int denomination[] = new int[4];

// prompt userInput for name

System.out.print("Enter name: ");

String name = userInput.nextLine();

boolean nameNotFound = true;

// verify display name and coinAmount in record

for (int i = 0; i < personNum; i++) {

if (person[i].getName().equalsIgnoreCase(name)) {

// calculate and return count of each denomination

denomination = person[i].calDenomination();

System.out.println(ANSI_GREEN);

System.out.println("\n" + person[i].getName() + " has an account of: " + person[i].getAmount() + " cents\n");

// display count of each denomination

System.out.println("Change:");

if (denomination[0] != 0)

System.out.println("$1 : " + denomination[0]);

if (denomination[1] != 0)

System.out.println("50¢: " + denomination[1]);

if (denomination[2] != 0)

System.out.println("20¢: " + denomination[2]);

if (denomination[3] != 0)

System.out.println("10¢: " + denomination[3]);

if (denomination[4] != 0)

System.out.println("5¢ : " + denomination[4] + "\n");

System.out.println(ANSI_RESET);

// set flag to false since name is in record

nameNotFound = false;

break;

}

}

// display error msg if name not in record

if (nameNotFound) {

System.out.print(ANSI_YELLOW);

System.out.print("\n[System Msg] ");

System.out.print(ANSI_RESET);

System.out.println(name + " not found in record!\n");

}

} // end of returnIndividual()

public static void returnDenomination(Change person[], int personNum) {

// for menu 4

} // end of returnDenomination

} // end of Client class

Solutions

Expert Solution

//import Scanner class to take user input

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.Map.Entry;

//Client class

public class Denomination {

//for display colored terminal output

public static final String ANSI_RESET = "\u001B[0m";

public static final String ANSI_YELLOW = "\u001B[33m";

public static final String ANSI_GREEN = "\u001B[32m";

//scanner object

static Scanner userInput = new Scanner(System.in);

public static void main(String[] args) {

//declare and initialise variables

     char yesNo = ' ';
     
     String name;
     
     boolean newPerson;
     
     int menuInput;
     
     int coinAmount;
     
     int personNum = 0;
     
     // declar array of object, Change type
     
     Change person[] = new Change[10];
     
     // for the purpose of testing the program by tutor, hard-coded data
     
     personNum = 9;

     person[0] = new Change("Abel", 10);
     
     person[1] = new Change("Belle", 25);
     
     person[2] = new Change("Chanel", 235);
     
     person[3] = new Change("Desmond", 240);
     
     person[4] = new Change("Elaine", 55);
     
     person[5] = new Change("Farah", 60);
     
     person[6] = new Change("Gabriel", 170);
     
     person[7] = new Change("Hazel", 285);
     
     person[8] = new Change("Iris", 190);

//display recommendation msg
 
 System.out.print(ANSI_YELLOW);
 
 System.out.print("\n[System Msg] ");
 
 System.out.print(ANSI_RESET);
 
 System.out.println("Recommendation: Please enter at least 9 records to test the program.");

//loop while userInput is not 'N' for new person

 do {

     newPerson = true;
     
     System.out.println("\nCurrent record: " + personNum + "/9");
     
     // userInput for name & coinAmount
     
     System.out.print("\nPlease enter the name of the person: ");
     
     name = userInput.nextLine();
     
     System.out.print("Please enter coin value for the person (range 5 to 95, a multiple of 5): ");
     
     coinAmount = userInput.nextInt();
     
     userInput.nextLine();
     
     // verify if useInput amount is valid (range 5 to 95, multiple of 5)
     
     if(coinAmount < 5 || coinAmount > 95 || (coinAmount % 5) !=0) {
     
     System.out.print(ANSI_YELLOW);
     
     System.out.print("\n[System Msg] ");
     
     System.out.print(ANSI_RESET);
     
     System.out.println("Incorrect coin value. Must be in the range 5 to 95, multiple of 5");
     
     continue;
     
     }
     
     // verify person in record
     
     for (int i = 0; i < personNum; i++) {
     
     // update total amount if name matches exisiting record
     
     if (person[i].getName().equalsIgnoreCase(name)) {
     
     person[i].setAmount(person[i].getAmount() + coinAmount);
     
     newPerson = false;
     
     break;
     
     }

 }

//add to record if the person is new

 if (newPerson)
 
     person[personNum++] = new Change(name, coinAmount);
 
 // break when number of person in record greater/equals 9
 
 if (personNum >= 9)
 
     break;
 
 System.out.print("\nDo you have more person to enter (Y/N): ");
 
 yesNo = Character.toUpperCase(userInput.nextLine().charAt(0));
 
 // verify only Y or N userInput
 
 while (yesNo != 'Y' && yesNo != 'N') {
 
     System.out.print(ANSI_YELLOW);
     
     System.out.print("\n[System Msg] ");
     
     System.out.print(ANSI_RESET);
     
     System.out.print("Invalid! Do you have more person to enter (Y/N): ");
     
     yesNo = Character.toUpperCase(userInput.nextLine().charAt(0));
     
     }
 
 } while(yesNo != 'N'); // end of newPerson do-while loop
 
 // loop until userInput 5 to exit

do {

//display menu

 System.out.println("\n1. Enter a name and display change to be given for each denomination");
 
 System.out.println("2. Find the name with the smallest amount and display change to be given for each denomination");
 
 System.out.println("3. Find the name with the largest amount and display change to be given for each denomination");
 
 System.out.println("4. Calculate and display the largest number of coin denomination, and the total number of the coin");
 
 System.out.println("5. Exit");
 
 // wait for userInput
 
 System.out.print("\nWhat would you like to do? Enter your choice: ");
 
 menuInput = userInput.nextInt();
 
 userInput.nextLine();

//display according to userInput

switch (menuInput) {

//display changes based on name of person entered

case 1:

returnIndividual(person, personNum);

break;

//display person with smallest amount and changes

case 2:

returnSmallest(person, personNum);

break;

//display person with largest amount and changes

case 3:

returnLargest(person, personNum);

break;

//display total of largest numer of coin denomination

case 4:

returnDenomination(person, personNum);

break;

//display farewell message and exit program

case 5:

 System.out.print(ANSI_YELLOW);
 
 System.out.print("\n[System Msg] ");
 
 System.out.print(ANSI_RESET);
 
 System.out.println("Have a good day ahead. Bye!\n");
 
 System.exit(0);
 
 break;
 
 // default for no case match
 
 default:
 
 System.out.print(ANSI_YELLOW);
 
 System.out.print("\n[System Msg] ");
 
 System.out.print(ANSI_RESET);
 
 System.out.println("Invalid! Please enter your choice again: \n");

 }
 
 } while (menuInput != 5); // end of menu do-while loop

} // end of main()

//method to find individual person by name and display denomination

public static void returnIndividual(Change person[], int personNum) {

//declare variable to store array of denomination

 int denomination[] = new int[4];
 
 // prompt userInput for name
 
 System.out.print("Enter name: ");
 
 String name = userInput.nextLine();
 
 boolean nameNotFound = true;
 
 // verify display name and coinAmount in record
 
 for (int i = 0; i < personNum; i++) {
     
     if (person[i].getName().equalsIgnoreCase(name)) {
     
     // calculate and return count of each denomination
     
     denomination = person[i].calDenomination();
     
     System.out.println(ANSI_GREEN);
     
     System.out.println("\n" + person[i].getName() + " has an account of: " + person[i].getAmount() + " cents\n");
     
     // display count of each denomination
     
     System.out.println("Change:");
     
     if (denomination[0] != 0)
     
     System.out.println("$1 : " + denomination[0]);
     
     if (denomination[1] != 0)
     
     System.out.println("50¢: " + denomination[1]);
     
     if (denomination[2] != 0)
     
     System.out.println("20¢: " + denomination[2]);
     
     if (denomination[3] != 0)
     
     System.out.println("10¢: " + denomination[3]);
     
     if (denomination[4] != 0)
     
     System.out.println("5¢ : " + denomination[4] + "\n");
     
     System.out.println(ANSI_RESET);
     
     // set flag to false since name is in record
     
     nameNotFound = false;
     
     break;
     
     }
 
 }
 
 // display error msg if name not in record
 
 if (nameNotFound) {
 
 System.out.print(ANSI_YELLOW);
 
 System.out.print("\n[System Msg] ");
 
 System.out.print(ANSI_RESET);
 
 System.out.println(name + " not found in record!\n");
 
 }

} // end of returnIndividual()

public static void returnDenomination(Change person[], int personNum) {

//for menu 4
 int denomination[] = new int[5];
 System.out.print("Enter name: ");
 String name = userInput.nextLine();
 
   for (int i = 0; i < personNum; i++) {
     if (person[i].getName().equalsIgnoreCase(name)) {
     
     // calculate and return count of each denomination
     
     denomination = person[i].calDenomination();
         

                        Map<String, Integer> tm = new TreeMap<String, Integer>();
                        tm.put("$1",50);
                        tm.put("50¢",20);
                        tm.put("20¢",35);
                        tm.put("10¢",15);
                        tm.put("5¢",18);
        
                        List<Integer> al = new ArrayList<Integer>();
                for(Map.Entry<String, Integer> entry:tm.entrySet()) {
                        al.add(entry.getValue());
                }
        
                Collections.sort(al);
                int max=al.get(5-1);
                System.out.println(max);

                System.out.println("The largest number of coin denomination is:");
                
                for(Entry<String, Integer> entry:tm.entrySet()) {
                         if(entry.getValue()==max) {
                                 System.out.println(entry.getKey());
                         }
                   }
                
                for(Entry<String, Integer> entry:tm.entrySet()) {
                         if(entry.getValue()==max) {
                                 System.out.println("The total number of "+entry.getKey()+" coin is: "+max);
                         }
                   }   
         
     }
  
       
   }
 

} // end of returnDenomination

} // end of Client class

In this example Change class is missing so I assume the methods and its type from the given code snippet.

I hope you like the solution. If you do so then support us by pressing that thumbs up button.


Related Solutions

Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names     &
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close Do you...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close...
You must prompt the user to enter a menu selection. The menu will have the following...
You must prompt the user to enter a menu selection. The menu will have the following selections (NOTE: all menu selections by the user should not be case sensitive): 1. ‘L’ – Length a. Assume that a file containing a series of names is called names.txt and exists on the computer’s disk. Read that file and display the length of each name in the file to the screen. Each name’s middle character or characters should be displayed on a line...
Write a C program Your program will prompt the user to enter a value for the...
Write a C program Your program will prompt the user to enter a value for the amount of expenses on the credit card. Retrieve the user input using fgets()/sscanf() and save the input value in a variable. The value should be read as type double. The user may or may not enter cents as part of the input. In other words, expect the user to enter values such as 500, 500.00 and 500.10. The program will then prompt the user...
In Python, your program will read in a number (no need to prompt the user), and...
In Python, your program will read in a number (no need to prompt the user), and then reads in that number of lines from the terminal. Then the program should print an array of strings formatted in a nice regular box. So if the user inputs this: 5 Grim visaged war has smooth’d his wrinkled front And now, instead of mounting barded steeds To fright the souls of fearful adversaries He capers nimbly in a lady’s chamber To the lascivious...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199 Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT