0

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();
        }
    });
}
}
LordGash
  • 285
  • 1
  • 4
  • 14
  • 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. Can you please elaborate on this? – Viraj Patel Jul 06 '18 at 09:52

1 Answers1

0

Add MemberId in your Transaction.java entity class and also set @ForeignKey with Member.java entity class.

Transaction.java

import static android.arch.persistence.room.ForeignKey.CASCADE;

@Entity(tableName = "transaction_table", foreignKeys = @ForeignKey(entity = Member.class,
        parentColumns = "MemberID",
        childColumns = "member_id",
        onDelete = CASCADE))
public class Transaction {


    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "TransactionID")
    private long id;

    @ColumnInfo(name = "member_id")
    private long MemberId;

    public long getMemberId() {
    return MemberId;
    }

    public void setMemberId(long memberId) {
        MemberId = memberId;
    }

    //Rest of the fields
}

How do I link a transaction id with multiple member ids?

If there is split payment in your application then you have to map a transaction id to multiple members.

Above code will map one to many relationships between Member and Transaction table. One Member can do many transactions.

You can get all user's transaction by following query:

@Query("SELECT * FROM transaction_table tt INNER JOIN member_table mt on tt.member_id = mt.MemberID WHERE member_id =:memberId")
List<Transaction> getMemberTransactions(long memberId);

To update the balance of Member, you have to write update balance query in which you have to check the available balance of Member and have to deduct transaction amount from member's total balance which will be reflected Balance column of member_table.

Viraj Patel
  • 2,113
  • 16
  • 23
  • Okay, I'm gonna try to implement your idea and I will post the results. – LordGash Jul 06 '18 at 13:07
  • Ok fine. Let me know if you need any help. – Viraj Patel Jul 06 '18 at 13:18
  • Does the MemberId in Transactions have to be int or can it also be long? – LordGash Jul 06 '18 at 13:21
  • You have to give the same type which you have in Member.java entity. I updated my answer as well. It was my mistake. – Viraj Patel Jul 06 '18 at 13:26
  • In your query you still have int which should be long. I wrote your query in my TransactionDao. Now I want to implement that into the Repository, but in which one? The repository for member or the repository for transaction? – LordGash Jul 06 '18 at 20:30
  • Now I have the methods to get all transactions for a member right? Now, how can I even link the transaction to the member? I have an activity to create new transaction, in that activity I enter the name of the transaction and have a button to insert. After I click on that button, I want to see the list of members. Then I want to choose the members I want to link to the transaction. By clicking on every member, it should show me a tick next to the member I chose. Then the transaction should be inserted with all the linked members I chose. – LordGash Jul 08 '18 at 09:18
  • Yes in Query it should be long. To link transaction with multiple members, you can carry transaction detail to next screen instead of inserting it in new transaction screen. Once you select members in member listing screen than on that screen you can insert multiple transactions along with member id by executing selected members for loop. – Viraj Patel Jul 09 '18 at 05:16
  • I get your idea, but I don't know how to do it. Can you give me an example or something? This is my new question: https://stackoverflow.com/questions/51236477/how-to-use-multiple-selection-with-room-database Can you help me there? It's the same question, but more elaborated. – LordGash Jul 09 '18 at 10:35
  • @LordGash I have posted answer [here on your another question](https://stackoverflow.com/a/51246033/9293029) that how you can integrate my idea of inserting multiple transactions for selected members. – Viraj Patel Jul 09 '18 at 13:06