title: How to use setBalance method in my entity to set the actual balance value of a member?
body: <p>I use Android Room for my database. I have a title screen and when I click on a button there, my activity opens. The activity shows me a list of members saved in the database. My entity Member consists of first name, surname and balance. I can add new members. The user can set first name and surname, the balance is automatically set with the value 0. I do have another entity called transactions, where I can add transaction and link transactions with members.</p>
  
  <p>If I add two new transactions for a member a, the member a's balance goes up. I can click on a member in my member activity, then I see additional information of the member. There, I can see the actual balance he has with a SUM() query in my DAO.</p>
  
  <p>But I don't know, how to use that query to set Balance of a member. I also don't know where(in which class) I should use the set Balance method of the member class.</p>
  
  <p>Entity Member:</p>
  
  <pre><code>@TypeConverters(Converters.class)
  @Entity(tableName = "member_table")
  public class Member {
  
      @PrimaryKey(autoGenerate = true)
      @ColumnInfo(name = "MemberID")
      private long id;
  
      @ColumnInfo(name = "FirstName")
      private String firstname;
  
      @ColumnInfo(name = "Surname")
      private String surname;
  
      @ColumnInfo(name = "MemberBalance")
      private BigDecimal balance;
  
      private boolean isSelected = false;
  
      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 BigDecimal getBalance() {
          return balance;
      }
  
      public void setBalance(BigDecimal 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 = new BigDecimal(0).setScale(2, RoundingMode.HALF_EVEN);
      }
  
      public void setSelected(boolean selected) {
          isSelected = selected;
      }
  
      public boolean isSelected() {
          return isSelected;
      }
  
  }
  </code></pre>
  
  <p>Entity Transaction:</p>
  
  <pre><code>@TypeConverters(Converters.class)
  @Entity(tableName = "transaction_table")
  public class Transaction {
  
      @PrimaryKey(autoGenerate = true)
      @ColumnInfo(name = "TransactionID")
      private long id;
  
      @ColumnInfo(name = "TransactionName")
      private String transactionName;
  
      @ColumnInfo(name = "TransactionBalance")
      private BigDecimal balance;
  
      public BigDecimal getBalance() {
          return balance;
      }
  
      public void setBalance(BigDecimal 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, BigDecimal balance) {
          this.transactionName = transactionName;
          this.balance = new BigDecimal(String.valueOf(balance)).setScale(2, RoundingMode.HALF_EVEN);
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/msTyC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/msTyC.png" alt="This is my member activity where I see the list of all members. The balance is always 0.00 because I never use setBalance() method, and the balance should be the value gotten by the getBalance() method."></a></p>
  
  <p><a href="https://i.stack.imgur.com/vygcB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vygcB.png" alt="This is my transaction activity where I can see the list of all transactions."></a></p>
  
  <p><a href="https://i.stack.imgur.com/WnRTG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WnRTG.png" alt="This activity starts when I click on a member. Then I see the member names and below all the transactions he took part. The balance of the member here is taken with a DAO-Query: SELECT SUM(TransactionBalance)FROM member_transaction_table INNER JOIN transaction_table ON transaction_table.TransactionID = member_transaction_table.Transaction_ID WHERE member_transaction_table.Member_ID =:memberid"></a></p>
  
  <p>First picture: This is my member activity where I see the list of all members. The balance is always 0.00 because I never use setBalance() method, and the balance should be the value gotten by the getBalance() method.</p>
  
  <p>Second picture: This is my transaction activity where I can see the list of all transactions.</p>
  
  <p>Third picture: This activity starts when I click on a member. Then I see the member names and below all the transactions he took part. The balance of the member here is taken with a DAO-Query: SELECT SUM(TransactionBalance)FROM member_transaction_table INNER JOIN transaction_table ON transaction_table.TransactionID = member_transaction_table.Transaction_ID WHERE member_transaction_table.Member_ID =:memberid</p>
  
  <p>I can't figure out how and where to let the app calculate the transactions. The value returned by the calculations should be set with the setBalance() method.</p>
  
  <p>Can anybody help me with this? If you need any specific code, just write in the comments and I'll add it to this question. Thanks!</p>
  
  <p>Transaction Repository:</p>
  
  <pre><code>public class TransactionRepository {
  
      private TransactionDao mTransactionDao;
      private MemberTransactionDao mMemberTransactionDao;
      private LiveData&lt;List&lt;Transaction&gt;&gt; mAllTransaction;
      private MemberDao mMemberDao;
  
      TransactionRepository(Application application) {
          SmartBookkeepingRoomDatabase db = SmartBookkeepingRoomDatabase.getDatabase(application);
          mTransactionDao = db.transactionDao();
          mMemberTransactionDao = db.memberTransactionDao();
          mMemberDao = db.memberDao();
          mAllTransaction = mTransactionDao.getAllTransactions();
      }
  
      public void addTransactionBalanceToMember(Member member, Transaction transaction, OnTransactionInsertedListener listener) {
          insert(transaction, listener);
          BigDecimal newBalance = member.getBalance().add(transaction.getBalance());
          member.setBalance(newBalance);
          mMemberDao.update(member);
      }
  
      LiveData&lt;List&lt;Transaction&gt;&gt; getAllTransactions() {
          return mAllTransaction;
      }
  
  
      public void insert(Transaction transaction, OnTransactionInsertedListener listener) {
          new insertAsyncTask(mTransactionDao, listener).execute(transaction);
      }
  }
  </code></pre>