Skip to content

Instantly share code, notes, and snippets.

@B45i
Last active July 5, 2018 11:46
Show Gist options
  • Save B45i/ed942d8b8fa2f3cb79c9d30b7b150f68 to your computer and use it in GitHub Desktop.
Save B45i/ed942d8b8fa2f3cb79c9d30b7b150f68 to your computer and use it in GitHub Desktop.
C# Exercises
// Exercise - 4 - Date and Strings
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpTask
{
class DateString
{
private string NumToWord (int num) {
IDictionary<int, string> NumDictonary = new Dictionary<int, string>
{
{ 0, "" },
{ 1, "One" },
{ 2, "Two" },
{ 3, "Three" },
{ 4, "Four" },
{ 5, "Five" },
{ 6, "Six" },
{ 7, "Seven" },
{ 8, "Eight" },
{ 9, "Nine" },
{ 10, "Ten" },
{ 11, "Eleven" },
{ 12, "Twelve" },
{ 13, "Thirteen" },
{ 14, "Fourteen" },
{ 15, "Fifteen" },
{ 16, "Sixteen" },
{ 17, "Seventeen" },
{ 18, "Eighteen" },
{ 19, "Nineteen" },
{ 20, "Twenty" },
{ 30, "Thirty" },
{ 40, "Forty" },
{ 50, "Fifty" },
{ 60, "Sixty" },
{ 70, "Seventy" },
{ 80, "Eighty" },
{ 90, "Ninety" }
};
if (num < 20)
{
return NumDictonary[num];
}
else if (num < 100)
{
if (num % 10 == 0)
return NumDictonary[num];
else
return NumDictonary[(num / 10) * 10] + NumDictonary[num % 10];
}
else if (num < 1000)
{
if (num % 100 == 0)
return NumToWord(num / 100) + " Hundred ";
else
return NumDictonary[num / 100] + " Hundred " + NumToWord(num % 100);
}
else
{
if (num % 1000 == 0)
return NumToWord(num / 1000) + " Thousand ";
else
return NumDictonary[num / 1000] + " Thousand " + NumToWord(num % 1000);
}
}
private string NumToMonth(int num) {
IDictionary<int, string> MonthDictonary = new Dictionary<int, string>
{
{ 1, "January" },
{ 2, "February" },
{ 3, "March" },
{ 4, "April" },
{ 5, "May" },
{ 6, "June" },
{ 7, "July" },
{ 8, "August" },
{ 9, "September" },
{ 10, "October" },
{ 11, "November" },
{ 12, "December" }
};
if (num < 0 && num > 12)
{
throw new System.ArgumentException("Invalid argument", "Month");
}
return MonthDictonary[num];
}
public static void Main(string[] args)
{
string DateWordString;
DateString dateString = new DateString();
DateTime dateTime = DateTime.UtcNow.Date;
string timeNow = dateTime.ToString("yyyy/MM/dd").Replace('-', '/');
Console.WriteLine(timeNow);
string[] DateParts = timeNow.Split('/');
DateWordString = $"{dateString.NumToWord(Int32.Parse(DateParts[0]))}" +
$" / {dateString.NumToMonth(Int32.Parse(DateParts[1]))}" +
$" / {dateString.NumToWord(Int32.Parse(DateParts[2]))}";
Console.WriteLine(DateWordString);
Console.ReadKey();
}
}
}
// Exercise - 2 - Class Concept
using System;
namespace CSharpTask
{
class Patient
{
private static string firstName;
public static string FirstName { get { return firstName; } set { firstName = value; } }
private static string lastName;
public static string LastName { get { return lastName; } set { lastName = value; } }
private static int age;
public static int Age { get { return age; } set { age = value; } }
private static char gender;
public static char Gender
{
get { return gender; }
set
{
if (Char.ToUpper(value) == 'M' || Char.ToUpper(value) == 'F' || Char.ToUpper(value) == 'O')
{
gender = value;
}
else
{
throw new System.ArgumentException("Gender can only be Male Female or Other", "Gender");
}
}
}
public static string GetFullName()
{
return firstName + " " + lastName;
}
}
public class PatientUser
{
public static void Main(string[] args)
{
Patient.FirstName = "Amal";
Patient.LastName = "Shajan";
Patient.Age = 21;
Patient.Gender = 'M';
Console.WriteLine(Patient.FirstName);
Console.WriteLine(Patient.LastName);
Console.WriteLine(Patient.GetFullName());
Console.WriteLine(Patient.Age);
Console.WriteLine(Patient.Gender);
Console.ReadKey();
}
}
}
// Exercise - 4 - Date and Strings
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharpTask
{
[Serializable()]
public class InsufficientBalanceException : System.Exception
{
public InsufficientBalanceException() : base() { }
public InsufficientBalanceException(string message) : base(message) { }
public InsufficientBalanceException(string message, System.Exception inner) : base(message, inner) { }
protected InsufficientBalanceException(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
{ }
}
class PatienttAccount
{
private double credit;
protected void AddToPatientCredit(double creditAmount)
{
credit += creditAmount;
}
protected double CheckPatientBalance()
{
return credit;
}
protected void WithDrawFromPatientCredit(double amount)
{
if (credit - amount < 0) {
throw new InsufficientBalanceException("Insufficient balance");
}
credit -= amount;
}
protected double CalculateIntrest(int month)
{
return credit * 0.24 * month;
}
}
class Newton : PatienttAccount
{
public static void Main(string[] args)
{
Newton newton = new Newton();
newton.AddToPatientCredit(100.50);
Console.WriteLine($"Balance : { newton.CheckPatientBalance() }");
try
{
newton.WithDrawFromPatientCredit(20.7);
Console.WriteLine($"Balance : { newton.CheckPatientBalance() }");
}
catch (InsufficientBalanceException e)
{
Console.WriteLine(e);
}
Console.WriteLine("Intrest for one month: " + newton.CalculateIntrest(1) );
Console.ReadKey();
}
}
}
Exercise - 2 - Class Concept
Create a Patient Class for Andrew with the following Properties
FirstName
LastName
Age
Gender
All these properties should have a get and set methods. The methods in this Patient class should be used be used by other class by not creating objects for Patient class.
Exercise - 3 - Collections
Create a collection to store the details of a patient as Key, Value Pairs. This collection should be able to accept any data-type. The following should be stored in the collection
FirstName
LastName
Age
Gender
Exercise - 4 - Date and Strings
Write a program to get the current date in the format YYYY/MM/DD and display it in the console. Also convert this date into words and print it.
Eg: 2017/10/01 => Two Thousand Seventeen / October / One
Exercise - 5 - Class, Method, Access Specifier, Exception Handling
Write a class “PatientCreditDebitAccount” with the following methods
AddToPatientCredit()
CheckPatientBalance()
WithDrawFromPatientCredit()
CalculateInterest() => interest rate for a month is 2.4%
Use access specifiers for methods and properties based on its visibility.
Create another class “Newton” and inherit the class “PatientCreditDebitAccount”. The following logic should be enclosed in a try catch block
Credit 100.50$ to Newton's account
Debit 20.70$ from Newton’s account
Check the Balance
Calculate the interest for the amount in the account for a month
Exercise-1 - Problem Statement:
Andrew is a Patient at Lister Dental Practice (LDP). He owns an insurance Policy such that his Dental Procedures will be waived off 33% from the total billed amount except for the Procedure D0120 and for all Medical Procedure 58% is waived off. The initial credit he owns in his account is $200.
One day he walks into LDP for his Appointment that was scheduled two weeks earlier. His appointment has got 5 procedures to be done(3 dental 2 medical). The details of the Procedures are as follows
D0130 - $85
D0120 - $126
D0140 - $200
20304 - $10
34567 - $99
Display the following in console
The amount he has to pay for each code:
The total amount he has to pay from credit:
The balance credit in his account after treatment:
The discount he will get by using his insurance:
The total amount he has to pay at LDP if any:
Now Andrew decides not to perform a Procedure of his choice after seeing all the details. Now again display the following in console after getting his choice.
The amount he has to pay for each code:
The total amount he has to pay from credit:
The balance credit in his account after treatment:
The discount he will get by using his insurance:
The total amount he has to pay at LDP if any:
The Development code must contain the following.
1. Constants
2. Array
3. For loop
4. Multiple data types
5. Decision Statement
6. Operators
7. Methods, Variables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment