In: Computer Science
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
//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.