-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.cs
106 lines (95 loc) · 4.02 KB
/
Utility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Security.Cryptography;
using System.Text;
namespace CovidAirlines
{
public class Utility
{
/// <summary>
/// Hashing function using SHA-512
/// </summary>
/// <param name="pass">Raw password as a string</param>
/// <returns>SHA-512 Hash</returns>
public static byte[] GenerateHash(string pass)
{
using (SHA512 shaM = new SHA512Managed())
{
return shaM.ComputeHash(Encoding.UTF8.GetBytes(pass));
}
}
/// <summary>
/// Set hours, minutes, and seconds to current UTC date
/// </summary>
/// <param name="hours">Hours in military time</param>
/// <param name="minutes">Minutes in military time</param>
/// <param name="seconds">Optional: Seconds in military time</param>
/// <returns>Current UTC date with given time</returns>
public static DateTime Time(int hours, int minutes, int seconds = 0)
{
return new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, hours, minutes, seconds);
}
/// <summary>
/// We award 10 points for every dollar spent
/// </summary>
/// <param name="ticketPrice"></param>
/// <returns>Points</returns>
public static int CalculatePointsAwarded(double ticketPrice)
{
return Convert.ToInt32(ticketPrice * 10);
}
/// <summary>
/// Calculate ticket price based on distance between given cities
/// - Fixed price of $50
/// - Every connection is charged $8 for the TSA
/// - Charge 12 cents per mile
/// </summary>
/// <param name="city1ID">Origin City ID</param>
/// <param name="city2ID">Destination City ID</param>
/// <returns>Ticket Price based on distance between given cities</returns>
public static decimal CalculateTicketPrice(int city1ID, int city2ID)
{
double fixedPrice = 50.00 + 8.00;
return Convert.ToDecimal(fixedPrice + CalculateDistanceBetween(city1ID, city2ID) * 0.12);
}
/// <summary>
/// Degree to radian conversion
/// </summary>
/// <param name="degree">Latitude or longitude</param>
/// <returns>Radians</returns>
private static double ToRadian(double degree)
{
return (Math.PI / 180) * degree;
}
/// <summary>
/// Calculate distance between two cities using the Haversine formula
/// Creds to jammin77 on GitHub
/// </summary>
/// <param name="city1ID">Origin City ID</param>
/// <param name="city2ID">Destination City ID</param>
/// <returns>Distance, in miles, between two cities</returns>
public static double CalculateDistanceBetween(int city1ID, int city2ID)
{
using (var entities = new CovidAirlinesEntities())
{
City city1 = entities.Cities.Find(city1ID);
City city2 = entities.Cities.Find(city2ID);
if (city1 == null || city2 == null)
{
return 0;
}
double latitude1 = decimal.ToDouble(city1.Latitude);
double latitude2 = decimal.ToDouble(city2.Latitude);
double latitudeDifference = ToRadian(latitude2 - latitude1);
double longitude1 = decimal.ToDouble(city1.Longitude);
double longitude2 = decimal.ToDouble(city2.Longitude);
double longitudeDifference = ToRadian(longitude2 - longitude1);
double a = Math.Sin(latitudeDifference / 2) * Math.Sin(latitudeDifference / 2) +
Math.Cos(ToRadian(latitude1)) * Math.Cos(ToRadian(latitude2)) *
Math.Sin(longitudeDifference / 2) * Math.Sin(longitudeDifference / 2);
double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
double d = 3960 * c;
return d;
}
}
}
}