I was working on this simple currency converter just to reinforce some concepts I learn. Everything is working as planned except after the converter runs the first time, the second iteration of the while loop causes the first answer to use both the if and the else. The output is as follows:
Convert US dollar into which currency? [Yen, Yuan, Peso, Pound, Euro] Pound
How many dollars would you like converted? 5
You have converted $5.0 into 3.8 Pound
Convert US dollar into which currency? [Yen, Yuan, Peso, Pound, Euro] Please select a currency Convert US dollar into which currency? [Yen, Yuan, Peso, Pound, Euro]
Currency class:
package currency;
import java.util.HashMap;
import java.util.Map;
public class Currency {
private Map<String, Double> ct;
private double newCurrency;
private String answer;
private double amount;
public Currency() {
this.ct = new HashMap<String, Double>();
}
// Getters and Setters
public void setAnswer(String answer) {
this.answer = answer;
}
public String getAnswer() {
return this.answer;
}
public void setAmount(double amount) {
this.amount = amount;
}
public double getAmount() {
return this.amount;
}
// Current available currencies
public void loadMap() {
ct.put("Pound", 0.76);
ct.put("Euro", 0.85);
ct.put("Yen", 112.23);
ct.put("Yuan", 6.61);
ct.put("Peso", 18.87);
System.out.println(ct.keySet());
}
// Multiples amount times currency
public double multiplyCurrency(String answer, double amount) {
newCurrency += ct.get(getAnswer()) * getAmount();
return newCurrency;
}
public double getNewCurrency() {
return this.newCurrency;
}
// Checks user if Map contains userinput.
public boolean checkAnswer(String answer) {
if (!ct.containsKey(answer)) {
return false;
} else {
return true;
}
}
@Override
public String toString() {
return "You have converted $" + getAmount() + " into " + getNewCurrency() + " " + this.answer + "\n";
}
}
Logic class:
package logic;
import java.util.Scanner;
import currency.Currency;
public class Logic {
private Currency currency;
private Scanner reader;
private boolean running;
private String answer;
public Logic() {
this.currency = new Currency();
this.reader = new Scanner(System.in);
this.running = true;
this.answer = null;
}
public void start() {
//Runs until quit
while (running) {
System.out.println("Convert US dollar into which currency?");
currency.loadMap();
answer = reader.nextLine();
currency.setAnswer(answer);
// checks to make sure the value is in the map
if (currency.checkAnswer(currency.getAnswer()) == true) {
System.out.println("\n" + "How many dollars would you like converted?");
double amount = reader.nextDouble();
currency.setAmount(amount);
currency.multiplyCurrency(currency.getAnswer(), currency.getAmount());
System.out.println("");
System.out.println(currency);
} else {
System.out.println("Please select a currency");
}
}
}
}
Main Class:
package logic;
public class Main {
public static void main(String[] args) {
Logic l = new Logic();
l.start();
}
}
I would really appreciate any feedback so I can make note and and never have this happen again. Thanks in advance.