Hi everyone,
I'm fairly new here. This is my second post. I'm just learning how to use c#, and I was wondering if someone here can help me with arrays.
-
First, I want to declare and create an array of 5 elements. I created another class called Account, and I want the array to be of type Account.
-
Second, I want to assign the existing objects of the class I made (Account) to the elements of the array
-
Lastly, I'm trying to figure out how to use a for loop to call the method display from each element of the array.
I've literally tried everything, and I just cant seem to get this.
I want my new code to get the same results as my old code, but using an array (assigning the existing objects to the arrays elements) and a for loop to call the display method from each of the arrays elements.
I know first I need to declare a constant for max size, so I started off writing this
public const int MAX_SIZE = 5;
Then, to have an array declared with elements of the Account class, I wrote this under Main
int[] AccountArray = new Account[5];
At this point I'm lost. I tried looking online and in my textbook, but I can't find any examples specific to what I'd like to accomplish. Here is my original code that I want to modify
static void Main(string[] args)
{
Account acct = new Account(100, 2.5);
int choice;
double bal, r, amt;
int y;
double fut;
Console.WriteLine("\nPlease make your selection:");
Console.WriteLine(" 0 - create an account");
Console.WriteLine(" 1 - deposit money");
Console.WriteLine(" 2 - withdraw money");
Console.WriteLine(" 3 - add monthly interest");
Console.WriteLine(" 4 - calculate future value");
Console.WriteLine(" or -1 to finish");
choice = Convert.ToInt32(Console.ReadLine());
while (choice != -1)
{
switch (choice)
{
case 0:
Console.WriteLine("Enter balance:");
bal = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter percent of annual rate:");
r = Convert.ToDouble(Console.ReadLine());
acct = new Account(bal, r);
acct.display();
break;
case 1:
Console.WriteLine("Enter deposit amount:");
amt = Convert.ToDouble(Console.ReadLine());
acct.deposit(amt);
acct.display();
break;
case 2:
Console.WriteLine("Enter withdrawal amount:");
amt = Convert.ToDouble(Console.ReadLine());
acct.withdraw(amt);
acct.display();
break;
case 3:
acct.applyMonthlyInterest();
acct.display();
break;
case 4:
Console.WriteLine("Enter number of years for future value calculation:");
y = Convert.ToInt32(Console.ReadLine());
fut = acct.futureBalance(y);
Console.WriteLine("In {0} years you will have {1:c}." , y, fut);
break;
}
Console.WriteLine("\nPlease make your selection:");
Console.WriteLine(" 0 - create an account");
Console.WriteLine(" 1 - deposit money");
Console.WriteLine(" 2 - withdraw money");
Console.WriteLine(" 3 - add monthly interest");
Console.WriteLine(" 4 - calculate future value");
Console.WriteLine(" or -1 to finish");
choice = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Press any key, and good bye...");
Console.ReadKey();