I have a question about the task: i need to implement a public static method getTotalAmount(), which takes as input a wallet with money in the form of an array of strings and the name of the currency. The method must return the amount of money in the specified currency
Method parameters:
An array of strings containing banknotes of different currencies with different denominations and the name of the currency (a string of 3 characters).
I need to implement this method using control instructions such as "break" and "continue".
How the program should work:
String[] money1 = {"eur 10", "usd 1", "usd 10", "rub 50", "usd 5"};
App.getTotalAmount(money1, "usd"); // 16
String[] money2 = {"eur 10", "usd 1", "eur 5", "rub 100", "eur 20", "eur 100", "rub 200"};
App.getTotalAmount(money2, "eur"); // 135
String[] money3 = {"eur 10", "rub 50", "eur 5", "rub 10", "rub 10", "eur 100", "rub 200"};
App.getTotalAmount(money3, "rub"); // 270
Notice to the task:
1.To convert a string to an integer, use the parseInt() method of the Integer class.
2.To extract a substring from a string, use the substring() method.
var text = "some text";
text.substring(1, 6); // "ome t"
text.substring(7); // "xt"
My code looks like this:
package com.arrays.problem6;
public class Currency {
public static void main(String[] args){
}
public static String getTotalAmount(String[] money,String currency){
int result=0;
for(String item: money){
if(currency==item.substring(0,3)){
// condition: if the specified rate is equal to the array element that is sliced from 0 - 3 index, then
// add to the counter what is left until the end of the element, converting the string into a number through parseInt()
}
result=Integer.parseInt(item.substring(4, item.length()));
}
return result;
}
}
I am completely confused in the implementation of the logic of the getTotalAmount() method. Please help me to implement it.
CodePudding user response:
First of all, do never compare String
using double equal signs ==
. Rather use a.equals(b)
.
Secondly you can use a.contains(b)
to check if a String contains another String.
You can then use the length of the currency 1 as start for the substring to get the amount of money:
package com.arrays.problem6;
public class Currency {
public static void main(String[] args){
}
public static int getTotalAmount(String[] money,String currency){
int result=0;
for(String item: money){
if(item.contains(currency)){ // condition
result = Integer.parseInt(item.substring(currency.length() 1)); // add to the counter
}
}
return result;
}
}
If you're forced to use a comparison to solve the exercise, you can also use a.equals(b)
instead of a.contains(b)
:
package com.arrays.problem6;
public class Currency {
public static void main(String[] args){
}
public static int getTotalAmount(String[] money,String currency){
int result=0;
for(String item: money){
if(currency.equals(item.substring(0, currency.length()))){ // condition
result = Integer.parseInt(item.substring(currency.length() 1)); // add to the counter
}
}
return result;
}
}
EDIT Note that I changed the signature of your static method from String
to int
since you're counting numbers and probably want to return the numbers, not the String.
If you have to use continue
and break
you could negate the condition for example, so you would check if the item does not contain the currency. Then you would use continue
to skip to the next item.
CodePudding user response:
Here is a more complete example.
public class Wallet {
enum Currency {
USD, EUR, RUB;
public String getName() {
return this.name().toLowerCase();
}
}
private static final String[] WALLET_1 = {"eur 10", "usd 1", "usd 10", "rub 50", "usd 5"};
private static final String[] WALLET_2 = {"eur 10", "usd 1", "eur 5", "rub 100", "eur 20", "eur 100", "rub 200"};
private static final String[] WALLET_3 = {"eur 10", "rub 50", "eur 5", "rub 10", "rub 10", "eur 100", "rub 200"};
public static void main(String... args) {
var amounts = new Integer[] {
getTotalAmount(WALLET_1, Currency.USD),
getTotalAmount(WALLET_2, Currency.EUR),
getTotalAmount(WALLET_3, Currency.RUB)
};
for(var i = 0; i < amounts.length; i ){
System.out.println("Wallet " (i 1) " amount is: " amounts[i]);
}
}
private static Integer getTotalAmount(String[] wallet, Currency currency) {
int amount = 0;
for (var note : wallet) {
var noteDetails = note.split(" ");
if (!currency.getName().equals(noteDetails[0])) {
continue;
}
amount = amount Integer.parseInt(noteDetails[1]);
}
return amount;
}
}
So let's breakdown the changes:
- Instead of using strings for the currency comparison I am using an
Enum
which models the currency -- in our case EUR, USD and RUB. - Instead of substringing which uses magic numbers to make assumptions about the start and end index I am using
split
which will return an array containing two strings one for the currency and one for the amount in string form. Note, that I do understand that this is a requirement of your task so maybe you need to change this. - Since your task calls for using
continue
andbreak
, this is how the iteration is handled here. So in case, this check fails!currency.getName().equals(noteDetails[0])
we simply usingcontinue
to jump to the next iteration. Also, note that string equality should always be checked using theequals
and NEVER EVER with==
as the last one will perform an object equality check and not a content equality. - In case the currency is the one we expect we add to the total amount which is returned once the loop finishes.