Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 852195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:33:17+00:00 2026-05-15T07:33:17+00:00

Here is a small currency converter piece of code: public enum CurrencyType { DOLLAR(1),

  • 0

Here is a small currency converter piece of code:

 public enum CurrencyType {

        DOLLAR(1),
        POUND(1.2),
        RUPEE(.25);

        private CurrencyType(double factor) {
            this.factor = factor;
        }
        private double factor; 

        public double getFactor() {
            return factor;
        }

    }

    public class Currency {

        public Currency(double value, CurrencyType type) {
            this.value = value;
            this.type = type;
        }

        private CurrencyType type;
        private double value;

        public CurrencyType getCurrencyType() {
            return type;
        }

        public double getCurrencyValue() {
            return value;
        }

        public void setCurrenctyValue(double value){
            this.value = value;
        }

    }

public class CurrencyConversion {


    public static Currency convert(Currency c1, Currency c2)
            throws Exception {
        if (c1 != null && c2 != null) {
            c2.setCurrenctyValue(c1.getCurrencyValue()
                    * c1.getCurrencyType().getFactor()
                    * c2.getCurrencyType().getFactor());
            return c2;
        } else
            throw new Exception();
    }


}

I would like to improve this code to make it work for different units of conversion, for example: kgs to pounds, miles to kms, etc etc. Something that looks like this:

public class ConversionManager<T extends Convertible> {


    public T convert(T c1, T c2) 
    {
        //return null;
    }
}

Appreciate your ideas and suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:33:18+00:00Added an answer on May 15, 2026 at 7:33 am

    This is the object-oriented way to do it in my opinion. Note that you could templatize on the data type as well but I’ve just used doubles here since they can represent any scalar units. This provides a nice way of handling conversion between units of different metrics as you asked: distance, currency, mass, etc.

    public interface Metric {
        public String getReferenceUnit();
    }
    
    public class Distance implements Metric {
        public String getReferenceUnit() {
            return new String("Meters");
        }
    }
    
    public interface Units<T extends Metric> {
        public double getToReferenceFactor();
    }
    
    public class Kilometers implements Units<Distance> {
        public double getToReferenceFactor() {
            return 1000;
        }
    }
    
    static <T extends Metric> double convert(Units<T> from, Units<T> to) {
        return from.getToReferenceFactor() * (1.0 / to.getToReferenceFactor());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small problem with interfaces. Here it is in Pseudo code :
Ease of installation/use is the most important factor here - not performance. Small is
Here's a small sample of some test code that simply goes to http://www.un.org/apps/news/story.asp?NewsID=37180&Cr=Haiti&Cr1= and
Just a small SVN problem here. I setup my own SVN server Setting up
Here is the situation. This small company I'm working with wants to have a
Here is my code, which takes two version identifiers in the form 1, 5,
Here is my small program, import pygame pygame.init() Here is my compilation command. python
Here is a small example with the masked input plugin : //apply the mask
I've run into a small problem here. I wrote the Tortoise and Hare cycle
I've got a little stuck with a small problem here: I use serialport communications

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.