Android : Reviewing and correcting code style

on Thursday, March 19, 2015


I am new to android developement.

Created calculator application.

Want to improve my code quality.

Will be helpfull for any advice to make the code better and more professional

(overall structure, splitting onto logical units, accordance to code conventions, methods and variables names, comments etc).

Here is my code:



public class OperationsAndFunctions {

static enum OperEnum {
SUM, SUBSTRACT, MULTIPLY, DIVIDE, EQUAL
}

static enum FuncEnum {
RESET, BACKSPACE, SIGN, PERCENT, POINT
}
}

public class LCalculator extends Activity {

private TextView mDisplay;
private boolean mDisplayIsEmpty = true;
private boolean mFirstNumberReceived;
private boolean mNumberEntered;
private boolean mPointEntered;

private double sNum1;
private double sNum2;

private static OperationsAndFunctions.OperEnum sOperation;
private static OperationsAndFunctions.FuncEnum sFunction;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);

mDisplay = (TextView) findViewById(R.id.tvDisplay);
mDisplay.setText("0");
}

// listening for number buttons pressing
public void num_Clicked(View view) {

Button button = (Button) view;
int number = 0;

// checking button ID
switch (button.getId()) {

case R.id.button1:
number = 1;
break;

case R.id.button2:
number = 2;
break;

case R.id.button3:
number = 3;
break;

case R.id.button4:
number = 4;
break;

case R.id.button5:
number = 5;
break;

case R.id.button6:
number = 6;
break;

case R.id.button7:
number = 7;
break;

case R.id.button8:
number = 8;
break;

case R.id.button9:
number = 9;
break;

case R.id.button0:
if (mDisplay.getText().toString().equals("0")) {
return;
}
number = 0;
break;
}

// setting a new digit onto display if it is empty
if (mDisplayIsEmpty) {

if (!(number == 0)) {
mDisplayIsEmpty = false;
}
mDisplay.setText(Integer.toString(number));
}

// appending digit if display isn't empty
else {
mDisplay.append(Integer.toString(number));
}
mNumberEntered = true;
}

// listenning for operational buttons pressing
public void op_Clicked(View view) {
Button button = (Button) view;

// calculating and printing result
// if a number has been entered before
// (Not entering here if another operation has been pressed before)
if (mNumberEntered) {
// if received first number and saved to sNum1
if (mFirstNumberReceived) {
sNum2 = Double.parseDouble(mDisplay.getText().toString());
printResult(calculateResult(sNum1, sOperation, sNum2));
sNum1 = Double.parseDouble(mDisplay.getText().toString());
}

// if not received first number - receiving it now
else {
sNum1 = Double.parseDouble(mDisplay.getText().toString());
mFirstNumberReceived = true;
}
mNumberEntered = false;
}

// checking operational buttons id
// and assigning appropriate operations enum value
switch (button.getId()) {

case R.id.buttonPlus:
sOperation = OperationsAndFunctions.OperEnum.SUM;
break;

case R.id.buttonMinus:
sOperation = OperationsAndFunctions.OperEnum.SUBSTRACT;
break;

case R.id.buttonMultiply:
sOperation = OperationsAndFunctions.OperEnum.MULTIPLY;
break;

case R.id.buttonDivide:
sOperation = OperationsAndFunctions.OperEnum.DIVIDE;
break;

case R.id.buttonEqual:
sOperation = OperationsAndFunctions.OperEnum.EQUAL;
break;
}

// next digit entered will be a new number
mDisplayIsEmpty = true;
mPointEntered = false;
}

// listening for functional buttons pressing
public void func_Clicked(View view) {
Button button = (Button) view;

// checking operational buttons id
// and assigning appropriate functions enum value
switch (button.getId()) {

case R.id.buttonAc:
sFunction = OperationsAndFunctions.FuncEnum.RESET;
break;

case R.id.buttonC:
sFunction = OperationsAndFunctions.FuncEnum.BACKSPACE;
break;

case R.id.buttonSign:
sFunction = OperationsAndFunctions.FuncEnum.SIGN;
break;

case R.id.buttonPercent:
sFunction = OperationsAndFunctions.FuncEnum.PERCENT;
break;

case R.id.buttonPoint:
sFunction = OperationsAndFunctions.FuncEnum.POINT;
break;
}

// implementing chosen function
implementFunction(sFunction);
}

// implementing function
private void implementFunction(OperationsAndFunctions.FuncEnum function) {
sFunction = function;

switch (function) {

case RESET:
reset();
break;

case BACKSPACE:
printResult(Double.parseDouble(backSpace(mDisplay.getText())
.toString()));
break;

case SIGN:
printResult(readDisplayState() * (-1));
break;

case PERCENT:
printResult(readDisplayState() / 100);
break;

case POINT:
if (!mPointEntered) {
if (!mDisplayIsEmpty) {
mDisplay.append(".");
} else {
mDisplay.setText("0.");
mDisplayIsEmpty = false;
}
mPointEntered = true;
}
break;
}
}

// reseting the calculator
private void reset() {
mDisplay.setText("0");
sNum1 = sNum2 = 0;
mDisplayIsEmpty = true;
mFirstNumberReceived = false;
mNumberEntered = false;
mPointEntered = false;
}

// deleting the last symbol on display
private CharSequence backSpace(CharSequence displayState) {
CharSequence result = displayState;

if (displayState.length() > 1) {
result = displayState.subSequence(0, displayState.length() - 1);
}

else if (displayState.length() == 1) {
result = "0";
}

return result;
}

// calculating the result
private double calculateResult(double num1,
OperationsAndFunctions.OperEnum operation, double num2) {

sNum1 = num1;
sNum2 = num2;
sOperation = operation;
double result = 0;

switch (operation) {
case SUM:
result = num1 + num2;
break;

case SUBSTRACT:
result = num1 - num2;
break;

case MULTIPLY:
result = num1 * num2;
break;

case EQUAL:
mDisplayIsEmpty = true;
mNumberEntered = false;
mFirstNumberReceived = false;
break;

case DIVIDE:
// division by zero
if (num2 == 0) {
showError();
break;
}

result = num1 / num2;
break;
}
return result;
}

// reading current displayNumber
private double readDisplayState() {
return Double.parseDouble(mDisplay.getText().toString());
}

// printing the result
private void printResult(Double result) {
// checking if the result having decimal part
// if not then we print integer value of it without point and zero(s)
if (result % 1 == 0) {
mDisplay.setText(Integer.toString(result.intValue()));
}

else {
mDisplay.setText(Double.toString(result));
}
}

// Error message (in case of division by zero)
private void showError() {
// printing error message
mDisplay.setText("Error");
// and reseting current state
sNum1 = sNum2 = 0;
mDisplayIsEmpty = true;
mFirstNumberReceived = false;
mNumberEntered = false;
mPointEntered = false;
}
}


Thank you very much in advance!


0 comments:

Post a Comment