-
Notifications
You must be signed in to change notification settings - Fork 107
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
ToofastTurnip
wants to merge
2
commits into
ZipCodeCore:master
Choose a base branch
from
ToofastTurnip:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) { | ||
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; | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.