-
-
Save udacityandroid/609dbb5370ba0665955a to your computer and use it in GitHub Desktop.
| package com.example.android.justjava; | |
| import android.os.Bundle; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.view.View; | |
| import android.widget.CheckBox; | |
| import android.widget.TextView; | |
| /** | |
| * This app displays an order form to order coffee. | |
| */ | |
| public class MainActivity extends AppCompatActivity { | |
| int quantity = 2; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| } | |
| /** | |
| * This method is called when the plus button is clicked. | |
| */ | |
| public void increment(View view) { | |
| quantity = quantity + 1; | |
| displayQuantity(quantity); | |
| } | |
| /** | |
| * This method is called when the minus button is clicked. | |
| */ | |
| public void decrement(View view) { | |
| quantity = quantity - 1; | |
| displayQuantity(quantity); | |
| } | |
| /** | |
| * This method is called when the order button is clicked. | |
| */ | |
| public void submitOrder(View view) { | |
| // Figure out if the user wants whipped cream topping | |
| CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox); | |
| boolean hasWhippedCream = whippedCreamCheckBox.isChecked(); | |
| // Figure out if the user wants chocolate topping | |
| CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox); | |
| boolean hasChocolate = chocolateCheckBox.isChecked(); | |
| // Calculate the price | |
| int price = calculatePrice(); | |
| // Display the order summary on the screen | |
| String message = createOrderSummary(price, hasWhippedCream, hasChocolate); | |
| displayMessage(message); | |
| } | |
| /** | |
| * Calculates the price of the order. | |
| * | |
| * @return total price | |
| */ | |
| private int calculatePrice() { | |
| return quantity * 5; | |
| } | |
| /** | |
| * Create summary of the order. | |
| * | |
| * @param price of the order | |
| * @param addWhippedCream is whether or not to add whipped cream to the coffee | |
| * @param addChocolate is whether or not to add chocolate to the coffee | |
| * @return text summary | |
| */ | |
| private String createOrderSummary(int price, boolean addWhippedCream, boolean addChocolate) { | |
| String priceMessage = "Name: Lyla the Labyrinth"; | |
| priceMessage += "\nAdd whipped cream? " + addWhippedCream; | |
| priceMessage += "\nAdd chocolate? " + addChocolate; | |
| priceMessage += "\nQuantity: " + quantity; | |
| priceMessage += "\nTotal: $" + price; | |
| priceMessage += "\nThank you!"; | |
| return priceMessage; | |
| } | |
| /** | |
| * This method displays the given quantity value on the screen. | |
| */ | |
| private void displayQuantity(int numberOfCoffees) { | |
| TextView quantityTextView = (TextView) findViewById( | |
| R.id.quantity_text_view); | |
| quantityTextView.setText("" + numberOfCoffees); | |
| } | |
| /** | |
| * This method displays the given text on the screen. | |
| */ | |
| private void displayMessage(String message) { | |
| TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view); | |
| orderSummaryTextView.setText(message); | |
| } | |
| } |
I had to write this code, otherwise, when clicked on the checkbox I got the message JustJava has stopped. But in the code is not mentioned can someone explain, please?
`public void CheckBox(View view) {
boolean checked = ((CheckBox) view).isChecked();
}
public void CheckBox2(View view) {
boolean checked = ((CheckBox) view).isChecked();
}`
This code also affect the price as per chocolatebox is checked or whippedCreamBox checked
`package com.o.just_java;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import java.text.NumberFormat;
import androidx.appcompat.app.AppCompatActivity;
/**
-
This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {int quantity= 0;
int price = 5;
int whippedCreamPrice=0;
int chocolatePrice=0;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 submitOrder(View view) {
CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whippedCreamCheckBox);
boolean hasWhippedCream= whippedCreamCheckBox.isChecked();CheckBox chocolateBox = (CheckBox) findViewById(R.id.chocolateBox); boolean hasChocolate= chocolateBox.isChecked(); if(hasWhippedCream == true) { whippedCreamPrice=2; } else { whippedCreamPrice=0; } if(hasChocolate == true) { chocolatePrice=3; } else { chocolatePrice=0; } String priceMessage= "Name: "+"Omesh kumar"+ "\nQuantity: " +quantity+ "\nwippedCream: " + hasWhippedCream + "\nChocolate: " + hasChocolate + "\nTotal $ "+ quantity* (price +whippedCreamPrice +chocolatePrice) + "\nThankyou"; displayMessage(priceMessage); // displayPrice(quantity*5);}
public void Increment(View view)
{
quantity= quantity + 1;
display(quantity);
}public void Decrement(View view)
{
quantity= quantity - 1;
display(quantity);
}/**
- 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));
}
private void displayMessage(String message) {
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(message);
}
}`
- This method is called when the order button is clicked.
Visit my github to see the code xP.
XML Code:
https://github.com/deepak9962/just-java/blob/master/app/src/main/res/layout/activity_main.xml
MainActivity:
https://github.com/deepak9962/just-java/blob/master/app/src/main/java/com/justjava/MainActivity.java
I did it using a different code, still worked :D ! thank you for the code tho !!

package com.example.myapplication;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
/**
This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
int quantity = 2;
@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 submitOrder(View view) {
CheckBox whippercreamcheckbox = (CheckBox) findViewById(R.id.Whipped_Cream_checkbox);
CheckBox chocolat = (CheckBox) findViewById(R.id.Whipped_Chocolat_checkbox);
boolean hasWhpperCream = whippercreamcheckbox.isChecked();
boolean haschocolat = chocolat.isChecked();
calculatePrice(quantity);
displayMessage(creatOrderSummary(hasWhpperCream, haschocolat));
}
private int calculatePrice(int quantity){
int price = quantity *5 ;
return price ;
}
public void increment(View view) {
quantity =quantity +1;
display(quantity);
}
public void decrement(View view) {
quantity=quantity -1;
display(quantity);
}
/**
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
private void displayMessage (String number) {
int x = calculatePrice(quantity);
}