Results 1 to 11 of 11

Thread: Taxi fares

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    4

    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

  2. #2
    Member
    Join Date
    Mar 2005
    Posts
    43

    Re: Taxi fares

    by writing the code, do you mean writing the basic steps on paper, or writing a working program in C# ?

  3. #3
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    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....
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  4. #4
    Junior Member
    Join Date
    Feb 2005
    Posts
    31

    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    4

    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

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    New Member
    Join Date
    Apr 2006
    Posts
    1

    Re: Taxi fares

    It would be interesting to see what they consider bad design in this instance.

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9
    New Member
    Join Date
    May 2006
    Posts
    1

    Re: Taxi fares

    can anybody post the C# solution of this problem?!

  10. #10
    New Member
    Join Date
    Sep 2012
    Posts
    1

    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;
    }
    }
    }

  11. #11
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Taxi fares

    Did you get the job ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width