Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Created June 9, 2015 23:49
Show Gist options
  • Save udacityandroid/efc863aff5c2ff5784ce to your computer and use it in GitHub Desktop.
Save udacityandroid/efc863aff5c2ff5784ce to your computer and use it in GitHub Desktop.
Add to MainActivity.java
/**
* Calculates the price of the order.
*
* @param quantity is the number of cups of coffee ordered
*/
private void calculatePrice(int quantity) {
int price = quantity * 5;
}
@mamdogogo
Copy link

package com.example.kisho.viewbutton;
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);
    }

    public void increment(View view) {

     quantity = quantity + 1;
     displayQuantity(quantity);
    

    }
    /**

    • This method decrement displays the increase int on the screen.
      /
      public void decrement(View view) {
      quantity = quantity - 1;
      displayQuantity (quantity);
      }
      /
      *
    • This method is called when the order button is clicked.
    • here displayMessage is the verbal
      */

    public void submitOrder(View view) {
    String price = "Total: $" + calculatePrice(quantity);
    displayMessage(price + "\n Thank You!");

     calculatePrice(quantity);
    

    }

    private int calculatePrice(int quantity) {
    return quantity * 5;
    }
    /**

    • This method displays the given quantity value on the screen.
      /
      private void displayQuantity(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 increment displays the increase int on the screen.
      */
      private void displayMessage(String message){
      TextView PriceTextView = (TextView)
      findViewById(R.id.price_text_view);
      PriceTextView.setText(message);

}

@wbadry
Copy link

wbadry commented Feb 20, 2018

@PrincePascal a shortest version would be

/**
     * Calculates the price of the order.
     *
     * @param quantity is the number of cups of coffee ordered
     */
    private int calculatePrice(int quantity) {
       return quantity * 5;
    }

@badrddinb
Copy link

DONE

@codersewar
Copy link

Done too

@louayeldin
Copy link

Done

@AsmaaMghodia
Copy link

Done

@anouswazzan
Copy link

done

@lakshmiprasannatirumala
Copy link

This is my code to calculate price of coffees.
int quan=2;
public void submitOrder(View view)
{
String pm="Your order is "+quan+"\n Your order is ready\n";
pm=pm + " Total is $"+ calculatePrice();
pm=pm+" \n thank you visit again ";
displayMessage(pm);
}
public void increment(View view)
{
quan=quan+1;
display(quan);
}
public void decrement(View view)
{
display(quan=quan-1);
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.one_text_view);
quantityTextView.setText("" + number);
}
private void displayPrice(int number) {
TextView priceTextView = (TextView) findViewById(R.id.dollar_text_view);
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
}
private void displayMessage(String message) {
TextView priceTextView = (TextView) findViewById(R.id.dollar_text_view);
priceTextView.setText(message);
}
private int calculatePrice() {
int price = quan * 10;
return price;
}

@mohitsisodiya
Copy link

/**

  • Calculates the price of the order.
  • @param quantity is the number of cups of coffee ordered
  • @param price is the price of 1 cup of coffee
    */
    private int calculatePrice(int quantity,int price){
    return (quantity * price);
    }

@shoooka
Copy link

shoooka commented May 11, 2018

Done

@bhargavkonkathi
Copy link

Done

@SaeedAlbaradie
Copy link

/**
* This method is called when the order button is clicked.
*/

public void submitOrder(View view) {

    int price = quantity * 5;
    //String priceMessage = "That would be $" + price + " please";
    //String priceMessage = "You owe" +" "+ price + " bucks, dude!";
    //String priceMessage = price + " dollars for " + quantity + " cups of coffee. Pay up.";
    //String priceMessage = "Total: $" + price + "\n" +  "Thank You!";
    String priceMessage = "Total: $" + price;
    //priceMessage = priceMessage + " Thank you!";
    priceMessage = priceMessage + "\nThank you!";
    displayMessage(priceMessage);

    //calculatePrice(quantity);
    // calculatePrice(quantity, 10);
    calculatePrice();
}

/**
 * Calculates the price of the order.
 *
 * @param quantity    is the number of cups of coffee ordered
 * @param pricePerCup is the price of one cup of coffee
 */

//private void calculatePrice(int quantity ) {
//private void calculatePrice(int quantity, int pricePerCup){
private void calculatePrice(){
    int price = quantity * 5;
    //int price = quantity * pricePerCup;

}

/**
 * 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));

}

/**
 * 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);

}

}

@Eduese
Copy link

Eduese commented May 21, 2018

private void calculatePrice(int quantity) {
int price = quantity * 5;
}

I experimented with private int method_name() but without an argument, and with a return type, tho

Got the same answer

@mahmoudkashef
Copy link

thanks for the tips guys

@orwa-te
Copy link

orwa-te commented Jun 24, 2018

Very very simple

@Younes666
Copy link

done

@HDAII
Copy link

HDAII commented Jun 30, 2018

untitled

Can someone help me please i have a problem when i press run the program give me error select android SDK !!!!

@djalilo24
Copy link

answer 1
int numberOfCoffees =0;int price=0;
public void submitOrder(View view) {
calculatePrice(numberOfCoffees);
String PriceMessge="Total: $"+price+ " \nThank You!";
displayMessage(PriceMessge);
}

/**
 * Calculates the price of the order.
 *
 * @param quantity is the number of cups of coffee ordered
 */
private int calculatePrice(int quantity) {
     price = quantity * 5;
    return price;
}

===============================================================
answer 2
int numberOfCoffees =0;int price=0;
public void submitOrder(View view) {
calculatePrice(numberOfCoffees,10);
String PriceMessge="Total: $"+price+ " \nThank You!";
displayMessage(PriceMessge);
}

/**
 * Calculates the price of the order.
 *
 * @param quantity is the number of cups of coffee ordered
 */
private int calculatePrice(int quantity,int prix) {
     price = quantity * prix;
    return price;
}

===========================================
answer 3
int numberOfCoffees =0;int price=0;
public void submitOrder(View view) {
calculatePrice();
String PriceMessge="Total: $"+price+ " \nThank You!";
displayMessage(PriceMessge);
}

/**
 * Calculates the price of the order.
 *
 * @param price is the number of cups of coffee ordered
 */
private int calculatePrice() {
     price = numberOfCoffees * 5;
    return price;
}

@MinaMagdyLuis
Copy link

please help me
I can not find the just java app code to solve this test

@bgannon29
Copy link

I declared quantity as a global variable that is updated with increment and decrement buttons......

public void submitOrder(View view) {
    displayMessage(calculatePrice(quantity));
}

@org.jetbrains.annotations.NotNull
private String calculatePrice(int quantity){
    return priceMessage + "\n" + NumberFormat.getCurrencyInstance().format(quantity * pricePerCup);
}

@nagaawy
Copy link

nagaawy commented Dec 31, 2019

where to find the new code of the project of this course? as submitOrder..etc?

@pro-coder-fast
Copy link

public int displayQuantityA(int quantity,int price_of_per_coffee) {
int calculate = quantity * price_of_per_coffee;
return calculate;
}

@pro-coder-fast
Copy link

public int displayQuantityB() {
int calculate = quantity * 5;
return calculate;
}

@abhi0320
Copy link

abhi0320 commented Jun 3, 2021

public int displayQuantityB() {
int calculate = quantity * 5;
return calculate;
}

@ahmednajatusman
Copy link

public void submitOrder(View view) {
String priceMessage = "Free";
displayMessage(priceMessage);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment