TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
nazim mahied
NA
12
7k
I need explanation for this code plz ?
Oct 8 2013 1:29 AM
I need explanation for this code plz ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TariffCategory
{
class Program
{
static double getTariff(double usage)
{
const double MIN_MONTHLY_CHARGE = 3.00;
double tariff = 0.0;
var cases = new Dictionary<Func<double, bool>, Action>
{
{x => x < 1, () => tariff = MIN_MONTHLY_CHARGE},
{x => (x >= 1 && x <=200), () => tariff = (usage * 0.218)},
{x => (x >= 201 && x <= 300), () => tariff = (usage * 0.334)},
{x => (x >= 301 && x <= 400), () => tariff = (usage * 0.4)},
{x => (x >= 401 && x <= 500), () => tariff = (usage * 0.402)},
{x => (x >= 501 && x <= 600), () => tariff = (usage * 0.416)},
{x => (x >= 601 && x <= 700), () => tariff = (usage * 0.426)},
{x => (x >= 701 && x <= 800), () => tariff = (usage * 0.437)},
{x => (x >= 801 && x <= 900), () => tariff = (usage * 0.453)},
{x => x >= 901, () => tariff = (usage * 0.454)}
};
cases.First(kvp => kvp.Key(usage)).Value();
return tariff;
}
static void Main(string[] args)
{
int count = 0;
double totalResidentBill = 0.0, totalResidentialUsage = 0.0;
double kwh = 0.0;
List<Double> outputArray = new List<Double>();
Console.Write("Enter the number of houses in residential area: ");
int numOfhouses = Convert.ToInt32(Console.ReadLine());
Double[] inputArray = new Double[numOfhouses];
Console.WriteLine();
Console.WriteLine("Enter the Electricity usage of each house in KWH: ");
Console.WriteLine();
while (numOfhouses > 0)
{
Console.Write("House #" + (count + 1) + ": ");
String readInput = Console.ReadLine();
Double.TryParse(readInput, out kwh);
if (numOfhouses <= 0)
{
break;
}
else
{
inputArray[count] = kwh;
numOfhouses--;
count++;
continue;
}
}
foreach (double value in inputArray)
{
double houseTariff = getTariff(value);
outputArray.Add(houseTariff);
}
Console.WriteLine();
Console.WriteLine("***********Computing House Bills**************");
Console.WriteLine();
for (int i = 0; i < outputArray.Count; i++)
{
totalResidentBill += outputArray[i];
totalResidentialUsage += inputArray[i];
Console.WriteLine("HOUSE #" + (i + 1));
Console.WriteLine("Usage: " + inputArray[i] + "KW/H");
Console.WriteLine("Bill: RM" + outputArray[i].ToString("#0.00"));
Console.WriteLine();
}
string check4plural = "";
if (inputArray.Length > 1)
check4plural = "Houses";
else
check4plural = "House";
Console.WriteLine("*************Total Residence Bill*****************");
Console.WriteLine();
Console.WriteLine("TOTAL NUMBER OF HOUSES: " + inputArray.Length + " " + check4plural);
Console.WriteLine("TOTAL USAGE: " + totalResidentialUsage.ToString() + "KW/H");
Console.WriteLine("TOTAL BILL: RM" + totalResidentBill.ToString("#0.00"));
Console.WriteLine();
Console.ReadKey(true);
}
}
}
Reply
Answers (
0
)
Cad libraries which is compatible with C#.net?
Custom Printing