|
-
Apr 14th, 2005, 09:29 PM
#1
Thread Starter
New Member
Taxi fares
I had the following question on job interview. It took 1 hour for me to write the code for this program, but interviewer said that my design was not good. How would you do design for this problem? Any tips will be appreciated.
Background
====
Taxi fares in New York City are regulated by the Taxi & Limousine Commission (TLC) and calculated by meter. The rate of fare is as follows:
- The sum of $2.50 is payable upon entry.
- $0.40 is paid for every additional unit.
- A unit fare is one-fifth of a mile when the taxicab is traveling 6 mph or more; or
- 120 seconds (at a rate of 20 cents per minute), when the taxicab is not in motion or is traveling at less than 6 mph.
There is an additional night surcharge of $0.50 after 8:00 pm and before 6:00 am. There is also a peak weekday surcharge of $1.00 after 4:00 pm and before 8:00 pm.
There are 20 New York City blocks to the mile.
Problem Definition
====
You will be provided with an array of integers representing the number of blocks the cab travels a passenger’s ride. You will also be provided with a DateTime value indicating the exact moment in time when the taxi ride is considered to have begun.
You are to calculate the total fares in dollars and cents.
Return the total fare as a normally formatted USD string.
You are to expose your calculation functionality as a public instance method named Calculate. The method’s ordered parameters should be as follows:
DateTime rideStart, int[] blocksTraveled
-
Apr 15th, 2005, 01:24 AM
#2
Member
Re: Taxi fares
by writing the code, do you mean writing the basic steps on paper, or writing a working program in C# ?
-
Apr 15th, 2005, 09:21 AM
#3
Frenzied Member
Re: Taxi fares
LOL....You must really have messed up. Got a lot of Q's like that the last week.
At one company I calculated that the entire population in Beijing is gonna die this year (calc the number of baby girls born in Beijing in a day)....and I got the job!
At MS I did not know how to describe the difference between a primary and foreign key (I'm using it, not a bloody teacher)....got the job too....
-
Apr 15th, 2005, 11:16 AM
#4
Junior Member
Re: Taxi fares
Your design should demonstrate a clear understand of hidden and exposed code. I would suggest making a "fare" object, which you would declare as such:
Code:
Fare newFare = new Fare(blocksTraveled, rideStart);
this would initialize a new object that would run the start time through a series of conditionals, which would end in a final price. The price would be exposed by a function that returned Curency, called Calculate.
Dan
-
Apr 16th, 2005, 09:51 AM
#5
Thread Starter
New Member
Re: Taxi fares
I did the following way:
Fare newFare = new Fare();
newFare.Calculate (DateTime rideStart, int[] blocksTraveled) ;
I just wondering how to implement Calculate function? As an abstract, interface or overriding? How would you do it? They asked me to write a working program in C#.
Last edited by kevgueni; Apr 16th, 2005 at 09:57 AM.
Reason: adding some staff
-
Apr 16th, 2005, 10:15 AM
#6
Re: Taxi fares
Please don't double post - it doesn't do you any good or future searches on the forum.
Answers are coming up in your other post - they should stop here!
http://www.vb-forum.com/showthread.php?t=334185
-
Apr 26th, 2006, 06:08 PM
#7
New Member
Re: Taxi fares
It would be interesting to see what they consider bad design in this instance.
-
Apr 26th, 2006, 07:13 PM
#8
Re: Taxi fares
Welcome to the forum!
Generally posting in a year old thread is not a good practice
Especially when the thread was a double-post that didn't really belong.
At any rate - if you have a question or issue needing discussion please post a new thread and I'm sure that we will all jump in and give our opinions
-
May 11th, 2006, 04:56 PM
#9
New Member
Re: Taxi fares
can anybody post the C# solution of this problem?!
-
Sep 26th, 2012, 10:35 AM
#10
New Member
Re: Taxi fares
Its been years since this Q was posted but I got asked the same Q yesterday and I could not find an answer online anywhere. So here is what I wrote.. its not solving the problem exactly bc I am not calculating speed.. but some code to go by.. its in c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Args[0] will have RideDate. "9/25/2012 20:00:00"
//Args[1] will have Blocks as comma delimited string. eg. "20,30,40,50"
try
{
if (args.Length == 2)
{
Console.WriteLine("Begin Fare Calculation ");
DateTime ridestart = Convert.ToDateTime(args[0]);//Assigining RideStart Datetime
List<int> blocksTravelled = new List<int>(Array.ConvertAll<string, int>(args[1].Split(','), Convert.ToInt32));//Converting Sting to list of Integers.
//Calculate Fare for given rides.
Fare.Calculate(ridestart, blocksTravelled);
}
else
{
Console.WriteLine("Please provide Start Datetime and Blocks.");
}
}
catch {
//Catch exception and deal with it.
Console.WriteLine("Error!!");
}
}
}
class Fare
{
const double EntryCharge = 2.50;//Can be stored in app.config file so it can be easily changed.Applies to all these costs.
const double AdditionalUnit = .40;
const double NgtlySurcharge = .50;
const double WkDaySurcharge = 1;
//Method to Calculate Fare.
public static void Calculate(DateTime rideStart, List<int> blocksTravelled)
{
try
{
foreach (int blocks in blocksTravelled)
{
double FairValue = 0;
FairValue += EntryCharge + GetUnitCharge(blocks);
//Add WeekDay Surcharge
if (WeekDaySurcharge(rideStart) == true) FairValue += WkDaySurcharge;
//Add Nightly Surcharge
if (NightlySurcharge(rideStart) == true) FairValue += NgtlySurcharge;
//Console.WriteLine("Total Fare:" + FairValue.ToString("C"));
Console.WriteLine("For Ride Starting at:" + rideStart.ToString() + "; Total blocks traveled: " + blocks.ToString() + "; Total Fare = " + string.Format("{0:c}", FairValue));
}
}
catch
{
Console.WriteLine("Error occured in Calculate().");
}
}
//Find out whether to apply weekday surcharge.
public static Boolean WeekDaySurcharge(DateTime rideStart)
{
Boolean Surcharge = false;
try
{
if (rideStart.DayOfWeek == DayOfWeek.Monday || rideStart.DayOfWeek == DayOfWeek.Tuesday || rideStart.DayOfWeek == DayOfWeek.Wednesday || rideStart.DayOfWeek == DayOfWeek.Thursday || rideStart.DayOfWeek == DayOfWeek.Friday)
{
if (rideStart.Hour >= 16 && rideStart.Hour <= 20) { Surcharge = true; }
}
}
catch
{
Console.WriteLine("Error occured in WeekDaySurcharge().");
}
return Surcharge;
}
//Find out whether to apply Night Surcharge.
public static Boolean NightlySurcharge(DateTime rideStart)
{
Boolean Surcharge = false;
try
{
if (rideStart.Hour >= 20 && rideStart.Hour <= 6)
{ Surcharge = true; }
}
catch
{
Console.WriteLine("Error occured in NightlySurcharge().");
}
return Surcharge;
}
//Get Additional Unit Cost.
private static double GetUnitCharge(int blocks)
{
//20 blocks == 1 mile,
//1/5 mile == 1 unit
//cant calculate speed without an ending time.
double miles = (blocks / 20); //Can be used if we know end-time to calculate Avg. Speed.
double unts = 0;
unts = (blocks / 4) * AdditionalUnit;
return unts;
}
}
}
-
Sep 26th, 2012, 10:52 PM
#11
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|