using System; using System.Collections.Generic; using System.Globalization; using System.Linq; namespace AppointmentScheduler { class Apointment // this class to define class of Apointment { public string Title { get; set; } // The title of Apointment public DateTime Date { get; set; } // The date of Apointment public string Location { get; set; }// The location of Apointment } class Program // class to execute what is required { // this list to add all apointments are added static List<Apointment> apointments = new List<Apointment>(); static void Main(string[] args) // main method { bool noStopping = true; // to check if the user will be continue while (noStopping) // loop to serve all what user wants { // All choices Console.WriteLine("Welcome to Appointment Scheduler!"); Console.WriteLine("Menu:"); Console.WriteLine("1. Schedule Appointment");// to add new apointment Console.WriteLine("2. View Appointments"); // to view all apointments Console.WriteLine("3. Update Appointment"); // to update an apointment Console.WriteLine("4. Delete Appointment"); // to delete an apointment Console.WriteLine("5. Exit"); // To exist from program Console.Write("Enter your choice (1-5):"); // to allow choicing a choice int choice; // save what the user wants if (!int.TryParse(Console.ReadLine(), out choice)) //to read choice from console { // if user enters an invalid choice Console.WriteLine("Invalid choice. Please enter a number from 1-5."); continue; } switch (choice) // check choice { case 1: // case adding AddApointment(); // go to add method break; case 2: // case view all apointments ViewApointments(); // go to view all apointments break; case 3: // case update UpdateApointment(); // go to update method break; case 4: // case delete DeleteApointment();// go to delete method break; case 5: // case end execution noStopping = false;// end the loop break; default: // default case // not choice any available options Console.WriteLine("Invalid choice. Please enter a number from 1-5."); break; } } } static void AddApointment() // add new apointment { if (checkEntryApointment(-1)) //check if user enter valid apointment, pass -1 for adding { Console.WriteLine("Apointment added successfully.");// successfully adding // sort the list after adding new apointment apointments = apointments.OrderBy(p => p.Date).ToList(); } } // check valid apointment inde=-1 for add and the index of apointment // which the user wants to update it static bool checkEntryApointment(int index) { try// try catch any execption { Console.Write("Enter appointment details:"); string myString = Console.ReadLine();// read console int pos = myString.IndexOf(','); // to get the title string title = myString.Substring(0, pos);// get title string second = myString.Substring(pos + 1); // get the rest of line string[] rest = second.Split("at ");// split to get the date and location // parse the date which entry to be exacttly to be dddd dd/MM/yyyy',' hh:mm tt DateTime date = DateTime.ParseExact(rest[0], " dddd dd/MM/yyyy',' hh:mm tt ", CultureInfo.InvariantCulture); // rest[0]: date and rest[1] represent location string location = rest[1]; if (index == -1)// to know if user want to add new apointment { // create new object of Apointment Apointment apoint = new Apointment { Title = title, Date = date, Location = location }; // add new apointment to list apointments.Add(apoint); return true; // return true to addApointment method } else // else to update details of updated Apointment { // update date apointments[index].Date = date; // update title apointments[index].Title = title; // update location apointments[index].Location = location; return true; // return true to updateApointment method } } catch (Exception e) // catch if it occurs an execption { // to show where the error occurs Console.WriteLine("Entry must be as: title, dddd dd/MM/yyyy, hh:mm tt at location "); return false; // return false because no change } } static void ViewApointments() // view all apointments where existed { if (apointments.Count == 0) // check if the list empty { Console.WriteLine("No Apointments found.");// show message to user return; // exit from method } Console.WriteLine("Apointments:");// the list not empty show all Apointments // to show all, we make loop to pass every apointment foreach (Apointment apointment in apointments) { // show all details of apointment Console.WriteLine("{0}, {1} at {2}", apointment.Title, apointment.Date.ToString("MM/dd/yyyy hh:mm tt"), apointment.Location); } } static void UpdateApointment() // update Apointment { if (apointments.Count == 0) // list of Apointments is empty { Console.WriteLine("No Apointments found.");// show message return; // exit from method } // message to enter the index of appointment Console.Write("Enter the index of the appointment to update: "); if (!int.TryParse(Console.ReadLine(), out int index) // read index || index < 1 || // if index not valid index > apointments.Count) // index is larger of size { // message show if this entry index not valid Console.WriteLine("Invalid index. Please enter a number between 1 and {0}.", apointments.Count); return;// exit from method } // index in list begin from 0 so we subtract 1 from it // and then execute the check new Entry of updated apointment if (checkEntryApointment(index-1)) { // sort the list after update this list apointments = apointments.OrderBy(p => p.Date).ToList(); // show message successful Console.WriteLine("Apointment updated successfully."); } } static void DeleteApointment()// delete an appointment { if (apointments.Count == 0)// the list of appointments is empty { Console.WriteLine("No Apointments found."); // show message return;// exit from method } // enter the index of the apointment which the user wants to delete it Console.Write("Enter the index of the appointment to delete:"); // check if index is invalid if (!int.TryParse(Console.ReadLine(), out int index) || index < 1 || index > apointments.Count) { // message to show the entry index is invalid Console.WriteLine("Invalid index. Please enter a number between 0 and {0}.", apointments.Count); return; //exit from method } // index in list begin from 0 so we subtract 1 from it apointments.RemoveAt(index-1); // message to show successful deletion Console.WriteLine("Apointment deleted successfully."); } } }
Write, Run & Share C# code online using OneCompiler's C# online compiler for free. It's one of the robust, feature-rich online compilers for C# language, running on the latest version 8.0. Getting started with the OneCompiler's C# compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C#
and start coding.
OneCompiler's C# online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
using System;
namespace Sample
{
class Test
{
public static void Main(string[] args)
{
string name;
name = Console.ReadLine();
Console.WriteLine("Hello {0} ", name);
}
}
}
C# is a general purpose object-oriented programming language by Microsoft. Though initially it was developed as part of .net but later it was approved by ECMA and ISO standards.
You can use C# to create variety of applications, like web, windows, mobile, console applications and much more using Visual studio.
Data Type | Description | Range | size |
---|---|---|---|
int | To store integers | -2,147,483,648 to 2,147,483,647 | 4 bytes |
double | to store large floating point numbers with decimals | can store 15 decimal digits | 8 bytes |
float | to store floating point numbers with decimals | can store upto 7 decimal digits | 4 bytes |
char | to store single characters | - | 2 bytes |
string | to stores text | - | 2 bytes per character |
bool | to stores either true or false | - | 1 bit |
datatype variable-name = value;
When ever you want to perform a set of operations based on a condition or set of few conditions IF-ELSE is used.
if(conditional-expression) {
// code
}
else {
// code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement) {
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type[] array-name;
Method is a set of statements which gets executed only when they are called. Call the method name in the main function to execute the method.
static void method-name()
{
// code to be executed
}