Android Open Source - SmartCocktailShaker Prepare Drink Model






From Project

Back to project page SmartCocktailShaker.

License

The source code is released under:

MIT License

If you think the Android project SmartCocktailShaker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.tonydicola.smartshaker;
//from   w  ww.j a  va 2  s .  c o  m
import com.tonydicola.smartshaker.interfaces.Drink;
import com.tonydicola.smartshaker.interfaces.PreparationStep;

public class PrepareDrinkModel {

    private Drink drink;
    private int currentStep;
    private double measureGrams;
    private Double tareGrams;

    public PrepareDrinkModel(Drink drink) {
        this.drink = drink;
        startOver();
    }

    public void nextStep() {
        // Advance to the next step if not at the end, and reset all the appropriate state.
        if (currentStep < (drink.getPreparation().size() - 1)) {
            currentStep += 1;
            tareGrams = null;
            measureGrams = 0;
        }
    }

    public void startOver() {
        currentStep = 0;
        measureGrams = 0;
        tareGrams = null;
    }

    public int getCurrentStep() {
        return currentStep;
    }

    public boolean isCurrentStep(int number) {
        return currentStep == number;
    }

    public boolean stepHasMeasurement(int number) {
        // Return true or false if the requested step has a measurement or not.
        PreparationStep step = drink.getPreparation().get(number);
        return (step.getMeasureGrams() != null && step.getGramsPerOz() != null);
    }

    public double getMeasureGrams() {
        return measureGrams;
    }

    public double getMeasureOz() {
        // Convert current measure from weight to volume using the density of the liquid in the current step.
        Double density = drink.getPreparation().get(currentStep).getGramsPerOz();
        if (density != null) {
            return measureGrams / density;
        }
        else {
            return 0;
        }
    }

    public int getMeasureProgress() {
        // Return a value from 0 to 100 based on what percent of the current step's measure is applied to the scale.
        Double total = drink.getPreparation().get(currentStep).getMeasureGrams();
        if (total != null) {
            return (int)(measureGrams / total * 100.0);
        }
        else {
            return 0;
        }
    }

    public void updateMeasure(double rawGrams) {
        // Don't make any updates if the current step has no measure defined.
        if (!stepHasMeasurement(currentStep)) return;
        // Use the first measurement to tare future measures.
        if (tareGrams == null) {
            tareGrams = rawGrams;
        }
        // Else set the current measurement to the measured value minus the tare value.
        else {
            measureGrams = rawGrams - tareGrams;
            measureGrams = measureGrams < 0.0 ? 0.0 : measureGrams;
        }
    }
}




Java Source Code List

com.hoho.android.usbserial.driver.CdcAcmSerialDriver.java
com.hoho.android.usbserial.driver.CommonUsbSerialDriver.java
com.hoho.android.usbserial.driver.Cp2102SerialDriver.java
com.hoho.android.usbserial.driver.FtdiSerialDriver.java
com.hoho.android.usbserial.driver.ProlificSerialDriver.java
com.hoho.android.usbserial.driver.UsbId.java
com.hoho.android.usbserial.driver.UsbSerialDriver.java
com.hoho.android.usbserial.driver.UsbSerialProber.java
com.hoho.android.usbserial.driver.UsbSerialRuntimeException.java
com.hoho.android.usbserial.util.HexDump.java
com.hoho.android.usbserial.util.SerialInputOutputManager.java
com.hoho.android.usbserial.util.UsbSerialInputStream.java
com.hoho.android.usbserial.util.UsbSerialOutputStream.java
com.tonydicola.smartshaker.BluetoothSppProvider.java
com.tonydicola.smartshaker.JsonDrinkProvider.java
com.tonydicola.smartshaker.MockConnectionProvider.java
com.tonydicola.smartshaker.PrepareDrinkModel.java
com.tonydicola.smartshaker.StepListAdapter.java
com.tonydicola.smartshaker.UsbSerialProvider.java
com.tonydicola.smartshaker.activities.ChooseConnection.java
com.tonydicola.smartshaker.activities.DrinkDetails.java
com.tonydicola.smartshaker.activities.DrinkList.java
com.tonydicola.smartshaker.activities.PrepareDrink.java
com.tonydicola.smartshaker.factories.ConnectionFactory.java
com.tonydicola.smartshaker.factories.DrinkFactory.java
com.tonydicola.smartshaker.interfaces.ConnectionProvider.java
com.tonydicola.smartshaker.interfaces.DeviceConnection.java
com.tonydicola.smartshaker.interfaces.DrinkProvider.java
com.tonydicola.smartshaker.interfaces.Drink.java
com.tonydicola.smartshaker.interfaces.PreparationStep.java