Skip to content

Instantly share code, notes, and snippets.

@mezdour
Created January 30, 2018 08:27
Show Gist options
  • Save mezdour/b1a95a7728e8dd8f258cf00a2567fbc8 to your computer and use it in GitHub Desktop.
Save mezdour/b1a95a7728e8dd8f258cf00a2567fbc8 to your computer and use it in GitHub Desktop.
JustJava java code
package com.example.android.justjava;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.text.NumberFormat;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int numberOfCoffees = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display(numberOfCoffees);
}
public void submitOrder(View view) {
display(numberOfCoffees);
displayPrice(numberOfCoffees*3);
}
public void quantityPlus(View view) {
numberOfCoffees = numberOfCoffees + 1;
display(numberOfCoffees);
}
public void quantityMines(View view) {
if(numberOfCoffees != 0)
{
numberOfCoffees = numberOfCoffees - 1;
}
display(numberOfCoffees);
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
/**
* This method displays the given price on the screen.
*/
private void displayPrice(int number) {
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment