Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, i am working on an inventory application.
i am stuck at the unit conversion , like if a product is in Kilogram, i need to sell in grams or tons
, dozen to pcs and custom pack is also there like a carton may contain 20 pcs and need to sell it in pcs or it may contain 40 pcs. i hope you understand my question,

Product table is like

SQL
ProductID    Name      Rate   Unit          Baseunit   conversion
1            Item A    100    carton         Pcs             20  
2            item B    120    carton         Doz             10
3            item C    100    Kilogram       Gram             2 

ex: Item A is in carton of 20 pcs
item C is a bag of 2 kilogram

and there is another table for measuring Unit

SQL
Unit_id      Unit_Name      Parent_ID   Conversion 
1            Pcs              -       1
2            Doz              1       12
3            gram             -       1
4            Kilogram         3       1000


like wise



What I have tried:

i tried but didn't get a complete solution for this, please help

this is what i tried
C#
public enum Units
    {
        //Mass
        //Hectogram = 0,
        Grams = 1,
        KiloGrams = 2,
        Pounds = 3,
        //Ounces = 4,
        //Tonnes = 5,
        //MilliGram = 6,

        //Length
        Metres = 101,
        Millimetres = 102,
        Centimeters = 103,
        Feet = 106,
        Inches = 107,
        
        //Volume
        Litres = 201,
        Millilitres = 202,
        FluidOunces = 204,
        
        //Quantity
        Numbers = 401,
        Dozen = 402,
        Carton = 400
    }

    class UnitConverter
    {
        public static double CtnCount = 0;

        const int MassRange = 0;
        const int LengthRange = 100;
        const int VolumeRange = 200;
        const int Quantity = 400;

        static double[] ToBaseWeight = new double[7];
        static double[] ToBaseLength = new double[8];
        static double[] ToBaseVolume = new double[10];
        static double[] ToBaseQty = new double[7];


        static UnitConverter()
        {
            //ToBaseWeight[(int)Units.MilliGram] = 1 / 100;
            ToBaseWeight[(int)Units.Grams] = 1;
            //ToBaseWeight[(int)Units.Hectogram] = 100;
            ToBaseWeight[(int)Units.KiloGrams] = 1000;
            //ToBaseWeight[(int)Units.Ounces] = 28.3495231; 
            ToBaseWeight[(int)Units.Pounds] = 453.59237;
            //ToBaseWeight[(int)Units.Tonnes] = 1000000;
            ToBaseLength[(int)Units.Metres - LengthRange] = 1;
            ToBaseLength[(int)Units.Millimetres - LengthRange] = 0.001;
            ToBaseLength[(int)Units.Centimeters - LengthRange] = 0.01;
            ToBaseLength[(int)Units.Feet - LengthRange] = 0.3048;
            ToBaseLength[(int)Units.Inches - LengthRange] = 0.0254;
            ToBaseVolume[(int)Units.Litres - VolumeRange] = 1000;
            ToBaseVolume[(int)Units.Millilitres - VolumeRange] = 1;
            ToBaseVolume[(int)Units.FluidOunces - VolumeRange] = 29.5735296;
            ToBaseQty[(int)Units.Numbers - Quantity] = 1;
            ToBaseQty[(int)Units.Dozen - Quantity] = 12;
            ToBaseQty[(int)Units.Carton - Quantity] = 100;
        }


        public static string ConvertUnits(double amount, Units from, Units to,double cnt)
        {
            CtnCount = cnt;
            if (from == to) return amount.ToString();

            string msg = ValidateUnitMatch(from, to);
            if (msg != "")
            {
                return msg;
            }

            int range = GetConversionRange(from);

            double[] rateArray = GetConversionArray(range);

            double answer = 0;
            //Convert From to base,
            if (CtnCount>0)
            {
                rateArray[0] = CtnCount;
            }
            
            answer = rateArray[(int)from - range] * amount;

            //Convert base to To
            answer = answer * (1.0 / rateArray[(int)to - range]);
            return answer.ToString();
        }

        static string ValidateUnitMatch(Units from, Units to)
        {

            int frange = ((((int)from) / 100) * 100);
            int trange = ((((int)to) / 100) * 100);

            if (frange != trange)
            {
                return "The from unit and to unit are not compatible. They need to be of the same type (Volume/Area/Mass/Length)";

            }
            else
            {
                return "";
            }
        }
        static int GetConversionRange(Units unit)
        {
            int range = ((((int)unit) / 100) * 100);
            return range;
        }

        static double[] GetConversionArray(int range)
        {
            switch (range)
            {
                case VolumeRange: return ToBaseVolume;
                case LengthRange: return ToBaseLength;
                case MassRange: return ToBaseWeight;
                case Quantity: return ToBaseQty;
            }
            throw new ArgumentOutOfRangeException("Unknown unit type");
        }
    }


its working but i was not able to use custom unit.. like i said above
if i want to add a new unit like a bag contains 10KG item i cannot do it in this.
Posted
Updated 7-Jul-20 3:09am
v2

You don't have any idea for an approach ...?
Possibli you could do this :
- your base is an object - a class
- this class has the Properties as seen in your table-desciption
- the Type of those Properties could be Enum's
- depending on the Selection with your Properties (of the Object) you could do the Conversion directly inside this Object and give the possible Result as an additional Property.

Give it a try ...
 
Share this answer
 
Comments
[no name] 7-Jul-20 11:21am    
Yes a 5. And more, 'every' unit can be expressed by the 'mks' system ;)
Ralf Meier 7-Jul-20 11:26am    
Thank you for ypur vote ... ;-)
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

And this ... this is trivial homework: you already know how to multiply too numbers together, so this should be extremely simple for you. All you need to do is use a JOIN to combine the tables using the BaseUnit / Unit and the Unit_Name.

Read your course notes for the last lecture, and give it a try - this isn't complicated.
 
Share this answer
 
Comments
Lisanas 7-Jul-20 6:06am    
please check the above code that i tried.. i know how to multiply 2 numbers.. may be the way i ask the question is wrong.. i should add the code i tried..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900