Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Base Numbers #207

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ private int getConversionFromDrawer(int itemId) {

case R.id.drawer_volume:
return Conversion.VOLUME;

case R.id.base_numbers:
return Conversion.BASE;
}

return Conversion.AREA;
Expand Down Expand Up @@ -224,6 +227,8 @@ private int getMenuPositionOfConversion(@Conversion.id final int conversion) {
return 13;
case Conversion.VOLUME:
return 14;
case R.id.base_numbers:
return 15;
default:
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ private void convert() {
double value = isNumeric(input) ? Double.parseDouble(input) : 0;

switch (mConversionId) {
case Conversion.BASE:
mPresenter.convertBaseNumber(value, getCheckedUnit(mGrpFrom), getCheckedUnit(mGrpTo));
break;

case Conversion.TEMPERATURE:
mPresenter.convertTemperatureValue(value, getCheckedUnit(mGrpFrom), getCheckedUnit(mGrpTo));
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.physphil.android.unitconverterultimate.models;

public class BaseNumberUnit extends Unit{

/**
* Create a unit object
*
* @param id id of the unit
* @param labelResource string resource id of the label
*/
public BaseNumberUnit(int id, int labelResource) {

//Need custom function to convert
super(id, labelResource, 0.0, 0.0);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ public final class Conversion {
public static final int TIME = 11;
public static final int TORQUE = 12;
public static final int VOLUME = 13;
public static final int BASE = 15;

private int id;
private int labelResource;
private List<Unit> units;

@IntDef({AREA, COOKING, CURRENCY, STORAGE, ENERGY, FUEL, LENGTH, MASS, POWER, PRESSURE, SPEED,
TEMPERATURE, TIME, TORQUE, VOLUME})
TEMPERATURE, TIME, TORQUE, VOLUME, BASE})
public @interface id {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ public class Unit {
public static final int CUBIC_FOOT = 1218;
public static final int CUBIC_YARD = 1219;

public static final int BINARY = 1501;
public static final int DECIMAL = 1502;
public static final int OCTAL = 1503;
public static final int HEXADECIMAL = 1504;


@IntDef({SQ_KILOMETRES, SQ_METRES, SQ_CENTIMETRES, HECTARE, SQ_MILE, SQ_YARD, SQ_FOOT, SQ_INCH, ACRE,
AUD, BGN, BRL, CDN, CHF, CNY, CZK, DKK, EUR, GBP, HKD, HRK, HUF, IDR, ILS, INR, ISK, JPY, KRW, MXN, MYR, NOK, NZD, PHP, PLN, RON, RUB, SEK, SGD, THB, LIRA, USD, ZAR,
BIT, BYTE, KILOBIT, KILOBYTE, MEGABIT, MEGABYTE, GIGABIT, GIGABYTE, TERABIT, TERABYTE,
Expand All @@ -204,7 +210,8 @@ public class Unit {
YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND, MILLISECOND, NANOSECOND,
N_M,
TEASPOON, TABLESPOON, CUP, FLUID_OUNCE, QUART, PINT, GALLON, BARREL, FLUID_OUNCE_UK, QUART_UK, PINT_UK,
GALLON_UK, BARREL_UK, MILLILITRE, LITRE, CUBIC_CM, CUBIC_M, CUBIC_INCH, CUBIC_FOOT, CUBIC_YARD})
GALLON_UK, BARREL_UK, MILLILITRE, LITRE, CUBIC_CM, CUBIC_M, CUBIC_INCH, CUBIC_FOOT, CUBIC_YARD,
BINARY, DECIMAL, OCTAL, HEXADECIMAL})
public @interface id {}

private int id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,152 @@ public void onGetUnitsToDisplay(@Conversion.id int conversionId) {
}
}


/**
* Convert a number from a base to another
*
* @param value the value to convert
* @param from the base to be converted from
* @param to the base to be converted to
*/

public void convertBaseNumber(double value, Unit from, Unit to){
double result = value;
if(from.getId() == Unit.BINARY) {
if(!isBinary((int) value)) {
mView.showToast(R.string.toast_error_not_binary);
return;
}
}

if(from.getId() == Unit.OCTAL){
if(!isOctal((int) value)) {
mView.showToast(R.string.toast_error_not_octal);
return;
}
}

if(from.getId() == Unit.HEXADECIMAL){
mView.showToast(R.string.toast_error_hex_coming_soon);
}

if (from.getId() != to.getId()) {
switch (to.getId()) {
case (Unit.BINARY):
result = toBinary(from.getId(), value);
break;

case(Unit.OCTAL):
result = toOctal(from.getId(), value);
break;

case(Unit.DECIMAL):
result = toDecimal(from.getId(), value);
break;

}
}
mView.showResult(result);
}

//Handle conversions to Binary
private double toBinary(int fromId, double value){
double result = value;
double temp;
switch (fromId){
case(Unit.DECIMAL):
result = Double.parseDouble(Integer.toBinaryString((int) value));
break;

case(Unit.OCTAL):
temp = octToDecimal((int) value);
result = Double.parseDouble(Integer.toBinaryString((int) temp));
break;
}
return result;
}

//Handle conversions to Decimal
private double toDecimal(int fromId, double value){
double result = value;
String valueString = String.valueOf((int)value);
switch (fromId){
case(Unit.BINARY):
result = Integer.parseInt(valueString, 2);
break;

case(Unit.OCTAL):
result = Integer.parseInt(valueString, 8);
break;
}
return result;

}

//Handle conversions to Hex
private double toOctal(int fromId, double value){
double result = value;
switch (fromId){
case(Unit.DECIMAL):
result = Double.parseDouble(Integer.toOctalString((int) value));
break;

case(Unit.BINARY):
double temp = binToDecimal((int) value);
result = Double.parseDouble(Integer.toOctalString((int) temp));
break;
}
return result;
}

//Convert from Binary to Decimal
private double binToDecimal(int binaryNum){
String valueString = String.valueOf(binaryNum);

if(isBinary(binaryNum)){
double result = Integer.parseInt(valueString, 2);
return result;
}
else{
mView.showToast(R.string.toast_error_not_binary);
return 0;
}
}

//Convert from Octal to Decimal
private double octToDecimal(int octalNum){
String valueString = String.valueOf(octalNum);

if(isOctal(octalNum)){
double result = Integer.parseInt(valueString, 8);
return result;
}
else{
mView.showToast(R.string.toast_error_not_octal);
return 0;
}
}

//Check if given number is Octal
private static boolean isOctal(int num){
return String.valueOf(num).matches("^[1-7][0-7]*$");
}

//Check if given number is Binary base
private static boolean isBinary(int num) {
if (num == 0 || num < 0) {
return false;
}
while (num != 0) {
if (num % 10 > 1) {
return false;
}
num = num / 10;
}
return true;
}


/**
* Convert a temperature value from one unit to another
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.physphil.android.unitconverterultimate.R;
import com.physphil.android.unitconverterultimate.api.models.Country;
import com.physphil.android.unitconverterultimate.api.models.Currencies;
import com.physphil.android.unitconverterultimate.models.BaseNumberUnit;
import com.physphil.android.unitconverterultimate.models.Conversion;
import com.physphil.android.unitconverterultimate.models.TemperatureUnit;
import com.physphil.android.unitconverterultimate.models.Unit;
Expand Down Expand Up @@ -71,6 +72,7 @@ private Conversions() {
getTimeConversions();
getTorqueConversions();
getVolumeConversions();
getBaseConversions();
mCurrencyUpdated = false;
}

Expand Down Expand Up @@ -362,6 +364,19 @@ private void getVolumeConversions() {
addConversion(Conversion.VOLUME, new Conversion(Conversion.VOLUME, R.string.volume, units));
}

private void getBaseConversions() {
List<Unit> units = new ArrayList<>();

units.add(new BaseNumberUnit(BINARY, R.string.binary));
units.add(new BaseNumberUnit(OCTAL, R.string.octal));
units.add(new BaseNumberUnit(DECIMAL, R.string.decimal));
units.add(new BaseNumberUnit(HEXADECIMAL, R.string.hexadecimal));

addConversion(Conversion.BASE, new Conversion(Conversion.BASE, R.string.baseNum, units));
}



public boolean hasCurrency() {
return mConversions.get(Conversion.CURRENCY).getUnits().size() > 0;
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/menu/menu_navigation_drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@
android:icon="@drawable/ic_volume"
android:title="@string/volume"/>

<item
android:id="@+id/base_numbers"
android:icon="@drawable/ic_volume"
android:title="Base Numbers"/>

</group>

<item
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<string name="toast_copied_clipboard">Text copied to clipboard</string>
<string name="toast_currency_updated">Currency conversions updated</string>
<string name="toast_error_updating_currency">There was an error updating the currency conversions.</string>
<string name="toast_error_not_binary">Given number is not binary </string>
<string name="toast_error_not_octal">Given number is not octal</string>
<string name="toast_error_hex_coming_soon">Hexadecimal coming soon!</string>

<string name="prefs_title_formatting">Formatting</string>
<string name="prefs_summary_number_decimals">Set the number of decimal places to be shown in converted value</string>
Expand Down Expand Up @@ -105,6 +108,13 @@
<string name="time">Time</string>
<string name="torque">Torque</string>
<string name="volume">Volume</string>
<string name="baseNum">Base Numbers</string>

<!-- BaseNumbers -->
<string name="binary">Binary</string>
<string name="decimal">Decimal</string>
<string name="octal">Octal</string>
<string name="hexadecimal">Hexadecimal</string>

<!-- Area -->
<string name="sq_kilometre">Sq Kilometre</string>
Expand Down Expand Up @@ -285,6 +295,7 @@
<string name="usd">American Dollar</string>
<string name="zar">South African Rand</string>


<!-- All strings below this line require translation to at least one of the supported languages -->
<!-- Once translated, please move above this line in the corresponding `strings.xml` file -->
<string name="title_activity_acknowledgements">Acknowledgements</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,27 @@ public void testVolume()
mPresenter.convert(5.5, volume.getUnitById(CUBIC_YARD), volume.getUnitById(TEASPOON));
verify(view, atLeastOnce()).showResult(AdditionalMatchers.eq(853138.298312504, DELTA_9));
}

@Test
public void testBinary()
{
Conversion basenumbers = mConversions.getById(Conversion.BASE);

mPresenter.convertBaseNumber(11.0, basenumbers.getUnitById(BINARY), basenumbers.getUnitById(DECIMAL));
verify(view, atLeastOnce()).showResult(eq(3));

mPresenter.convertBaseNumber(11.0, basenumbers.getUnitById(BINARY), basenumbers.getUnitById(OCTAL));
verify(view, atLeastOnce()).showResult(eq(5));

mPresenter.convertBaseNumber(6, basenumbers.getUnitById(DECIMAL), basenumbers.getUnitById(BINARY));
verify(view, atLeastOnce()).showResult(eq(110));

mPresenter.convertBaseNumber(15, basenumbers.getUnitById(DECIMAL), basenumbers.getUnitById(BINARY));
verify(view, atLeastOnce()).showResult(eq(17));

mPresenter.convertBaseNumber(12, basenumbers.getUnitById(OCTAL), basenumbers.getUnitById(DECIMAL));
verify(view, atLeastOnce()).showResult(eq(10));


}
}