Skip to content

Basic currency converter #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<version>1.0-SNAPSHOT</version>

<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Empty file removed src/main/java/DELETEME
Empty file.
64 changes: 64 additions & 0 deletions src/main/java/ExchangeMaster.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import java.text.DecimalFormat;

public class ExchangeMaster {



public final double usDollarRate = 1;
public final double euroRate = 0.94;
public final double britishPoundRate = 0.82;
public final double indianRupeeRate = 68.32;
public final double australianDollarRate = 1.35;
public final double canadianDollarRate = 1.32;
public final double singaporeDollarRate = 1.43;
public final double swissFrancRate = 1.01;
public final double malaysianRinggitRate = 4.47;
public final double japaneseYenRate = 115.84;
public final double chineseYuanRenminbiRate = 6.92;

public String converter (double originalCurrencyAmount, String typeOfOriginalCurrency, String currencyToConvertTo) {
double unformatted = (originalCurrencyAmount/rateReturner(typeOfOriginalCurrency))*rateReturner(currencyToConvertTo);
return formatCurrency(unformatted);
}

public double rateReturner (String requestedRate) {
String formattedInput = requestedRate.toLowerCase();
if (formattedInput.equals("us dollar")) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

burying string literals inside the code makes it hard to use your class.
rework by changing all these to ENUMs and making the ENUM public, that way I might be able to use the class from other code.

return usDollarRate;
} else if (formattedInput.equals("british pound")) {
return britishPoundRate;
} else if (formattedInput.equals("euro")) {
return euroRate;
} else if (formattedInput.equals("indian rupee")) {
return indianRupeeRate;
} else if (formattedInput.equals("australian dollar")) {
return australianDollarRate;
} else if (formattedInput.equals("canadian dollar")) {
return canadianDollarRate;
} else if (formattedInput.equals("singapore dollar")) {
return singaporeDollarRate;
} else if (formattedInput.equals("swiss franc")) {
return swissFrancRate;
} else if (formattedInput.equals("malaysian ringgit")) {
return malaysianRinggitRate;
} else if (formattedInput.equals("japanese yen")) {
return japaneseYenRate;
} else if (formattedInput.equals("chinese yuan renminbi")) {
return chineseYuanRenminbiRate;
} else {
return usDollarRate;
}
}

public String formatCurrency(double value){
DecimalFormat decimalFormat = new DecimalFormat("#.##");
String formatted = decimalFormat.format(value);
int length = formatted.length();
if (formatted.charAt(length - 2) == '.') {
return formatted + '0';
} else {
return formatted;
}
}

}
24 changes: 24 additions & 0 deletions src/main/java/UserInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.Scanner;

public class UserInput {

public static void main(String[] args) {

ExchangeMaster m = new ExchangeMaster();

Scanner input = new Scanner(System.in);

System.out.println("What kind of money you got b?");
String startingCurrencyType = input.nextLine();
System.out.println("How many " + startingCurrencyType + " do you have?");

Scanner input2 = new Scanner(System.in);

Double startingAmount = input2.nextDouble();
System.out.println("And what do you want to convert it to?");
String targetCurrencyType = input.nextLine();
System.out.println("You have $" + m.converter(startingAmount, startingCurrencyType, targetCurrencyType) + " in" + targetCurrencyType);

}

}
Empty file removed src/test/java/DELETEME
Empty file.
138 changes: 138 additions & 0 deletions src/test/java/ExchangeTester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;



