In this code, I try to code to keep customer data to keep in an array list. I need to keep first name, last name, contact number, and payment method.
public static void Rent()
{
WriteLine("Enter your first name");
input_fname = ReadLine();
WriteLine("Enter your last name");
input_lname = ReadLine();
WriteLine("Enter your contact number");
input_phone = ReadLine();
WriteLine("Enter your payment method");
input_payment = ReadLine();
ArrayList arr_cust = new ArrayList();
for (int i = 0; i <= arr_cust.Count; i )
{
arr_cust.Add(input_fname input_lname input_phone input_payment);
}
WriteLine("Pease enter 1 to return to previous menu and 0 to exit");
int input = Convert.ToInt32(ReadLine());
bool exit = false;
while (exit == false)
{
if (input == 1)
{
Main();
}
else if (input == 0)
{
WriteLine("Thank you for use Rental E-Bike System");
System.Environment.Exit(0);
}
else
{
}
}
}
My problem is when I put all of the value that i need to keep and after the I put 1 to back to main page. Next, I open Rent page again to put the second of value to keep in array list. Next step, I need to show data which I keep in array list.
CodePudding user response:
Your Rent
function creates a new instance of ArrayList
each time it is invoked and that is why arr_cust
is always empty and the only value it has is the one you insert within the Rent
function.
Instead of initializing arr_cust
within Rent
you can initialize it as a static field outside of the function so it retains the previous values.
Also you do not need the for loop to insert your data. You are inserting only once and with the for loop you are going to get N duplicates of that entry. N being the previous item count.
public static ArrayList arr_cust = new ArrayList();
public static void Rent(ArrayList arr_cust)
{
WriteLine("Enter your first name");
input_fname = ReadLine();
WriteLine("Enter your last name");
input_lname = ReadLine();
WriteLine("Enter your contact number");
input_phone = ReadLine();
WriteLine("Enter your payment method");
input_payment = ReadLine();
arr_cust.Add(input_fname input_lname input_phone input_payment);
WriteLine("Pease enter 1 to return to previous menu and 0 to exit");
int input = Convert.ToInt32(ReadLine());
bool exit = false;
while (exit == false)
{
if (input == 1)
{
Main();
}
else if (input == 0)
{
WriteLine("Thank you for use Rental E-Bike System");
System.Environment.Exit(0);
}
}
}