Answer to Question #241247 in C# for Gayatri

Question #241247

Q4) Create a class named User with the following attributes and it needs above address entity as subelement name of type StringmobileNumber of type String

email of type Stringaddress of type AddresscreditCardNumber of type String

Include appropriate getters, setters and constructors.

Include methods to Search for user details using mobile number. If no records, print “No records found”.Search for user details using mobile number and then change the email id of the above user to new id provided. If no records exist, print “No records found”.

List the names of users in which the given string is a substring. List the users sorted in alphabetical order. If no records exist, print “No records found”.

List the names of users from a particular district. List the users sorted in alphabetical order. If no records exist, print “No records found”.

Write a main method to test the above class. Input and Output Format:

All text in bold corresponds to inp


1
Expert's answer
2021-09-26T02:42:32-0400
using System;
using System.Collections.Generic;


namespace ConsoleApp
{
    public class User
    {
        private string _name;
        private string _adress;
        private string _mobile;
        private string _creditcard;
        private string _email;
        public string Name  
        {
            get => _name;
            set => _name = value;
        }
        public string Adress
        {
            get => _adress;
            set => _adress = value;
        }
        public string MobileNumber
        {
            get => _mobile;
            set => _mobile= value;
        }
        public string CreditCardNumber
        {
            get => _creditcard;
            set=> _creditcard=value;
        }
        public string Email
        {
            get => _email;
            set => _email = value;
        }


        public User(string name, string adress, string mobile, string creditcard, string email)
        {
            _name = name;
            _adress = adress;
            _mobile = mobile;
            _creditcard = creditcard;
            _email = email;
        }
    }
    
        class Program
    {
        public static void  Searchbymobile(List<User> users1,string number)
        {
            foreach (User i in users1)
            {
                if (number == i.MobileNumber)
                {
                    Console.Write("User found name : ");
                    Console.WriteLine(i.Name);
                    return;
                }
            }
            Console.WriteLine("No records found");
        }
        public static void GetMobChangeMail(List<User> users1, string number, string mail)
        {
            foreach (User i in users1)
            {
                if (number == i.MobileNumber)
                {
                    Console.Write("User found name : ");
                    Console.WriteLine(i.Name);
                    Console.Write("User Email : ");
                    Console.WriteLine(i.Email);
                    i.Email = mail;
                    Console.Write("Change  Email : ");
                    Console.WriteLine(i.Email);
                    return;
                }
            }
            Console.WriteLine("No records found");
        }
        public static List<string> GetListName(List<User> users1, string substr)
        {
            List<string> result = new List<string>();
            foreach (User i in users1)
            {
                if (i.Name.Contains(substr))
                    result.Add(i.Name);


            }
            result.Sort();
                return result;
        }
        public static List<string> GetListNameDistr(List<User> users1, string distr)
        {
            List<string> result = new List<string>();
            foreach (User i in users1)
            {
                if (i.Adress.Contains(distr))
                    result.Add(i.Name);


            }
            result.Sort();
            return result;
        }
        static void Main(string[] args)
        {
            // test case all data is fictional coincidence with real characters by accident
            List<User> users = new List<User>();
            List<string> names_contain = new List<string>();
            List<string> names_district= new List<string>();
            string[] name =  new string[] { "Adam ","Stiv","Bob","Divdlon","Alivex"};
            string[] adress = new string[] { "Westing ", "Osting", "Easting", "Central ", "Central" };
            string[] mobile = new string[] { "+256", "+489", "+555", "+321", "+045" };
            string[]  creditcard= new string[] { "237256", "102489", "789555", "404321", "256045" };
            string[] email = new string[] { "2372@6", "10@489", "7895@5", "4@4321", "25@045" };
            // Object list initialization
            for (int i = 0; i < 5; i++)
            {
                users.Add(new User(name[i], adress[i], mobile[i], creditcard[i],email[i]));
            }
            Console.WriteLine("test search by phone");
            Searchbymobile(users, "+489");
            Console.WriteLine("test search by phone and change mail");
            GetMobChangeMail(users, "+555", "@change");
            Console.WriteLine("test ist the names of users in which the given string is a substring.");
            names_contain = GetListName(users, "iv");
            if (names_contain.Count == 0)
            {
                Console.WriteLine("No records found");
            }
            else
            {
                names_contain.ForEach(Console.WriteLine);
            }
            Console.WriteLine("test List the names of users from a particular district.");
            names_district = GetListNameDistr(users, "Central");
            if (names_district.Count == 0)
            {
                Console.WriteLine("No records found");
            }
            else
            {
                names_district.ForEach(Console.WriteLine);
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS