Tip Sample program for classes (personnel program version 2)

eiman13562

Member
Joined
Aug 1, 2021
Messages
5
Programming Experience
Beginner
Program.cs :

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Person_Class_Practice
{
    class Program
    {
        static void Main(string[] args)

        {
            try
            {
                // برنامه پرسنل دوباره با استفاده از کلاس ها بازنگری شده
                // Personnel schedule revised using classes
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Welcome to Person App V2");
                Console.WriteLine(".");
                Console.WriteLine(".");
                Console.WriteLine(".");
                Console.WriteLine(".");
                Console.WriteLine(".");
                Console.WriteLine(".");
                Console.WriteLine(".");
                Console.ResetColor();
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("How many Create Person ?");
                Console.ResetColor();
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                int CountPerson = Convert.ToInt32(Console.ReadLine());
                Console.ResetColor();
                Person[] Peoples = new Person[CountPerson]; // ساخت شئی جدید بصورت آرایه
                // Build a new object as an array

                for (int i = 0; i < CountPerson; i++)
                {
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine($"Please enter Person Name [{(i + 1)}] : "); // بهتره حلقه عدد صفر را چاپ نکند پس با یک هر بار جمع شده
                    // It is better not to print the circle of zero, so it is added together one at a time
                    Console.ResetColor();
                    Console.ForegroundColor = ConsoleColor.DarkBlue;
                    string name = Console.ReadLine();
                    Console.ResetColor();

                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine($"Please enter Person family [{(i + 1)}] : ");
                    Console.ResetColor();
                    Console.ForegroundColor = ConsoleColor.DarkBlue;
                    string family = Console.ReadLine();
                    Console.ResetColor();

                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine($"Please enter Person Age [{(i + 1)}] : ");
                    Console.ResetColor();
                    Console.ForegroundColor = ConsoleColor.DarkBlue;
                    int age = Convert.ToInt32(Console.ReadLine());
                    Console.ResetColor();

                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine($"Please enter Person City [{(i + 1)}] : ");
                    Console.ResetColor();
                    Console.ForegroundColor = ConsoleColor.DarkBlue;
                    string city = Console.ReadLine();
                    Console.ResetColor();

                    Person p = new Person(name, family, age, city); // ساخت شئی داخل حلقه
                    // Build the object inside the ring
                    Peoples[i] = p; // مقدار دهی شئی آرایه با شئی داخل حلقه
                    // Value the array object with the object inside the loop



                }

                foreach (Person person in Peoples)
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine($"FullName : {person.name} {person.family} Age : {person.age} City : {person.city} ");
                    Console.ResetColor();
                }
                Console.ReadKey();
            }
            catch(FormatException)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Warning :  Just only enter  Number.");
                Console.ResetColor();
                Console.ReadKey();
            }
            catch
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Warning :  Unknown error.");
                Console.ResetColor();
                Console.ReadKey();
            }
        }
    }


Person.cs :

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Person_Class_Practice
{
    class Person
    {
        public string name;
        public string family;
        public int age;
        public string city;

        public Person ( string name , string family,int age,string city)
        {
            this.name = name;
            this.family = family;
            this.age = age;
            this.city = city;
        }
    }
}

Iman Pour Soltani From Iran
Thank You For use
 
Last edited by a moderator:
Please learn to use code tags. I'm going to edit your post above as a favor.
 
Also, don't just post some code and expect us to know what to do with it. Most people post code here because they have a question about a problem. If you're providing code for others to use then explain that and also select the Tip prefix for your thread, so we know that it's not a question. We shouldn't have to spend time working out stuff like that.
 
Also, don't just post some code and expect us to know what to do with it. Most people post code here because they have a question about a problem. If you're providing code for others to use then explain that and also select the Tip prefix for your thread, so we know that it's not a question. We shouldn't have to spend time working out stuff like that.


very well . I did not know that only in this case the question is answered. And code samples and transfer of educational issues are prohibited. Most console programs are for beginners learning the language and are familiar with the rules and structure of the language. But if I just have to send the code to fix the problems. And this is against the rules of this association. I will not send anymore. Only if I have a problem will I ask a question like the others.
 
Please learn to use code tags. I'm going to edit your post above as a favor.



Hello . If you want me to explain each command. I will write a comment in front of each command and block of code. Because I gave a general explanation about the performance of the program if more explanation is needed. Please let me know so I can add more details for the commands you are looking for. Thanks
 
