Answer to Question #242797 in C# for Gayatri

Question #242797

Q3. An institute have decided to automate their batch details operations. The application is to be developed in such a manner that a proper directory structure is to be maintained to store the files. The directory structure to be maintained is as shown below:Academy - 1. Chennai->Chennai.txt 2. Banglore->Banglore.txt 3. Mumbai->Mumbai.txt 4. Pune->Pune.txt .You need to perform operations in C# application Create a menu based application to store batch details.· The first option in the menu should allow the user to create a directory structure and the files (if not exists)in the c drive as shown in the above figure.· The second option should accept the batch details from the user. Based on the location given by the userappend the batch details in the respective files.· The third menu option allows the user to create a backup copy of the Academy folder in D Drive .The fourth option should allow the user to view the details of the all text files


1
Expert's answer
2021-09-27T02:59:07-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;
using System.IO;


namespace C_SHARP
{
    class Program
    {


        private static string parentDirectory = @"C:\Academy";
        private static string backupCopyDirectory = @"F:\Academy";


        //Chennai: Chennai.txt
        private static string childDirectoryChennai = @"C:\Academy\Chennai";
        //Bangalore: Bangalore.txt
        private static string childDirectoryBangalore = @"C:\Academy\Bangalore";
        //Mumbai: Mumbai.txt
        private static string childDirectoryMumbai = @"C:\Academy\Mumbai";
        //Pune: Pune.txt
        private static string childDirectoryPune = @"C:\Academy\Pune";


        //path to the file Chennai.txt
        private static string fileChennai = @"C:\Academy\Chennai\Chennai.txt";
        //path to the file Bangalore.txt
        private static string fileBangalore = @"C:\Academy\Bangalore\Bangalore.txt";
        //path to the file Mumbai.txt
        private static string fileMumbai = @"C:\Academy\Mumbai\Mumbai.txt";
        //path to the file Pune.txt
        private static string filePune = @"C:\Academy\Pune\Pune.txt";


        /// <summary>
        /// The start point of the program
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            int choice = 0;
            while (choice != 5)
            {
                Console.WriteLine("1. Create directory structure and files (if not exists)");
                Console.WriteLine("2. Add batch details");
                Console.WriteLine("3. Create a backup copy of Academy folder in D Drive");
                Console.WriteLine("4. View details of text files");
                Console.WriteLine("5. Exit");
                Console.Write("Your choice: ");
                choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        {
                            CreateDirectoryStructureFiles();
                        }
                        break;
                    case 2:
                        {
                            int choiceSubMenu = 0;
                            while (choiceSubMenu != 5)
                            {
                                Console.WriteLine("1. Add batch details for Chennai");
                                Console.WriteLine("2. Add batch details for Bangalore");
                                Console.WriteLine("3. Add batch details for Mumbai");
                                Console.WriteLine("4. Add batch details for Pune");
                                Console.WriteLine("5. Exit to main menu");
                                Console.Write("Your choice: ");


                                choiceSubMenu = int.Parse(Console.ReadLine());
                                switch (choiceSubMenu)
                                {
                                    case 1:
                                        {
                                            Console.Write("Enter the details infromation for Chennai: ");
                                            string detailsInfromation = Console.ReadLine();
                                            SaveBatchDetailsToFile(fileChennai, detailsInfromation);
                                        }
                                        break;
                                    case 2:
                                        {
                                            Console.Write("Enter the details infromation for Bangalore: ");
                                            string detailsInfromation = Console.ReadLine();
                                            SaveBatchDetailsToFile(fileBangalore, detailsInfromation);
                                        }


                                        break;
                                    case 3:
                                        {
                                            Console.Write("Enter the details infromation for Mumbai: ");
                                            string detailsInfromation = Console.ReadLine();
                                            SaveBatchDetailsToFile(fileMumbai, detailsInfromation);
                                        }
                                        break;
                                    case 4:
                                        {
                                            Console.Write("Enter the details infromation for Pune: ");
                                            string detailsInfromation = Console.ReadLine();
                                            SaveBatchDetailsToFile(filePune, detailsInfromation);
                                        }
                                        break;
                                    case 5:
                                        //exit
                                        break;


                                    default:
                                        Console.WriteLine("Wrong menu item. Try again.");
                                        break;
                                }
                            }
                        }


                        break;
                    case 3:
                        {
                            try
                            {
                                CreateBackupCopy(parentDirectory, backupCopyDirectory);
                                Console.WriteLine("\nThe backup copy of Academy folder in D Drive has been created.\n");
                            }
                            catch (Exception exp) { }


                        }
                        break;
                    case 4:
                        {
                            int choiceSubMenu = 0;
                            while (choiceSubMenu != 5)
                            {
                                Console.WriteLine("1. View batch details for Chennai");
                                Console.WriteLine("2. View batch details for Bangalore");
                                Console.WriteLine("3. View batch details for Mumbai");
                                Console.WriteLine("4. View batch details for Pune");
                                Console.WriteLine("5. Exit to main menu");
                                Console.Write("Your choice: ");


                                choiceSubMenu = int.Parse(Console.ReadLine());
                                switch (choiceSubMenu)
                                {
                                    case 1:
                                        {
                                            ViewatchDetails("Chennai", fileChennai);
                                        }
                                        break;
                                    case 2:
                                        {
                                            ViewatchDetails("Bangalore", fileBangalore);
                                        }


                                        break;
                                    case 3:
                                        {
                                            ViewatchDetails("Mumbai", fileMumbai);
                                        }
                                        break;
                                    case 4:
                                        {
                                            ViewatchDetails("Pune", filePune);
                                        }
                                        break;
                                    case 5:
                                        //exit
                                        break;


                                    default:
                                        Console.WriteLine("Wrong menu item. Try again.");
                                        break;
                                }
                            }
                        }
                        break;
                    case 5:
                        //exit
                        break;


                    default:
                        Console.WriteLine("Wrong menu item. Try again.");
                        break;
                }
            }


