Results 1 to 2 of 2
  1. #1
    bunny is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default urgent requirement

    I have class Person

    public class Person{
    private String firstName;
    private String lastName;
    private String type;
    ................
    ................
    }

    type variable can contain employee,self employee,businessman,farmer etc.
    Now my requirement is to calculate tax based on type variable.

    How can I do this without using clumsy if conditions.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    what if you created an enum, that would be a switch statement then.

    or perhaps define a type object base class where it defines abstract methods for getTaxPercent(), which child classes of this would implement to return their specific percentage, where the code then becomes

    TypeObj type;
    .......
    typeobj.getTaxPercentage();

    }

    and there is no switch or if..else block there at all.


    but you can also combine this with enum, have custom enum property. usually tax rates change from time to time, probalby better to not model a tax rate inside an enum field anyway.

    I would also say, use a database, where you can model

    employee_type,
    tax_rate,
    effective_from,
    effective_to

    This gives you the ability to have a point in time rate. consider computing a tax rate for an employee for today. then next month the tax rate changes. if you had to recompute the report for this month in the next month, we would want to have the result be consistent with the point in time the first report was ran.

    so for example, we would query this table with JDBC , maybe something like :

    select tax_rate from rate_table where employee_type = ? and effective_from <= ? and effective_to >= ?

Similar Threads

  1. Replies: 0
    Last Post: 06-15-2010, 05:29 PM
  2. Replies: 0
    Last Post: 06-15-2010, 05:22 PM
  3. urgent requirement...regarding process
    By LakshmiSireesha in forum New To Java
    Replies: 2
    Last Post: 01-15-2009, 03:55 PM
  4. Urgent Requirement for Java/J2ee
    By anushareddy7480 in forum Java Software
    Replies: 1
    Last Post: 07-23-2007, 01:35 PM
  5. Urgent Requirement for Java/J2ee
    By anushareddy7480 in forum New To Java
    Replies: 0
    Last Post: 07-23-2007, 11:56 AM

Posting Permissions

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