In: Computer Science
Using the provided Java program below, complete the code to do the following. You may need to create other data items than what is listed for this assignment.
a. Use a do-while loop
b. Prompt for a currency to find.
c. Inside this loop, call the lookUpCurrency() method can pass the inputted currency code value.
d. The lookUpCurrency() method needs to loop through the list of currencies and compare the passed currency code with the currency code in the array. This method should return a boolean result.
Look at the showCurrencies() method for a example of how to loop for each element.
e. In the findMyCurrency() method
If found - Display the currency code and state it was found - i.e. "FJD found !".
If not found - Display the currency code and not found - i.e " ZZZ not found!".
f. Prompt with "Retry Y/N ?" to try again. If I select N, then exit the loop. Allow any other char to restart the loop and prompt for a currency to find.
Because you are comparing strings, remember to use the .equals method - not ==.
Below is a partial example of the expected output:
Enter a Currency Code to Find: USD
USD found!
Retry Y/N ? Y
Enter a Currency Code to Find: ZZZ
ZZZ not found!
Retry Y/N ? N
Code you are working with:
/* * Student Name - * Student ID - * Semester - * Campus - EL Centro * Class - COSC 1436 * */ import java.util.Currency; import java.util.Iterator; import java.util.Set; public class CheckCurrencyCode { // Sets the MAX_CURRENCIES value from the Currency Object static int MAX_CURRENCIES = Currency.getAvailableCurrencies().size(); static String[] CURRENCY_CODES = new String[MAX_CURRENCIES]; /** * This method will load the currency_Codes array with 3 char codes. * It does not need to change */ private static void loadCurrencyCodes() { Set<Currency> currencies = Currency.getAvailableCurrencies(); Iterator<Currency> i = currencies.iterator(); int idx = 0; while(i.hasNext()) { CURRENCY_CODES[idx++] = i.next().getCurrencyCode(); } } /** * Raw display of the Currency_codes; * Could also use System.out.println(Arrays.toString(CURRENCY_CODES)); * It does not need to change */ private static void showCurrencies() { for (int idx = 0; idx < MAX_CURRENCIES;++idx) { System.out.print(CURRENCY_CODES[idx]+","); } System.out.println(); //System.out.println(Arrays.toString(CURRENCY_CODES)); } /** * Look up the given currency here and set the return value * to true if found or false if not found * * Hint: Use a FOR loop to traverse the loop * * @param myCurrencyToFind * @return */ private static boolean lookUpCurrency(String myCurrencyToFind) { //TODO remove line below. It was added only for compile purposes return true; } /** * Prompt for a currency to find * Call the lookUpCurrency with the inputted currency code * Display the found result * Prompt to try again and exit this method only if the choice is to not continue */ private static void findMyCurrency() { } public static void main(String[] args) { loadCurrencyCodes(); showCurrencies(); findMyCurrency(); //TODO Display an indication that the program is completed } }
Please up vote ,comment if any query . Thanks for question . Be safe .
Note : check attached image for output ,code tested in java netbeans ide.
Program : ************************CheckCurrencyCode.java**********************************
/*
* Student Name -
* Student ID -
* Semester -
* Campus - EL Centro
* Class - COSC 1436
*
*/
import java.util.Currency;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
public class CheckCurrencyCode {
// Sets the
MAX_CURRENCIES value from the Currency Object
static int
MAX_CURRENCIES = Currency.getAvailableCurrencies().size();
static String[]
CURRENCY_CODES = new String[MAX_CURRENCIES];
/**
* This method will
load the currency_Codes array with 3 char codes.
* It does not need
to change
*/
private static void
loadCurrencyCodes() {
Set<Currency> currencies =
Currency.getAvailableCurrencies();
Iterator<Currency> i = currencies.iterator();
int idx = 0;
while(i.hasNext()) {
CURRENCY_CODES[idx++] = i.next().getCurrencyCode();
}
}
/**
* Raw display of
the Currency_codes;
* Could also use
System.out.println(Arrays.toString(CURRENCY_CODES));
* It does not need
to change
*/
private static void
showCurrencies() {
for (int idx = 0; idx < MAX_CURRENCIES;++idx) {
System.out.print(CURRENCY_CODES[idx]+",");
}
System.out.println();
//System.out.println(Arrays.toString(CURRENCY_CODES));
}
/**
* Look up the
given currency here and set the return value
* to true if found
or false if not found
*
* Hint: Use a FOR
loop to traverse the loop
*
* @param
myCurrencyToFind
* @return
*/
private static boolean
lookUpCurrency(String myCurrencyToFind) {
for(int i=0;i<MAX_CURRENCIES;i++) //runa a loop from 0 to
MAX_CURRENCIES
{
//check for string match it will ignore upper case or lower
case
//shallow comparision
if(CURRENCY_CODES[i].equalsIgnoreCase(myCurrencyToFind))
{
return true; //return true
}
}
return false; //else return false not found
}
/**
* Prompt for a
currency to find
* Call the
lookUpCurrency with the inputted currency code
* Display the
found result
* Prompt to try
again and exit this method only if the choice is to not
continue
*/
private static void
findMyCurrency() {
Scanner sc=new Scanner(System.in); //Create a scanner object
while(true) //infinite loop
{
//prompt for currency code
System.out.print("Enter a Currency Code to Find: ");
String currencyToFind=sc.nextLine(); //read string
if(lookUpCurrency(currencyToFind)==true) //if function return
true
{
System.out.println(currencyToFind+" found!"); //print code
found
}
else //else not found
{
System.out.println(currencyToFind+" not found!");
}//get choice y or n
System.out.println("Retry Y/N ? ");
char choice=sc.nextLine().charAt(0); //match with first char
if(choice=='N') //if N break from loop or exit from function
return;
}
}
public static void
main(String[] args) {
loadCurrencyCodes(); //get currency in array
showCurrencies(); //print currency
findMyCurrency(); //find currency
System.out.println("Program completed. ");
//TODO Display an indication that the program is completed
}
}
Output :
Please up vote ,comment if any query .