Skip to content

Instantly share code, notes, and snippets.

@vdonchev
Created May 10, 2016 12:18
Show Gist options
  • Select an option

  • Save vdonchev/382c76d54c32c459e25e0c4a2454cf87 to your computer and use it in GitHub Desktop.

Select an option

Save vdonchev/382c76d54c32c459e25e0c4a2454cf87 to your computer and use it in GitHub Desktop.
namespace RoyalAccounting
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
public static class Program
{
private static readonly HashSet<string> Employees = new HashSet<string>();
private static readonly HashSet<Team> Database =
new HashSet<Team>();
public static void Main()
{
var line = Console.ReadLine();
while (line != "Pishi kuf i da si hodim")
{
var match = Regex.Match(line,
@"^(?<name>[a-zA-Z]+);(?<hours>\d+);(?<payment>\d+\.?\d*);(?<team>[a-zA-Z]+)$");
if (match.Success)
{
var name = match.Groups["name"].Value;
var hours = int.Parse(match.Groups["hours"].Value);
var payment = decimal.Parse(match.Groups["payment"].Value);
var team = match.Groups["team"].Value;
if (!Employees.Contains(name))
{
Employees.Add(name);
var teamObj = Database.FirstOrDefault(t => t.Name == team);
if (teamObj == null)
{
teamObj = new Team(team);
Database.Add(teamObj);
}
var employee = new Employee(name, hours, payment);
teamObj.Employees.Add(employee);
}
}
line = Console.ReadLine();
}
foreach (var team in Database.OrderByDescending(t => t))
{
Console.WriteLine(team);
}
}
}
internal class Team : IComparable<Team>
{
public Team(string name)
{
this.Name = name;
this.Employees = new SortedSet<Employee>();
}
public string Name { get; }
public SortedSet<Employee> Employees { get; }
public int CompareTo(Team other)
{
return this.Employees.Sum(e => e.MonthlyPayment)
.CompareTo(other.Employees.Sum(e => e.MonthlyPayment));
}
public override string ToString()
{
var builder = new StringBuilder();
builder.AppendLine($"Team - {this.Name}");
foreach (var employee in this.Employees)
{
builder.AppendLine(employee.ToString());
}
return builder.ToString().Trim();
}
}
internal class Employee : IComparable<Employee>
{
public Employee(
string name,
int workHours,
decimal dailyPayment)
{
this.Name = name;
this.WorkHours = workHours;
this.DailyPayment = dailyPayment;
this.MonthlyPayment = this.CalculateMonthlyPayment();
}
public string Name { get; private set; }
public int WorkHours { get; private set; }
public decimal DailyPayment { get; private set; }
public decimal MonthlyPayment { get; private set; }
private decimal CalculateMonthlyPayment()
{
return ((decimal)(this.DailyPayment * this.WorkHours) / 24M) * 30M;
}
public int CompareTo(Employee other)
{
var res = other.WorkHours.CompareTo(this.WorkHours);
if (res == 0)
{
res = other.CalcDailyIncome().CompareTo(this.CalcDailyIncome());
if (res == 0)
{
return string.Compare(this.Name, other.Name);
}
}
return res;
}
public override string ToString()
{
return $"$$${this.Name} - {this.WorkHours} - {decimal.Round(this.CalcDailyIncome(), 6, MidpointRounding.AwayFromZero):F6}";
}
private decimal CalcDailyIncome()
{
return (this.DailyPayment * this.WorkHours) / 24M;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment