-2

I am writing a C# console application where I have a class Customer as following:

    public class Customers
    {
        public string Name { get; set; }
        public string UserID { get; set; }
        public int Pin { get; set; }
        public int AccountNo { get; set; }
        public string AccountType { get; set; }
        public int Balance { get; set; }
        public string Status { get; set; }
    }

I am storing the customer data in text file as below ("Customers.txt"):

[
    {"Name":"Jack Willson","UserID":"Jack21","Pin":12345,"AccountNo":1,"AccountType":"Savings","Balance":2000,"Status":"Active"},
    {"Name":"Mary Poppins","UserID":"mary8912","Pin":67890,"AccountNo":2,"AccountType":"Savings","Balance":4567,"Status":"Active"},
    {"Name":"Harry Potter","UserID":"Harry45","Pin":12345,"AccountNo":4,"AccountType":"Savings","Balance":12000,"Status":"Active"}
]

I am reading this file as :

List<Customers> list = ReadFile<Customers>("Customers.txt");

I wish to update a specific customer field based on user input. If user leaves the field input blank then the field information remains unchanged.

The program will ask user to enter the AccountNo for which he wishes to update an information. Then program will display each property like UserID, AccountType, Status. If the user doesnot enter any input for any of the property then information remains unchanged. I was trying to save the input from user in new Customer() object but couldn't proceed to compare it or save it in List<customers> where i am storing the data from text file. How can I achieve this?

  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then check the [help/on-topic] to see what questions you can ask. Please show your attempts you have tried and the problems/error messages you get from your attempts. – Progman Apr 17 '21 at 07:57
  • Uhm, by trying? This is either an assignment or work: you should be able to do this yourself. SO is not a code writing service. We can help you with issues, but you have to put in the effort yourself. – JHBonarius Apr 17 '21 at 08:19

2 Answers2

0

Something like this should do the trick.

var accountNum = 2; //I'm going to assume you get this from Console.ReadLine() and validate it.

//Find the customer with that account number...
var query = list.Where(c => c.AccountNo == accountNum);

if (!query.Any()
    Console.WriteLine("Customer not found.");
    
var customer = query.First();

//Now you can manipulate the customer object as you please
//In your situation I think you want to loop through the properties they can change and set them.
customer.Name = "Some new name";
customer.Pin = 1234;

//Save the updated information back to the text file however you currently do this

Hopefully this helps :)

Ryan Thomas
  • 1,724
  • 2
  • 14
  • 27
0

You can look into this solution using Linq:

int acNo = ReadAccountNumber();
IEnumerable<Customers> results = myList.Where(c => c.AccountNo == acNo);

if you are sure that AccountNo are unique then results will hold just one element. You can update the first element if that exists.

Customers cust = myList.First(c => c.AccountNo == acNo);
cust?.Name = newName;