Last active
January 31, 2018 11:56
-
-
Save mathsways/403b4efb7c7b908e569f5dfde3cdf10c to your computer and use it in GitHub Desktop.
Just JAVA - My First programme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:paddingBottom="10dp" | |
android:paddingLeft="10dp" | |
android:paddingRight="10dp" | |
android:paddingTop="10dp" | |
android:background="@drawable/coffee" | |
tools:context=".MainActivity"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="16dp" | |
android:text="Quantity" | |
android:textSize="20dp" | |
android:textStyle="bold" | |
android:textColor="@color/colorPrimaryDark" | |
android:textAllCaps="true" /> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal"> | |
<Button | |
android:layout_width="48dp" | |
android:layout_height="48dp" | |
android:onClick="decrement" | |
android:textSize="20sp" | |
android:text="-" | |
android:textStyle="bold"/> | |
<TextView | |
android:id="@+id/quantity_text_view" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="8dp" | |
android:layout_marginRight="8dp" | |
android:text="0" | |
android:textStyle="bold" | |
android:textColor="@android:color/black" | |
android:textSize="20sp" /> | |
<Button | |
android:layout_width="48dp" | |
android:layout_height="48dp" | |
android:onClick="increment" | |
android:textStyle="bold" | |
android:textSize="20sp" | |
android:text="+" /> | |
</LinearLayout> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="16dp" | |
android:layout_marginTop="16dp" | |
android:text="price" | |
android:textColor="@color/colorPrimaryDark" | |
android:textSize="20dp" | |
android:textStyle="bold" | |
android:textAllCaps="true" /> | |
<TextView | |
android:id="@+id/price_text_view" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="0$" | |
android:textStyle="italic" | |
android:textColor="@android:color/black" | |
android:textSize="20sp" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="16dp" | |
android:onClick="submitOrder" | |
android:padding="8dp" | |
android:textSize="16dp" | |
android:text="ORDER" /> | |
/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* IMPORTANT: Make sure you are using the correct package name. | |
* This example uses the package name: | |
* package com.example.android.justjava | |
* If you get an error when copying this code into Android studio, update it to match teh package name found | |
* in the project's AndroidManifest.xml file. | |
**/ | |
package com.mathsways.justjava; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.TextView; | |
import java.text.NumberFormat; | |
/** | |
* This app displays an order form to order coffee. | |
*/ | |
public class MainActivity extends AppCompatActivity { | |
int quantity = 0; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
/** | |
* This method is called when the order button is clicked. | |
*/ | |
public void increment(View view) { | |
quantity=quantity + 1; | |
display(quantity); | |
} | |
/** | |
* This method is called when the order button is clicked. | |
*/ | |
public void decrement(View view) { | |
quantity=quantity - 1; | |
display(quantity); | |
} | |
/** | |
* This method is called when the order button is clicked. | |
*/ | |
public void submitOrder(View view) { | |
int price = quantity*5; | |
String priceMessage="You have " + quantity + " Cups of coffees \nThe total price is " + price + "$" + "\nTank you"; | |
displayMessage(priceMessage); | |
} | |
/** | |
* 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. | |
*/ | |
/** | |
* 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)); | |
} | |
/** | |
* This method displays the given text on the screen. | |
*/ | |
private void displayMessage(String message) { | |
TextView priceTextView = (TextView) findViewById(R.id.price_text_view); | |
priceTextView.setText(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment