1. Create a getSymbol method that returns the currency symbol for the currency u
ID: 3555711 • Letter: 1
Question
1. Create a getSymbol method that returns the currency symbol for the currency used in this transaction. This does not take parameters and returns a String. Using a switch statement and the currency type, determine which symbol to return. Use the table to determine what symbol to return. You can use the escape sequence to get the symbol for the pound, euro, and yen.
2. Create a getAmountAsDollar method that returns the amount of the transaction converted to dollars. This does not take parameters and returns a double. Using a switch statement and the transaction type, determine how much the transaction is in dollars. Use the table to determine the conversion between the current currency and the dollar (i.e. One pound is 1.6589 dollars). Return the converted amount.
3. Create a formatString method that takes a String and adds spaces to it until it reaches a desired length. This takes an int (the desired length) and a String (the String to lengthen) as parameters and returns a String. First, determine the difference between the desired length (the int parameter) and the length of the String (the other parameter). You will also want a local String variable that will store the spaces needed to pad the String parameter to the desired length.
Explanation / Answer
Hi please find below the program,
The below program will work on a currency.txt table where there are conversion for Yen and pound are added.
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
public class CurrencyTransaction {
public static void main(String[] args) {
CurrencyTransaction ct = new CurrencyTransaction();
double amt = ct.getAmountAsDollars();
String symbol = ct.getSymbol();
System.out.println("1 " + symbol + " is "
+ ct.formatString(1, amt + "$"));
}
String getSymbol() {
String symbol = "";
try {
Scanner sc = new Scanner(System.in);
System.out
.println("Enter from currency, the symbol equivalent will be returned");
String fromCrr = sc.next();
File f = new File("currency.txt");
Scanner scanner = new Scanner(f);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String words[] = line.split("\s");
char c = words[1].charAt(0);
switch (c) {
case '
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.