I work with Android Room database.
I have an entity "Member" and an entity "Transaction". I want to create a new Transaction (e.g. buying a book). Then I want to link all members (per id) with the transaction id. That means if 5 people buy the book, then I want to link the 5 ids with the transaction id.
After I do that, the balance in member should change (e.g. a member has 20 euros and the book costs 10, after transaction - the member should have 10 euros).
I have the activity to create a new transaction. Now I want to create a new activity, which should be started after the NewTransactionActivity to handle the linking of the members to the transaction.
How do I link a transaction id with multiple member ids? Also, how I can perform calculations with BigDecimal? So when I make a new transaction, every linked member's balance should change.
Right now, I have the balance in type double, but I'm gonna change it.
Class Transaction:
@Entity(tableName = "transaction_table")
public class Transaction {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "TransactionID")
private long id;
@ColumnInfo(name = "Transaction Name")
private String transactionName;
@ColumnInfo(name = "Transaction Balance")
private double balance;
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTransactionName() {
return transactionName;
}
public void setTransactionName(String transactionName) {
this.transactionName = transactionName;
}
public Transaction(String transactionName, double balance) {
this.transactionName = transactionName;
this.balance = balance;
}
}
Class Member:
@Entity(tableName = "member_table")
public class Member {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "MemberID")
private long id;
@ColumnInfo(name = "First Name")
private String firstname;
@ColumnInfo(name = "Surname")
private String surname;
@ColumnInfo(name = "Balance")
private double balance;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Member(String firstname, String surname) {
this.firstname = firstname;
this.surname = surname;
this.balance = 0;
}
}
New Transaction Activity:
public class NewTransactionActivity extends AppCompatActivity {
public static final String EXTRA_REPLY = "com.example.android.transactionlistsql.REPLY";
public static final String EXTRA_REPLY2 = "com.example.android.transactionlistsql.REPLY2";
private EditText mEditTextTransaction;
private EditText mEditTextTransaction2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newtransaction_activity);
mEditTextTransaction = findViewById(R.id.NewTransactionName);
mEditTextTransaction2 = findViewById(R.id.NewTransactionBalance);
mEditTextTransaction2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
mEditTextTransaction2.setText("0");
final Button button = findViewById(R.id.NewTransactionButtonSave);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(mEditTextTransaction.getText())){
Toast.makeText(getApplicationContext(), R.string.TransactionNameMissing, Toast.LENGTH_LONG).show();
return;
//setResult(RESULT_CANCELED, replyIntent);
}
else if (TextUtils.isEmpty(mEditTextTransaction2.getText())){
mEditTextTransaction2.setText("0");
}
else {
String newtransactionname = mEditTextTransaction.getText().toString()
double newtransactionbalance = Double.parseDouble(mEditTextTransaction2.getText().toString()));
replyIntent.putExtra(EXTRA_REPLY, newtransactionname);
replyIntent.putExtra(EXTRA_REPLY2, newtransactionbalance);
setResult(RESULT_OK, replyIntent);
}
finish();
}
});
}
}