Multidimensional Array help
I'm learning how to implementing Multidimensional arrays and am having a problem properly using them. I will paste the prompt, then the code I have for the assignment thus far.
Assignment:
Modify your prior Lab by adding an array of objects. Within the loop, instantiate each individual object. Make sure the user cannot keep adding another Foreign conversion beyond your array size.
After the user selects quit from the menu, prompt if the user want to display a summary report. If they select ‘Y’ then, using your array of objects, display the following report:
Item Conversion Dollars Amount
1 Japanese Yen 100.00 32,000.00
2 Mexican Peso 400.00 56,000.00
3 Canadian Dollar 100.00 156.00
etc.
Number of Conversions = 3
Here is the code I have thus far. I understand that I probably need a 2dimensional array to accomplish the above result, but I can't seem to get a 2dimensional array to work while using the method to set a Maximum.
Code:
import java.util.Scanner;
public class lab8
{
public static void main(String[] args)
{
int choice;
String s;
char report;
int x = 0;
final int Max=3;
Foreign[] trade = new Foreign[Max];
Scanner input=new Scanner(System.in);
Foreign.title();
do
{
trade[x] = new Foreign();
trade[x].menu();
choice=trade[x].selection;
if (choice>= 1 && choice<= 4)
{
trade[x].dollars();
trade[x].amount();
trade[x].vertical();
System.out.println(trade[x]);
System.out.println("\n");
x++;
}
else if (choice > 4)
{
System.out.println("Please select 1 through 4, or 0 to quit");
}
}
while (choice != 0 && x < Max);
System.out.println("Would you like to see a summary report? Y/N");
s=input.nextLine();
s=s.toUpperCase();
report=s.charAt(0);
if(report == 'Y')
{
System.out.println("\nItem\t\tConversion\t\tDollars\t\tAmount");
for (int i=0; i < x; i++)
{
System.out.println(i+1 + "\t");
System.out.print(trade[i] + "\t");
}
}
Foreign.counters();
}
}
then the classes/objects
Code:
import java.util.Scanner;
import java.text.NumberFormat;
import java.text.DecimalFormat;
public class Foreign
{
static Scanner read=new Scanner(System.in);
public static double rate;
public static String country;
public static int selection;
private static int count=0, total=0;
private static String[] types= {"Canadian Dollars","Mexican Peso","Japanese Yen","Euro"};
private static double[] rates= {1.1553,10.550,117.57,.8407};
private double dollars, amount;
static NumberFormat dollarForm=NumberFormat.getCurrencyInstance();
static DecimalFormat currency=new DecimalFormat("###,###.00");
public Foreign()
{
selection=0;
dollars=0;
amount=0;
}
public static void title()
{
System.out.println("Foreign Exchange calculator");
}
public static void menu()
{
System.out.println("U.S. Dollar to Foreign Currency Exchange");
System.out.println("1. U.S. to Canada");
System.out.println("2. U.S. to Mexico");
System.out.println("3. U.S. to Japan");
System.out.println("4. U.S. to Euro");
System.out.println("0. Quit\n");
System.out.print("\nPlease enter your choice:");
selection=read.nextInt();
}
public void dollars()
{
System.out.print("\nPlease enter the amount of U.S. Dollars: ");
dollars= read.nextDouble();
count++;
total+=dollars;
}
public void amount()
{
rate=rates[selection-1];
amount=dollars*rate;
}
public void vertical()
{
country=types[selection-1];
System.out.println("Country = " +country);
System.out.println("Rate = " +dollarForm.format(rate));
System.out.println("Dollars = " +dollarForm.format(dollars));
System.out.println("Converted value = " +dollarForm.format(amount));
}
public static void counters()
{
System.out.println("\n The total transaction(s): " +count);
System.out.println(" The total amount converted is: " +dollarForm.format(total));
}
public String toString()
{
String horizontal;
horizontal = country + " " + rate + " " +dollarForm.format(dollars) + " " +currency.format(amount);
return horizontal;
}
}
I'm sure the problem lies in the first set of code. As of now, when calling on the Array it will just pull my toString. I need to figure out how to get the desired chart. I think I need to use an ArrayList but I'm not sure how to implement that into the code.