very well . I did not know that only in this case the question is answered. And code samples and transfer of educational issues are prohibited. Most console programs are for beginners learning the language and are familiar with the rules and structure of the language. But if I just have to send the code to fix the problems. And this is against the rules of this association. I will not send anymore. Only if I have a problem will I ask a question like the others.
Please read what I actually wrote. I did not say that you cannot post sample programs like this. If you can post working code samples that can help people then that's great. What I said was that MOST threads here are asking for help so, if you're not doing that, please identify that fact clearly so that we don't have to waste our time working it out for ourselves. The Tip prefix is specifically intended to do that and should be used in such cases, clearly differentiating such threads from questions, which SHOULD have the Question prefix, but many people don't bother making the effort to add that either. You should also provide a FULL and CLEAR explanation of the code in the thread, rather than just dumping code with little to no explanation and expecting the beginners whom it might help to trawl through it and work out for themselves whether it's relevant to them. It doesn't take much extra effort to make your thread significantly more helpful and avoid wasting anyone's time.
 
I'm sorry, but I couldn't take it anymore. I had to scratch the itch to rewrite the sample code above because it desperately needed improvement. Here's the updated code:
Person.cs:
namespace PersonClassPractice
{
    class Person
    {
        public string Name { get; set; }
        public string Family { get; set; }
        public int Age { get; set; }
        public string City { get; set; }

        public Person(string name, string family, int age, string city)
        {
            Name = name;
            Family = family;
            Age = age;
            City = city;
        }

        public override string ToString()
            => $"FullName : {Name} {Family} Age : {Age} City : {City}";
    }
}

Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;

namespace PersonClassPractice
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                WriteTitle();
                var people = GetPeople();
                ShowPeople(people);
            }

            catch (Exception e)
            {
                ColorIO.WriteLine(ConsoleColor.Red, $"Unknown error: {e}");
            }

            Console.ReadKey();

            void WriteTitle()
            {
                ColorIO.WriteLine(ConsoleColor.Green, "Welcome to Person App V2");
                ColorIO.WriteLines(ConsoleColor.Green, Enumerable.Repeat(".", 7));
            }

            List<Person> GetPeople()
            {
                int personCount = ColorIO.PromptInt("How many Persons to create?", ConsoleColor.Yellow, ConsoleColor.DarkYellow);
                var people = new List<Person>();
                for (int i = 0; i < personCount; i++)
                    people.Add(CreatePersonFromUserInput(i + 1));
                return people;
            }

            Person CreatePersonFromUserInput(int displayIndex)
            {
                ColorIO.WriteLine(ConsoleColor.Blue, $"Please enter details about Person [{displayIndex}]:");
                var name = ColorIO.Prompt("Name: ");
                var family = ColorIO.Prompt("Family: ");
                var age = ColorIO.PromptInt("Age: ");
                var city = ColorIO.Prompt("City: ");
                return new Person(name, family, age, city);
            }

            void ShowPeople(IEnumerable<Person> people)
                => ColorIO.WriteLines(ConsoleColor.Cyan, people);
        }
    }
}

ColorIO.cs:
using System;
using System.Collections.Generic;

namespace PersonClassPractice
{
    static class ColorIO
    {
        const ConsoleColor DefaultPromptColor = ConsoleColor.Blue;
        const ConsoleColor DefaultInputColor = ConsoleColor.DarkBlue;

        public static void WriteLine<T>(ConsoleColor color, T value)
        {
            Console.ForegroundColor = color;
            Console.WriteLine(value);
            Console.ResetColor();
        }

        public static void WriteLines<T>(ConsoleColor color, IEnumerable<T> values)
        {
            Console.ForegroundColor = color;
            foreach(var value in values)
                Console.WriteLine(value);
            Console.ResetColor();
        }

        public static string ReadLine(ConsoleColor color)
        {
            Console.ForegroundColor = color;
            string value = Console.ReadLine();
            Console.ResetColor();
            return value;
        }

        public static string Prompt(string prompt,
                                    ConsoleColor promptColor = DefaultPromptColor,
                                    ConsoleColor inputColor = DefaultInputColor)
        {
            WriteLine(promptColor, prompt);
            return ReadLine(inputColor);
        }

        public static int PromptInt(string prompt,
                                    ConsoleColor promptColor = DefaultPromptColor,
                                    ConsoleColor inputColor = DefaultInputColor)
        {
            do
            {
                WriteLine(promptColor, prompt);
                string input = ReadLine(inputColor);
                if (int.TryParse(input, out int value))
                    return value;
                WriteLine(ConsoleColor.Red, "Warning: Just only enter integers.");
            } while (true);
        }
    }
}

The main concept that has been applied to the refactoring of the original code into this version in the single responsibility principle. This is the the 'S' in SOLID object oriented programming principles. You want to have sections of code only do one thing -- e.g. only have a single responsibility.

Your code had screen color manipulation of IO interleaved with the main logic of your code. That made following the code really hard. Now it's been simplified to having Main() just have a single job: coordinate the calling of the 3 major sections: show a title, get all input from the user, and print out what was entered. (lines 13-15 of Program.cs). All the screen color manipulation has been shunted off to a separate ColorIO class whose job is to do console color manipulation.
 
Back
Top Bottom