            Console.ReadLine();


        }
        /// <summary>
        ///  View batch details
        /// </summary>
        /// <param name="batch"></param>
        /// <param name="fileName"></param>
        private static void ViewatchDetails(string batch, string fileName)
        {
            if (File.Exists(fileName))
            {
                string content = File.ReadAllText(fileName, Encoding.UTF8);
                Console.WriteLine(content);
            }
            else
            {
                Console.WriteLine("\nThe batch details for " + batch + " does not exist.\n");
            }
        }
        /// <summary>
        /// Create directory structure and files
        /// </summary>
        private static void CreateDirectoryStructureFiles()
        {
            // If directory does not exist, create it
            if (!Directory.Exists(parentDirectory))
            {
                Directory.CreateDirectory(parentDirectory);
            }
            if (Directory.Exists(parentDirectory))
            {
                //create child directories
                CreateDirectory(childDirectoryChennai);
                CreateDirectory(childDirectoryBangalore);
                CreateDirectory(childDirectoryMumbai);
                CreateDirectory(childDirectoryPune);
            }
            //Create the files in the folders
            if (Directory.Exists(parentDirectory))
            {


                CreateFile(childDirectoryChennai, fileChennai);
                CreateFile(childDirectoryBangalore, fileBangalore);
                CreateFile(childDirectoryMumbai, fileMumbai);
                CreateFile(childDirectoryPune, filePune);
                Console.WriteLine("\nThe directory structure and files have been created.\n");
            }
        }
        /// <summary>
        /// Creates directory
        /// </summary>
        /// <param name="directory"></param>
        private static void CreateDirectory(string directory)
        {
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
        }
        /// <summary>
        /// Creates file
        /// </summary>
        /// <param name="directory"></param>
        /// <param name="fileName"></param>
        private static void CreateFile(string directory, string fileName)
        {
            if (Directory.Exists(directory))
            {
                if (!(File.Exists(fileName)))
                {
                    File.CreateText(fileName);
                }
            }
        }
        /// <summary>
        /// Saves batch details to the file
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="batchDetails"></param>
        private static void SaveBatchDetailsToFile(string fileName, string batchDetails)
        {
            using (StreamWriter streamWriter = File.AppendText(fileName))
            {
                streamWriter.WriteLine(batchDetails);
            }
        }
        /// <summary>
        /// Create a backup copy of Academy folder in D Drive
        /// </summary>
        /// <param name="sourceFolder"></param>
        /// <param name="backupCopyFolder"></param>
        private static void CreateBackupCopy(string sourceFolder, string backupCopyFolder)
        {
            if (!Directory.Exists(backupCopyFolder))
                Directory.CreateDirectory(backupCopyFolder);
            string[] files = Directory.GetFiles(sourceFolder);
            foreach (string file in files)
            {
                string fileName = Path.GetFileName(file);
                string backupCopyFile = Path.Combine(backupCopyFolder, fileName);
                if ((File.Exists(backupCopyFile)))
                {
                    File.Delete(backupCopyFile);
                    File.Copy(file, backupCopyFile);
                }
                else
                {
                    File.Copy(file, backupCopyFile);
                }
            }
            string[] folders = Directory.GetDirectories(sourceFolder);
            foreach (string folder in folders)
            {
                string fileName = Path.GetFileName(folder);
                string backupCopyFile = Path.Combine(backupCopyFolder, fileName);
                CreateBackupCopy(folder, backupCopyFile);
            }
        }
    }
}

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
New on Blog
APPROVED BY CLIENTS