Last active
February 28, 2016 16:51
-
-
Save vzool/6b32e819ead7863dffd3 to your computer and use it in GitHub Desktop.
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
package com.example.megam.banksevs; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import com.parse.GetCallback; | |
import com.parse.ParseException; | |
import com.parse.ParseObject; | |
import com.parse.ParseQuery; | |
import com.parse.ParseUser; | |
public class TransferToAccount extends AppCompatActivity { | |
Button transfer; | |
EditText mAccountNumber, mConfirm, mbalance; | |
int mBalance, transfered, q; | |
ParseUser user; | |
String mAccountNum; | |
String mCAccountNum; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_transfer_to_account); | |
mAccountNumber = (EditText) findViewById(R.id.UserAccount); | |
mConfirm = (EditText) findViewById(R.id.UserAccountC); | |
mbalance = (EditText) findViewById(R.id.BalanceTrans); | |
transfer = (Button) findViewById(R.id.send); | |
mAccountNum = mAccountNumber.getText().toString().trim(); | |
mCAccountNum = mConfirm.getText().toString().trim(); | |
user = ParseUser.getCurrentUser(); | |
try { | |
transfer.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
String _balance = mbalance.getText().toString().trim(); | |
Log.d("balance :", _balance); | |
mBalance = Integer.parseInt(_balance); | |
if (mbalance.getText().toString().trim().equals("0")) { | |
mbalance.setError("Error"); | |
} | |
if (!mAccountNumber.getText().toString().trim().equals(mConfirm.getText().toString().trim())) { | |
mAccountNumber.setError("Error"); | |
mConfirm.setError("Error"); | |
} | |
Log.d("user 1 :",String.valueOf(user.getUsername())); | |
ParseQuery<ParseObject> userParseQuery = ParseQuery.getQuery(user.getClassName()); | |
userParseQuery.whereEqualTo("AccountNumber", mAccountNum); | |
userParseQuery.getFirstInBackground(new GetCallback<ParseObject>() { | |
@Override | |
public void done(final ParseObject object, ParseException e) { | |
if (e == null) { | |
q = (int) object.get("balance"); | |
Log.d("user 2 balance :",String.valueOf(q)); | |
transfered = (int) user.get("balance"); | |
Log.d("user 1 balance",String.valueOf(transfered)); | |
if (mBalance < 6 || transfered < 6) { | |
Toast.makeText(TransferToAccount.this | |
, "الحد الادنى هو 5جنيهات" | |
, Toast.LENGTH_SHORT).show(); | |
} else if (mAccountNum.equals(user.getString("AccountNumber"))) { | |
Toast.makeText(TransferToAccount.this | |
, "لا يمكنك التحويل لنفسك , يا غبي" | |
, Toast.LENGTH_SHORT).show(); | |
} else { | |
final ParseObject parseObject = new ParseObject("Transactions"); | |
parseObject.put("NationalID", "N/A"); | |
parseObject.put("PersonName", "N/A"); | |
parseObject.put("TransactionType", 1); | |
parseObject.put("Transformer", user.getUsername()); | |
Log.d("user1 :",String.valueOf(user.getUsername())); | |
parseObject.put("TransformerNumber", user.get("AccountNumber")); | |
Log.d("user 1 account number",String.valueOf(user.get("AccountNumber"))); | |
parseObject.put("TransformedTo", object.getString("username")); | |
Log.d("user 2 ",String.valueOf(object.get("username"))); | |
parseObject.put("TransformedToNumber", object.getString("AccountNumber")); | |
Log.d("user 2 account number",String.valueOf(object.getString("AccountNumber"))); | |
parseObject.put("Balance", mBalance); | |
Log.d("Transferred balance",String.valueOf(mBalance)); | |
parseObject.saveInBackground(); | |
ParseQuery<ParseObject> pq = ParseQuery.getQuery(user.getClassName()); | |
pq.whereEqualTo("objectId",user.getObjectId()); | |
pq.getInBackground(user.getObjectId(), new GetCallback<ParseObject>() { | |
@Override | |
public void done(ParseObject po, ParseException e) { | |
if (e == null) { | |
Log.d("user 1 objectID",String.valueOf(user.getObjectId())); | |
po.put("balance", transfered - mBalance); | |
Log.d("user 1 new balance",String.valueOf(transfered - mBalance)); | |
po.saveInBackground(); | |
Toast.makeText(TransferToAccount.this, "done updating 1st side", | |
Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
ParseQuery<ParseObject> pop = ParseQuery.getQuery(object.getClassName()); | |
pop.whereEqualTo("objectId",object.getObjectId()); | |
pop.getInBackground(object.getObjectId(), new GetCallback<ParseObject>() { | |
@Override | |
public void done(ParseObject parseObject, ParseException e) { | |
if (e == null) { | |
Log.d("user 2 objectID ",String.valueOf(object.getObjectId())); | |
parseObject.put("balance", q + mBalance); | |
Log.d("user 2 new balance",String.valueOf(q+mBalance)); | |
parseObject.saveInBackground(); | |
Toast.makeText(TransferToAccount.this, "done updating 2nd side " | |
, Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
} | |
} else { | |
Toast.makeText(TransferToAccount.this, | |
"Can not find Account associated with the account Number", | |
Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
} | |
}); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment