heroofhyla

Untitled

May 26th, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. package com.aezart.sous;
  2.  
  3. import java.util.HashMap;
  4.  
  5. public class Quantity {
  6.     public static enum Type{
  7.         EACH,
  8.         VOLUME,
  9.         MASS
  10.     };
  11.    
  12.     private static enum LiquidUnits{
  13.        
  14.         FLOZ(1.0, "fl oz", "fluid ounce", "floz", "fluid ounces"),
  15.         TSP(1/3.0, "tsp", "teaspoon", "teaspoons"),
  16.         TBSP(0.5, "tbsp", "tablespoon", "tablespoons"),
  17.         CUP(8.0, "cup", "cups"),
  18.         GAL(128.0,"gal", "gallon", "gallons");
  19.         double scaleFactor;
  20.         String[] labels;
  21.         LiquidUnits(double scaleFactor, String ... labels){
  22.             this.scaleFactor = scaleFactor;
  23.             this.labels = labels;
  24.         }
  25.     }
  26.     private static enum DryUnits{
  27.         DROZ(1.0, "oz", "ounce", "dry ounce", "ounces", "dry ounces"),
  28.         LB(16.0, "lb", "pound", "pounds");
  29.        
  30.         double scaleFactor;
  31.         String[] labels;
  32.         DryUnits(double scaleFactor, String ... labels){
  33.             this.scaleFactor = scaleFactor;
  34.             this.labels = labels;
  35.         }
  36.  
  37.  
  38.     }
  39.     private static HashMap<String, Double> volCF = new HashMap<>();
  40.     private static HashMap<String, Double> massCF = new HashMap<>();
  41.     private static HashMap<Type, HashMap<String, Double>> scaleMap = new HashMap<>();
  42.    
  43.     static{
  44.         for (LiquidUnits l :LiquidUnits.values()){
  45.             for (String s: l.labels){
  46.                 volCF.put(s, l.scaleFactor);
  47.             }
  48.         }
  49.         for (DryUnits d :DryUnits.values()){
  50.             for (String s: d.labels){
  51.                 massCF.put(s, d.scaleFactor);
  52.             }
  53.         }
  54.        
  55.         scaleMap.put(Type.MASS, massCF);
  56.         scaleMap.put(Type.VOLUME, volCF);
  57.         scaleMap.put(Type.EACH, null);
  58.     }
  59.    
  60.     private Type type;
  61.     private double quantity = 0.0;
  62.    
  63.     public Quantity(double quantity,String typeString){
  64.         int foundCount = 0;
  65.         if (volCF.containsKey(typeString)){
  66.             ++foundCount;
  67.             type = Type.VOLUME;
  68.         }
  69.         if (massCF.containsKey(typeString)){
  70.             ++foundCount;
  71.             type = Type.MASS;
  72.         }
  73.         if (foundCount != 1){
  74.             type = Type.EACH;
  75.         }
  76.        
  77.         this.quantity = quantity;
  78.     }
  79.    
  80.     public Type getType(){
  81.         return type;
  82.     }
  83.    
  84.     public double getQuantity(){
  85.         return quantity;
  86.     }
  87.    
  88.     public void setQuantity(double quantity, String typeString){
  89.         if (scaleMap.get(type) != null){
  90.             if (!isValidUnit(typeString)){
  91.                 throw new IllegalArgumentException("Unit " + typeString + " not recognized.");
  92.             }
  93.             this.quantity = quantity * scaleMap.get(type).get(typeString);
  94.         }else{
  95.             this.quantity = quantity;
  96.         }
  97.     }
  98.    
  99.     public void addQuantity(double quantity, String typeString){
  100.         if (scaleMap.get(type) != null){
  101.             if (!isValidUnit(typeString)){
  102.                 throw new IllegalArgumentException("Unit " + typeString + " not recognized.");
  103.             }
  104.             this.quantity += quantity * scaleMap.get(type).get(typeString);
  105.         }else{
  106.             this.quantity += quantity;
  107.         }
  108.     }
  109.    
  110.     public boolean isValidUnit(String typeString){
  111.         if (scaleMap.get(type) == null){
  112.             return true;
  113.         }
  114.         if (scaleMap.get(type).get(typeString) == null){
  115.             return false;
  116.         }
  117.         return true;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment