Friends database, using files Learn programming C#

Lesson:

File Management


Exercise:

Friends database, using files


Objetive:

Expand the "friends database", so that it loads data from file at the beginning of each session (if the file exists) and saves the data to file when the session ends. Therefore, the data entered in a session must be available for the next session.


Code:

using System;
using System.IO;
public class FriendsDatabase
{
    struct people
    {
        public string name;
        public string email;
        public string address;
        public ushort year;
    }

    public static void Main()
    {
        int total = 275;
        people[] p = new people[total];
        int amount = 0;

        char option;
        bool found;
        string textSearch;
        string name = "friends.dat";

        if (File.Exists(name))
        {
            StreamReader file = File.OpenText(name);
            amount = Convert.ToInt32(file.ReadLine());

            file.ReadLine();

            for (int i = 0; i < amount; i++)
            {
                if (i < total)
                {
                    p[i].name = file.ReadLine();
                    p[i].email = file.ReadLine();
                    p[i].address = file.ReadLine();
                    p[i].year = Convert.ToUInt16(file.ReadLine());
                    file.ReadLine();
                }
            }
            file.Close();
        }

        do
        {
            Console.WriteLine("1- Add data");
            Console.WriteLine("2- Show");
            Console.WriteLine("3- View all data");
            Console.WriteLine("4- Show between dates");
            Console.WriteLine("5- Show oldest");
            Console.WriteLine("6- Show fields match");
            Console.WriteLine("0- Exit");

            Console.Write("Enter a option: ");
            option = Convert.ToChar(Console.ReadLine());

            switch (option)
            {

                case '1':
                    if (amount < total - 1)
                    {
                        do
                        {
                            Console.Write("Name: ");
                            p[amount].name = Console.ReadLine();

                            if (p[amount].name.Length > 40)
                                Console.WriteLine("Max 40 letters");
                        }
                        while (p[amount].name.Length > 40);

                        do
                        {
                            Console.Write("Email: ");
                            p[amount].email = Console.ReadLine();

                            if (p[amount].email.Length > 30)
                                Console.WriteLine("Max 30 letters");
                        }
                        while (p[amount].email.Length > 30);

                        do
                        {
                            Console.Write("Address: ");
                            p[amount].address = Console.ReadLine();

                            if (p[amount].address.Length > 150)
                                Console.WriteLine("Max 150 letters");
                        }
                        while (p[amount].address.Length > 150);

                        do
                        {
                            Console.Write("Year: ");
                            p[amount].year = Convert.ToUInt16(Console.ReadLine());

                            if (p[amount].year < 1850 || p[amount].year > 2100)
                                Console.WriteLine("1850-2100");
                        }
                        while (p[amount].year < 1850 || p[amount].year > 2100);

                        amount++;
                        Console.WriteLine();
                    }
                    else
                        Console.WriteLine("Full");

                    break;

                case '2':
                    if (amount == 0)
                        Console.WriteLine("No data");
                    else
                    {
                        for (int i = 0; i < amount; i++)
                        {
                            if (p[i].name.Length <= 30)
                                Console.WriteLine("{0}: Name = {1}", i + 1, p[i].name);
                            else
                                Console.WriteLine("{0}: Name = {1}", i + 1,
                                p[i].name.Substring(0, 30) + "...");

                            if (i % 20 == 19)
                                Console.ReadLine();
                        }
                    }

                    break;
                case '3':
                    Console.Write("Enter the person: ");
                    textSearch = Console.ReadLine();

                    found = false;
                    for (int i = 0; i < amount; i++)
                    {
                        if (textSearch.ToLower() == p[i].name.ToLower())
                        {
                            found = true;
                            Console.WriteLine("{0}: Year = {1}, Email = {2}, Address = {3}",
                            i + 1, p[i].year, p[i].email, p[i].address);
                        }
                    }

                    if (!found)
                        Console.WriteLine("Not exists");

                    break;

                case '4':
                    Console.Write("Enter the first year: ");
                    int year1 = Convert.ToUInt16(Console.ReadLine());

                    Console.Write("Enter the second year: ");
                    int year2 = Convert.ToUInt16(Console.ReadLine());


                    if (year1 > year2)
                    {
                        int aux = year2;
                        year2 = year1;
                        year1 = aux;
                    }

                    found = false;
                    for (int i = 0; i < amount; i++)
                    {
                        if (p[i].year >= year1 && p[i].year <= year2)
                        {
                            Console.Write(p[i].name + " - ");
                            found = true;
                        }
                    }

                    if (!found)
                        Console.WriteLine("Not found");
                    break;


                case '5':
                    if (amount == 0)
                        Console.WriteLine("No data");
                    else
                    {
                        int firstYear = p[0].year;
                        found = false;

                        for (int i = 1; i < amount; i++)
                            if (p[i].year < firstYear)
                                firstYear = p[i].year;

                        for (int i = 0; i < amount; i++)
                        {
                            Console.WriteLine("{0}: Address = {2}, Year = {3}",
                            i + 1, p[i].name, p[i].address, p[i].year);

                            if (Console.ReadLine().ToLower() == "end")
                                break;
                        }
                    }
                    break;

                case '0':
                    break;

                default:
                    Console.WriteLine("Wrong option");
                    break;
            }

        }
        while (option != '0');

        StreamWriter dataFile = File.CreateText(name);
        dataFile.WriteLine(amount);
        dataFile.WriteLine();

        for (int i = 0; i < amount; i++)
        {
            dataFile.WriteLine(p[i].name);
            dataFile.WriteLine(p[i].email);
            dataFile.WriteLine(p[i].address);
            dataFile.WriteLine(p[i].year);
            dataFile.WriteLine();
        }

        dataFile.Close();
    }
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.