用Java建模钱包逻辑



我有一个关于任务的问题:我需要实现一个公共静态方法getTotalAmount(),它以字符串数组和货币名称的形式接收一个钱包作为输入。该方法必须以指定的货币

返回金额方法参数:

字符串数组内含不同币种、不同面额的钞票及币名(3个字符的字符串)。

我需要使用控制指令来实现这个方法,例如"break">"continue">.

程序应该如何工作:

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

任务通知:

1。要将字符串转换为整数,使用parseInt()Integer方法类。

2。要从字符串中提取子字符串,请使用substring()方法。

var text = "some text";
text.substring(1, 6); // "ome t"
text.substring(7); // "xt"

我的代码是这样的:

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;
}
}

我完全混淆了getTotalAmount()方法的逻辑实现。请帮助我实现它。

首先,不要用双等号==来比较String。不如用a.equals(b)

其次,您可以使用a.contains(b)来检查一个字符串是否包含另一个字符串。

您可以使用货币的长度+ 1作为子字符串的开始,以获得货币的数量:

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;
}
}

如果您被迫使用比较来解决练习,您也可以使用a.equals(b)代替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;
}
}

编辑请注意,我将静态方法的签名从String更改为int,因为您正在计数数字,并且可能希望返回数字,而不是字符串。

如果您必须使用continuebreak,您可以否定条件,例如,因此您将检查项是否不控制货币。然后使用continue跳转到下一个项目。

下面是一个更完整的例子。


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;
}
}

那么让我们来分解这些变化:

  1. 我没有使用字符串进行货币比较,而是使用Enum来模拟货币——在我们的例子中是欧元、美元和卢布。
  2. 不是使用幻数来假设开始和结束索引的子字符串,我使用split,它将返回一个包含两个字符串的数组,一个用于货币,一个用于字符串形式的金额。请注意,我确实理解这是您任务的要求,所以也许您需要更改它。
  3. 由于您的任务调用使用continuebreak,这就是这里处理迭代的方式。因此,如果!currency.getName().equals(noteDetails[0])检查失败,我们只需使用continue跳转到下一个迭代。另外,请注意,应该始终使用equalsNEVER EVER检查字符串是否相等。==作为最后一个将执行对象相等性检查,而不是内容相等性。
  4. 如果货币是我们期望的货币,我们将添加到循环结束后返回的总金额中。

最新更新