Skip to content

Instantly share code, notes, and snippets.

@sturdustgeorgiana
Last active January 29, 2018 01:07
Show Gist options
  • Save sturdustgeorgiana/65b79b7ddcb253b6c6dc8405bd40b39b to your computer and use it in GitHub Desktop.
Save sturdustgeorgiana/65b79b7ddcb253b6c6dc8405bd40b39b to your computer and use it in GitHub Desktop.
Primavara.javacode Android for Beginners
package com.sistahwork.costin; /**
* Add your package below. Package name can be found in the project's AndroidManifest.xml file.
* This is the package name our example uses:
*
* package com.example.android.justjava;
*/
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* This app displays an order form to order coffee.
*/
public class Primavara extends AppCompatActivity {
int quantity = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_primavara);
}
/**
* This method is called when the plus button is clicked.
*/
public void increment(View view) {
if (quantity == 100) {
// Show a message if the quantity is greater.
Toast.makeText(this, "All the stars are yours ^^.", Toast.LENGTH_SHORT).show();
return;
}
quantity = quantity + 1;
displayQuantity(quantity);
}
/**
* This method is called when the minus button is clicked.
*/
public void decrement(View view) {
if (quantity == 1){
//Show a message if the quantity is less.
Toast.makeText(this, "Not allowed to have less then one star.", Toast.LENGTH_SHORT).show();
return;
}
quantity = quantity - 1;
displayQuantity(quantity);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
EditText theOwner = (EditText)findViewById(R.id.the_owner);
String owner = theOwner.getText().toString();
CheckBox nebulasCheckBox = (CheckBox) findViewById(R.id.nebulas_checkbox);
boolean hasNebulas= nebulasCheckBox.isChecked();
CheckBox blackHolesCheckBox = (CheckBox) findViewById(R.id.blackholes_checkbox);
boolean hasBlackHoles = blackHolesCheckBox.isChecked();
CheckBox cometCheckBox = (CheckBox) findViewById(R.id.comets_checkbox);
boolean hasComets = cometCheckBox.isChecked();
int stars = calculateStars(hasNebulas,hasBlackHoles,hasComets);
String priceMessage = creatOrderSummary (owner, stars, hasNebulas, hasBlackHoles, hasComets);
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_SUBJECT, owner + "`s universe");
intent.putExtra(Intent.EXTRA_TEXT, priceMessage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
/**
* Calculate the stars of the order.
* @return stars
*/
private int calculateStars(boolean addNebulas, boolean addBlackHoles, boolean addComets) {
int baseStars = 5;
if (addBlackHoles){
baseStars = baseStars + 0;
}
if (addComets) {
baseStars = baseStars + 10;
}
if (addNebulas) {
baseStars = baseStars + 1000;
}
return baseStars * quantity;
}
/** Creat summary order
* @param owner define the owner
* @param stars number of stras
* @param hasNebulas whether or net neblas are required
* @return text summary
*/
private String creatOrderSummary (String owner, int stars, boolean hasNebulas, boolean hasBlackHoles, boolean hasComets) {
String priceMessage = "Owner:" + owner;
priceMessage += "\nTotal of " + stars + " Stars are YouRs " + ".";
priceMessage += "\nNow you have a Nebula." + hasNebulas;
priceMessage += "\nBeaware of the BlackHole!" + hasBlackHoles;
priceMessage += "\nDon`t you worry, Rosetta is commin` ^^" + hasComets;
priceMessage = priceMessage + "\nMay the Force be with you.";
return priceMessage;
}
/**
* 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment