Skip to content

Instantly share code, notes, and snippets.

@rockarts
Last active June 17, 2024 15:26
Show Gist options
  • Save rockarts/e39758d5fd254bfd5962326ba679f11f to your computer and use it in GitHub Desktop.
Save rockarts/e39758d5fd254bfd5962326ba679f11f to your computer and use it in GitHub Desktop.
HackerRank
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
public enum Category
{
InformationTechnologies,
HumanResources,
Accounting,
Sales,
Marketing,
Legal
}
interface IEmployee
{
public string FullName { get; set; }
public int PointLevel { get; set; }
public List<Category> AssignedCategories { get; set; }
}
interface ITicket
{
public int Id { get; set; }
public string Name { get; set; }
public Category Category { get; set; }
public int Point { get; set; }
public bool IsCompleted { get; set; }
public string AssignedEmployee { get; set; }
}
interface IHelpDesk
{
public void AddTicket(ITicket ticket);
public void AddEmployee(IEmployee employee);
public void CompleteTicket(string employeeFullName, int ticketId);
public int GetWaitingTicketCount();
public int GetCompletedTicketsTotalPoint();
public List<(Category category, int totalPoint)> GetTicketsTotalPointByCategory();
public List<(IEmployee employee, int totalPoint)> GetTicketsTotalPointByEmployee();
}
/*
* Create the Employee, Ticket and HelpDesk classes
*/
class Employee: IEmployee{
public string FullName { get; set; }
public int PointLevel { get; set; }
public List<Category> AssignedCategories { get; set; }
public Employee(string FullName, int PointLevel, List<Category> AssignedCategories) {
this.FullName = FullName;
this.PointLevel = PointLevel;
this.AssignedCategories = AssignedCategories;
}
}
class Ticket: ITicket {
public int Id { get; set; }
public string Name { get; set; }
public Category Category { get; set; }
public int Point { get; set; }
public bool IsCompleted { get; set; }
public string AssignedEmployee { get; set; }
public Ticket(int Id, string Name, Category Category, int Point) {
this.Id = Id;
this.Name = Name;
this.Category = Category;
this.Point = Point;
this.AssignedEmployee = "";
}
}
class HelpDesk: IHelpDesk {
List<IEmployee> Employees = [];
List<ITicket> Tickets = [];
public void AddTicket(ITicket ticket) {
Tickets.Add(ticket);
}
public void AddEmployee(IEmployee employee) {
Employees.Add(employee);
}
public void CompleteTicket(string employeeFullName, int ticketId) {
//Employee exists in Employees
if(!Employees.Any(employee => employee.FullName.Equals(employeeFullName))) {
return;
}
//Ticket Exists in Tickets
if(!Tickets.Any(ticket => ticket.Id == ticketId)) {
return;
}
IEmployee employee = Employees.First(employee => employee.FullName.Equals(employeeFullName));
ITicket ticket = Tickets.First(ticket => ticket.Id == ticketId);
//Ticket is not yet marked complete
//Employee point level is > the tickets point value
if(!ticket.IsCompleted && employee.PointLevel > ticket.Point) {
ticket.IsCompleted = true;
ticket.AssignedEmployee = employeeFullName;
}
}
public int GetWaitingTicketCount() {
return Tickets.Where(ticket => ticket.IsCompleted == false).Count;
}
public int GetCompletedTicketsTotalPoint() {
var completedTickets = Tickets.Where(ticket => ticket.IsCompleted == true);
return completedTickets.Sum(ticket => ticket.Point);
}
public List<(Category category, int totalPoint)> GetTicketsTotalPointByCategory() {
return Enum.GetValues(typeof(Category))
.Cast<Category>()
.Select(category => (
category,
Tickets.Where(ticket => ticket.IsCompleted && ticket.Category == category)
.Sum(ticket => ticket.Point)
))
.Where(result => result.Item2 > 0)
.ToList();
}
public List<(IEmployee employee, int totalPoint)> GetTicketsTotalPointByEmployee() {
List<(IEmployee employee, int totalPoint)> totalPoints = new();
foreach (var employee in Employees)
{
int employeeTotalPoints = SumEmployeeTickets(employee.FullName);
if (employeeTotalPoints > 0)
{
totalPoints.Add((employee, employeeTotalPoints));
}
}
return totalPoints;
}
private int SumEmployeeTickets(string employeeFullName) {
return Tickets
.Where(ticket => ticket.IsCompleted && ticket.AssignedEmployee == employeeFullName)
.Sum(ticket => ticket.Point);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment