Sunday 5 May 2013

david is a team leader of XYZ organization. He wants to create a scheduler,which will store fields, such as the appointment date,the name of the person to meet, and time.He needs to develop an application that wiil enable the user to fill fields.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Chap7_A1
{
    class Schedule
    {
        struct details
        {
            public DateTime Date;
            public string Appointment;
            public string Time;
            public string Day;
        };
        private details Testdetails;
        static FileStream F;
        StreamWriter W;
        StreamReader R;
        public void Getdata()
        {
            String Duedata;
            Console.Write("Apointment Date (MM/DD/YY) : ");
            Duedata = Console.ReadLine();

            if ((Duedata.Length) != 8)
            {
                Console.WriteLine("Incorrect format of date");
                Console.ReadLine();
                System.Environment.Exit(0);
            }
            if (((Duedata.Substring(2, 1)) != "/") || ((Duedata.Substring(5, 1)) != "/"))
            {
                Console.WriteLine("Incorrect format of date");
                Console.ReadLine();
                System.Environment.Exit(0);
            }
            try
            {
                Testdetails.Date = Convert.ToDateTime(Duedata);
            }
            catch (Exception e)
            {
                Console.WriteLine("Invalid Date");
                System.Environment.Exit(0);
            }
            Testdetails.Day =
                Convert.ToString(Testdetails.Date.DayOfWeek);
            Console.Write("Appointment with: ");
            Testdetails.Appointment = Console.ReadLine();
            Console.Write("Appoinment Time(hr:mn): ");
            Testdetails.Time = Console.ReadLine();

            if ((Testdetails.Time.Length) != 5)
            {
                Console.WriteLine("Incorrect formet of time");
                System.Environment.Exit(0);
            }
            if ((Testdetails.Time.Substring(2, 1)) != ":")
            {
                Console.WriteLine("Incorrect format of time");
                System.Environment.Exit(0);
            }
            F = new FileStream("MyFile.txt", FileMode.Append, FileAccess.Write);
            W = new StreamWriter(F);
            W.Write("Date:");
            W.Write(Testdetails.Date.ToShortDateString());
            W.Write("?");
            W.Write("Day: ");
            W.Write(Testdetails.Day);
            W.Write("?");
            W.Write("Name: ");
            W.Write(Testdetails.Appointment);
            W.Write("?");
            W.Write("Time: ");
            W.Write(Testdetails.Time);
            W.WriteLine("?");

            W.Flush();
            W.Close();
        }
        public void Display()
        {
            string Str;
            F = new FileStream("MyFile.txt",
            FileMode.OpenOrCreate, FileAccess.Read);
            R = new StreamReader(F);
            Console.Clear();
            int Pos = 0;
            while ((Str = R.ReadLine()) != null)
            {
                while (true)
                {
                    Pos = Str.IndexOf("?");
                    if (Pos == -1)
                        break;
                    Console.WriteLine(Str.Substring(0, Pos));
                    Str = Str.Substring(Pos + 1);
                }
                Pos = 0;
            }
            R.Close();
        }
        public void Search()
        {
            string Str, Chkstrl, Chkstr2;
            DateTime DD; int Pos;
            F = new FileStream("MyFile.txt", FileMode.Open,
                FileAccess.Read);
            R = new StreamReader(F);
            Console.Write("Enter Date (MM/DD/YY): ");
            DD = Convert.ToDateTime(Console.ReadLine());
            while ((Str = R.ReadLine()) != null)
            {
                Chkstrl = "Date: " + DD.ToShortDateString();
                Pos = Str.IndexOf("?");
                Chkstr2 = Str.Substring(0, Pos);
                if ((Chkstrl.CompareTo(Chkstrl)) == 0)
                {
                    while (true)
                    {
                        Pos = Str.IndexOf("?");
                        if (Pos == -1)
                            break;
                        Console.WriteLine(Str.Substring(0, Pos));
                        Str = Str.Substring(Pos + 1);
                    }
                    Pos = 0;
                }
            }
            R.Close();
        }
        static void Main(string[] args)
        {
            Schedule S = new Schedule();
            int Ch = 0;
            Console.Clear();
            while(Ch != 4)
            {
                Console.WriteLine("1. Add appointment");
                Console.WriteLine("2. View appointment");
                Console.WriteLine("3. Search");
                Console.WriteLine("4. Exit");
                Console.WriteLine("Enter Choice: ");
                Ch = Convert.ToInt32(Console.ReadLine());
                switch (Ch)
                {
                    case 1: Console.Clear(); S.Getdata();
                        Console.Clear();break;
                    case 2: Console.Clear(); S.Display();
                        Console.ReadLine(); Console.Clear(); break;
                    case 3: Console.Clear(); S.Search();
                        Console.ReadLine(); Console.Clear(); break;
                    case 4: return;
                    default: Console.WriteLine("Invalid Input."); break;}
            }
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment