Coder Social home page Coder Social logo

amitnv / fphsdemo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from davidbenko/fphsdemo

0.0 2.0 0.0 4.32 MB

Demo Android application for Forest Park High School. Used for presentation on Nov. 25, 2013

Home Page: http://davidbenko.me

Java 100.00%

fphsdemo's Introduction

FPHSDemo

Demo Android application for Forest Park High School.
User has a bank that they can deposit and withdraw money from.
Google Play Link

Instructions to Make

  1. Create a blank activity project with no navigation
  • Name your MainActivity BankActivity
  • If eclipse can't find your AndroidManifest.xml, highlight your project folder and hit refresh (F5)
  1. Add UI Elements to your activity_bank.xml
  • Add a TextView and name it balanceTextView
  • Add an EditText and name it amountTextField
  • Change the input type of amountTextField to include number and numberDecimal
  • Add a Button and name it depositButton
  • Add a Button and name it withdrawButton
  1. Add Event Listeners to your buttons
  • In your activity_bank.xml add the following code to your <Button /> tags
  • Add android:onClick="depositClicked" to the depositButton
  • Add android:onClick="withdrawClicked" to the withdrawButton
  • Example Below:
    <Button
        android:id="@+id/depositButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/amountTextField"
        android:layout_below="@+id/amountTextField"
        android:layout_marginTop="15dp"
        android:onClick="depositClicked"
        android:text="Deposit" />

    <Button
        android:id="@+id/withdrawButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/depositButton"
        android:layout_alignBottom="@+id/depositButton"
        android:layout_alignRight="@+id/amountTextField"
        android:onClick="withdrawClicked"
        android:text="Withdraw" />
  • Add the following code to your BankActivity.java
	public void depositClicked(View v){
		Toast.makeText(getApplicationContext(), "Deposit Made",
				   Toast.LENGTH_SHORT).show();
	}
	public void withdrawClicked(View v){
		Toast.makeText(getApplicationContext(), "Withdraw Made",
				   Toast.LENGTH_SHORT).show();
	}
  • Press Cmd+Shift+O to import libraries
  • Build and Run - You should now see messages when you press your buttons.
  1. Create a new Class named Bank and add the following code:
/**
 * Bank
 * 
 * The Bank class is used to manage the amount of money the user has stored.
 * You can deposit, withdraw and view the bank amount with the Bank.
 */
import android.content.Context;
import android.content.SharedPreferences;

public class Bank {
	
	/*
	 * Set the amount of money in the bank
	 */
	private void setBalance(Context c, float balance){
		// Create an instance of the user preferences
		SharedPreferences userPreferences = c.getSharedPreferences("FPHS-Bank", Context.MODE_PRIVATE);
        // Create a preference editor
		SharedPreferences.Editor prefsEditor = userPreferences.edit();
		// Insert the balance into the user preferences
        prefsEditor.putFloat("balance", balance);
        // Save your changes
        prefsEditor.commit();
	}
	/*
	 * Get the amount of money in the bank
	 */
	public float getBalance(Context c){
		// Create an instance of the user preferences
		SharedPreferences userPreferences = c.getSharedPreferences("FPHS-Bank", Context.MODE_PRIVATE);
        // Request the balance from the user preferences. 
		// If the balance is not there give us zero instead
		float balance = userPreferences.getFloat("balance", 0.0f);
		// Return the balance
        return balance;
	}
	/*
	 * Deposit money into the bank
	 */
	public void deposit(Context c, float depositAmount){
		// Get the balance
		float balance = getBalance(c);
		// Increment the balance by the deposit amount
		balance += depositAmount;
		// Save the new balance
		setBalance(c,balance);
	}
	/*
	 * Withdraw Money from the bank
	 */
	public void withdraw(Context c, float withdrawAmount){
		// Get the balance
		float balance = getBalance(c);
		// Decrement the balance by the withdraw amount
		balance -= withdrawAmount;
		// Save the new balance
		setBalance(c,balance);
	}
}
  1. Modify your BankActivity.java
  • Add two new methods:
	/*
	 * Gets the number from the user's input
	 */
	public float getUserEntry(){
		// Find the EditText
		EditText amountTextField = (EditText) findViewById(R.id.amountTextField);
		// Parse user entry into a floating-point number
		float number = Float.valueOf(amountTextField.getText().toString());
		return number;
	}
	
	/*
	 * Update Balance Label
	 */
	private void updateUI(){
		// Make a new Bank
		Bank bank = new Bank();
		// Get the Bank balance as cast into a string
		String balance = Float.toString(bank.getBalance(getApplicationContext()));
		// Find the TextView
		TextView balanceTextView = (TextView) findViewById(R.id.balanceTextView);
		// Set the TextView's text to our balance string
		balanceTextView.setText(balance);
	}
  • Update your button event listeners
	/*
	 * onClick Listener for depositButton
	 */
	public void depositClicked(View v){
		// Make a new Bank
		Bank bank = new Bank();
		// Deposit the amount the user entered from the bank
		bank.deposit(getApplicationContext(), getUserEntry());
		// Update UI to reflect new balance
		updateUI();
		// Show Deposit Notice
		Toast.makeText(getApplicationContext(), "Deposit Made",
				   Toast.LENGTH_SHORT).show();
	}
	/*
	 * onClick Listener for withdrawButton
	 */
	public void withdrawClicked(View v){
		// Make a new Bank
		Bank bank = new Bank();
		// Withdraw the amount the user entered from the bank
		bank.withdraw(getApplicationContext(), getUserEntry());
		// Update UI to reflect new balance
		updateUI();
		// Show Withdraw Notice
		Toast.makeText(getApplicationContext(), "Withdraw Made",
				   Toast.LENGTH_SHORT).show();
	}
  1. Congratulations!
  • Build and Run the final product.
  • You should now be able to deposit and withdraw from the bank!

fphsdemo's People

Contributors

davidbenko avatar

Watchers

James Cloos avatar Amit Vaidya avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.