public class ExchangeTester {


@Test
public void testFormatter() {
ExchangeMaster m = new ExchangeMaster();
double numberToFormat = 36.7569;
String expected = "36.76";
String actual = m.formatCurrency(numberToFormat);
Assert.assertEquals(expected, actual);
}

@Test
public void testFormatter2() {
ExchangeMaster m = new ExchangeMaster();
double numberToFormat = 0.1;
String expected = "0.10";
String actual = m.formatCurrency(numberToFormat);
Assert.assertEquals(expected, actual);
}

@Test
public void testDollarToEuro() {
ExchangeMaster m = new ExchangeMaster();
double originalAmount = 10.00;
String originalCurrency = "US Dollar";
String convertTo = "Euro";
String expected = "9.40";
String actual = m.converter(originalAmount, originalCurrency, convertTo);
Assert.assertEquals(expected, actual);
}

@Test
public void testEuroToDollar() {
ExchangeMaster m = new ExchangeMaster();
double originalAmount = 10.00;
String originalCurrency = "EURO";
String convertTo = "us DOLLAR";
String expected = "10.64";
String actual = m.converter(originalAmount, originalCurrency, convertTo);
Assert.assertEquals(expected, actual);
}

@Test
public void testEuroToPound() {
ExchangeMaster m = new ExchangeMaster();
double originalAmount = 10.00;
String originalCurrency = "eurO";
String convertTo = "british POUnd";
String expected = "8.72";
String actual = m.converter(originalAmount, originalCurrency, convertTo);
Assert.assertEquals(expected, actual);
}

@Test
public void testPoundToRupee() {
ExchangeMaster m = new ExchangeMaster();
double originalAmount = 10.00;
String originalCurrency = "British Pound";
String convertTo = "indian rupee";
String expected = "833.17";
String actual = m.converter(originalAmount, originalCurrency, convertTo);
Assert.assertEquals(expected, actual);
}

@Test
public void testRupeeToCanada() {
ExchangeMaster m = new ExchangeMaster();
double originalAmount = 10.00;
String originalCurrency = "indian ruPEE";
String convertTo = "canadian dollar";
String expected = "0.19";
String actual = m.converter(originalAmount, originalCurrency, convertTo);
Assert.assertEquals(expected, actual);
}

@Test
public void testCanadaToSingapore() {
ExchangeMaster m = new ExchangeMaster();
double originalAmount = 10.00;
String originalCurrency = "canadian dollar";
String convertTo = "singAPORE Dollar";
String expected = "10.83";
String actual = m.converter(originalAmount, originalCurrency, convertTo);
Assert.assertEquals(expected, actual);
}

@Test
public void testSingaporeToFranc() {
ExchangeMaster m = new ExchangeMaster();
double originalAmount = 10.00;
String originalCurrency = "singapore dollar";
String convertTo = "swiss franc";
String expected = "7.06";
String actual = m.converter(originalAmount, originalCurrency, convertTo);
Assert.assertEquals(expected, actual);
}

@Test
public void testFrancToRinggit() {
ExchangeMaster m = new ExchangeMaster();
double originalAmount = 10.00;
String originalCurrency = "SWISS franc";
String convertTo = "malaysian ringgit";
String expected = "44.26";
String actual = m.converter(originalAmount, originalCurrency, convertTo);
Assert.assertEquals(expected, actual);
}

@Test
public void testRinggitToYen() {
ExchangeMaster m = new ExchangeMaster();
double originalAmount = 10.00;
String originalCurrency = "MALAYSIAN RINGGIT";
String convertTo = "japanese yen";
String expected = "259.15";
String actual = m.converter(originalAmount, originalCurrency, convertTo);
Assert.assertEquals(expected, actual);
}

@Test
public void testYenToRenminbi() {
ExchangeMaster m = new ExchangeMaster();
double originalAmount = 10.00;
String originalCurrency = "japanese YEN";
String convertTo = "Chinese Yuan Renminbi";
String expected = "0.60";
String actual = m.converter(originalAmount, originalCurrency, convertTo);
Assert.assertEquals(expected, actual);
}

}
Binary file added target/classes/ExchangeMaster.class
Binary file not shown.
Binary file added target/classes/UserInput.class
Binary file not shown.
Binary file added target/test-classes/ExchangeTester.class
Binary file not shown.