I submitted a technical assignment for a job interview process and I am stressing out wondering if my submission was any good. I would like to learn and be a better developer, so please give me pointers about what I could improve on.
using System;
namespace ParsetheParcel
{
interface ICalculatable<Parcel>
{
string Calculator(Parcel p);
}
public class Parcel
{
//(fun fact, store currency as decimal and not float/double)
public const decimal CostSmall = 5M;
public const decimal CostMedium = 7.5M;
public const decimal CostLarge = 8.5M;
//Uppser limit placed on the weight
public const double PackageWeight = 25;
public double Weight { get; set; }
public double Length { get; set; }
public double Height { get; set; }
public double Breadth { get; set; }
public enum SizeSmall
{
length = 200,
breadth = 300,
height = 150,
}
public enum SizeMedium
{
length = 300,
breadth = 400,
height = 200,
}
public enum SizeLarge
{
length = 400,
breadth = 600,
height = 250
}
}
class Program : ICalculatable<Parcel>
{
static void Main(string[] args)
{
//show welcome message
Console.WriteLine("Kia ora, welcome to the trademe parcel calculator!\n");
//start asking for inputs
//ask for dimensions
Console.WriteLine("Please enter the dimensions of your parcel and the unit (mm, cm or m) when promted:\n");
Console.Write("Enter your dimensions for length: ");
string length = GetInput();
Console.Write("\nEnter your dimensions for breadth: ");
string breadth = GetInput();
Console.Write("\nEnter your dimensions for height: ");
string height = GetInput();
Console.Write("\nEnter the unit used for length, breadth and height (valid inputs: mm or cm or m): ");
string dimenunit = GetUnitDimen();
//ask for weight
Console.WriteLine("\n Please Enter the weight of your parcel and the unit (g, kg) when promted: \n");
Console.Write("Enter your weight: ");
string weight = GetInput();
Console.Write("\nEnter the unit used for used for weight (valid inputs: kg or g): ");
string weightunit = GetUnitWeight();
Parcel myparcel = Converter(length, breadth, height, dimenunit, weight, weightunit);
Program p = new Program();
Console.WriteLine("\n"+p.Calculator(myparcel));
//make sure the console doesnt shutdown prematurely
Console.WriteLine("\nPress Return key (enter) to Exit");
Console.Read();
}
/* Function determines if what package type appropriate for the parcel*/
public string Calculator(Parcel parcel)
{
//check if package meets minimum 25kg limit first
if (parcel.Weight <= Parcel.PackageWeight)
{
//check if package is bigger than largest available first so it doesnt have to check through the other ifs
if (parcel.Length > (double)Parcel.SizeLarge.length ||
parcel.Breadth > (double)Parcel.SizeLarge.breadth || parcel.Height > (double)Parcel.SizeLarge.height)
{
return "Sorry your Parcel is too large for us to ship!" +
"\nOur Largest Package: " + (double)Parcel.SizeLarge.length + "mm x" + (double)Parcel.SizeLarge.breadth + "mm x" + (double)Parcel.SizeLarge.height + "mm" +
"\nYour Dimensions:" + parcel.Length + "mm x" + parcel.Breadth + "mm x" + parcel.Height + "mm";
}
//check if package can fit in smallest first
if (parcel.Length <= (double)Parcel.SizeSmall.length &&
parcel.Breadth <= (double)Parcel.SizeSmall.breadth && parcel.Height <= (double)Parcel.SizeSmall.height)
{
return "Your Parcel can fit in our small package type! The cost would be: " + Parcel.CostSmall.ToString("C");
}
else if (parcel.Length <= (double)Parcel.SizeMedium.length &&
parcel.Breadth <= (double)Parcel.SizeMedium.breadth && parcel.Height <= (double)Parcel.SizeMedium.height)
{
return "Your Parcel can fit in our Medium package type! The cost would be: " + Parcel.CostMedium.ToString("C");
}
else
{
/*we already know the parcel is not greater than the largest availbe and we know that it wont fit in small or medium*/
return "Your Parcel can fit in our Large package type! The cost would be: " + Parcel.CostLarge.ToString("C");
}
}
else //parcel too heavy
{
return "Sorry your Parcel is too heavy for us to ship! Our Limit: " + Parcel.PackageWeight + " Your Parcel Weight: " + parcel.Weight;
}
}
/* Gets dimensions and weight values from the user*/
private static string GetInput()
{
string Value; double result;
do
{
Value = Console.ReadLine();
if (!string.IsNullOrEmpty(Value) && double.TryParse(Value, out result))
{
return Value;
}
else
{
Console.WriteLine("Empty or incorrect input, please try again by entering a number only");
}
} while (string.IsNullOrEmpty(Value) || !double.TryParse(Value, out result));
return Value;
}
/* Gets the unit input for the dimensions (mm, cm, m) */
private static string GetUnitDimen()
{
string Value;
do
{
Value = Console.ReadLine();
if (!string.IsNullOrEmpty(Value) && Value.Equals("m", StringComparison.OrdinalIgnoreCase)
|| Value.Equals("mm", StringComparison.OrdinalIgnoreCase)
|| Value.Equals("cm", StringComparison.OrdinalIgnoreCase))
{
return Value;
}
else
{
Console.WriteLine("Empty or incorrect input, please try again");
}
} while (string.IsNullOrEmpty(Value) && !Value.Equals("m", StringComparison.OrdinalIgnoreCase)
|| !Value.Equals("mm", StringComparison.OrdinalIgnoreCase)
|| !Value.Equals("cm", StringComparison.OrdinalIgnoreCase));
return Value;
}
/* Gets the unit input for the weight (kg, g) */
private static string GetUnitWeight()
{
string Value;
do
{
Value = Console.ReadLine();
if (!string.IsNullOrEmpty(Value) && Value.Equals("kg", StringComparison.OrdinalIgnoreCase)
|| Value.Equals("g", StringComparison.OrdinalIgnoreCase))
{
return Value;
}
else
{
Console.WriteLine("Empty or incorrect input, please try again");
}
} while (string.IsNullOrEmpty(Value) && !Value.Equals("kg", StringComparison.OrdinalIgnoreCase)
|| !Value.Equals("g", StringComparison.OrdinalIgnoreCase));
return Value;
}
/*convert dimensions to mm if it isnt already and convert weight to kg if it isnt already */
private static Parcel Converter(string length, string breadth, string height, string dimenunit, string weight, string weightunit)
{
double result;
double[] parcel = new double[4];
if (double.TryParse(length, out result))//this should always pass as we have sanitized it in getInput Function
{
parcel[0] = result; //length
}
if (double.TryParse(breadth, out result))//this should always pass as we have sanitized it in getInput Function
{
parcel[1] = result; //breadth
}
if (double.TryParse(height, out result))//this should always pass as we have sanitized it in getInput Function
{
parcel[2] = result; //height
}
if (double.TryParse(weight, out result))//this should always pass as we have sanitized it in getInput Function
{
parcel[3] = result; //weight
}
//check units and convert to mm and kg if needed
if (dimenunit.Equals("m", StringComparison.OrdinalIgnoreCase))
{
//convert m to mm for comparision
parcel[0] = parcel[0] * 1000; //length convert to mm
parcel[1] = parcel[1] * 1000; //breadth convert to mm
parcel[2] = parcel[2] * 1000; //height convert to mm
}
else if (dimenunit.Equals("cm", StringComparison.OrdinalIgnoreCase))
{
//convert cm to mm for comparision
parcel[0] = parcel[0] * 10; //length convert to mm
parcel[1] = parcel[1] * 10; //breadth convert to mm
parcel[2] = parcel[2] * 10; //height convert to mm
}
if (weightunit.Equals("g", StringComparison.OrdinalIgnoreCase))
{
//convert gram to kg for comprasion
parcel[3] = parcel[3] / 1000;
}
Parcel p = new Parcel()
{
Length = parcel[0],
Breadth = parcel[1],
Height = parcel[2],
Weight = parcel[3]
};
return p;
}
}
}