using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace HelloWorld { public class Program { public static void Main(string[] args) { Random rand = new Random(); Room[] rooms = new Room[10]; Profile user = new Profile(); user.setName("Bill Buddy"); user.setStartTime(new DateTime(2021, 6, 16, 7, 0, 0)); for (int i = 1; i < 10; i++) { rooms[i] = new Room(i, rand.Next(0, 7)); Console.WriteLine($"Room {i}"); } rooms[2].Reserve(user.Name, 'A', user.StartTime, 5); Console.WriteLine("Select A Room (0 to exit):"); string selection = Console.ReadLine(); Console.Clear(); string[] choices = { "1. Reserve Room", "2. Check Availability" }; foreach (string choice in choices) { Console.WriteLine(choice); } string sel = Console.ReadLine(); Console.Clear(); int roomsel = Int32.Parse(selection); int logicsel = Int32.Parse(sel); switch (logicsel) { case 1: Console.WriteLine("Name: "); string name = Console.ReadLine(); Console.WriteLine("Desk ID: "); string deskID = Console.ReadLine(); char id = deskID[0]; Console.WriteLine("Date (mm/dd/yyyy): "); string date = Console.ReadLine(); string[] days = date.Split('/'); int[] dayz = { Int32.Parse(days[2]), Int32.Parse(days[1]), Int32.Parse(days[0]) }; DateTime day = new DateTime(DateTime.Now.Year, dayz[2], dayz[1]); Console.WriteLine("Duration: "); string dura = Console.ReadLine(); double duration = Double.Parse(dura); rooms[roomsel].Reserve(name, id, day, duration); break; case 2: break; } } } public class Room { int ID; Desk[] Desks; int Capacity; public Room(int id, int capacity) { ID = id; Capacity = capacity; Desks = new Desk[capacity]; for (int i = 0; i < Desks.Length; i++) { int temp = 65 + i; Desks[i] = new Desk((char)temp); } } public bool Reserve(string name, char deskID, DateTime date, double duration) { if (CheckDesk(deskID, date)) { Profile temp = new Profile(); temp.Name = name; temp.StartTime = date.AddHours(7); temp.EndTime = date.AddHours(duration); GetDesk(deskID).AddProfile(temp); Console.WriteLine($"{name} Reserved Desk {deskID} In Room {ID} on {date} to {temp.EndTime}"); return true; } return false; } bool CheckDesk(char ID, DateTime date) { for (int i = 0; i < Capacity; i++) { if(Desks[i].ID == ID) { foreach (Profile profile in Desks[i].GetProfiles()) { if(profile.StartTime.Day == date.Day) { Console.WriteLine("This Room Is Unavailable."); return false; } } return true; } else if(Desks[i] == null) { Console.WriteLine("ERROR: A Desk Returned NULL"); return false; } } return false; } Desk GetDesk(char ID) { for (int i = 0; i < Capacity; i++) { if (Desks[i].ID == ID) { return Desks[i]; } } return null; } } public class Desk { public char ID; List<Profile> Profiles; public Desk(char id) { ID = id; Profiles = new List<Profile>(); } public void AddProfile(Profile profile) { Profiles.Add(profile); } public void RemoveProfile(Profile profile) { Profiles.Remove(profile); } public List<Profile> GetProfiles() { return Profiles; } } public class Profile { public string Name; public DateTime StartTime; public DateTime EndTime; public void setName(string name) { Name = name; } public void setStartTime(DateTime StartTime) { this.StartTime = StartTime; } } }
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
}