Donation Model

We now need to refactor the Base class (next Step) and move the donation related attributes and method (i.e. the variables target, totalDonated and the donations list,and the newDonation() method) into our DonationApp class.

This is a revised version of DonationApp - which now manages a list of donations. It also centralises the ‘makeDonation’ event implementing it as a method. Replace your donation with this one:

  1. package app.main;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Application;
  5. import android.util.Log;
  6. import android.widget.Toast;
  7. import app.models.Donation;
  8. public class DonationApp extends Application
  9. {
  10. public final int target = 10000;
  11. public int totalDonated = 0;
  12. public List <Donation> donations = new ArrayList<Donation>();
  13. public boolean newDonation(Donation donation)
  14. {
  15. boolean targetAchieved = totalDonated > target;
  16. if (!targetAchieved)
  17. {
  18. donations.add(donation);
  19. totalDonated += donation.amount;
  20. }
  21. else
  22. {
  23. Toast toast = Toast.makeText(this, "Target Exceeded!", Toast.LENGTH_SHORT);
  24. toast.show();
  25. }
  26. return targetAchieved;
  27. }
  28. @Override
  29. public void onCreate()
  30. {
  31. super.onCreate();
  32. Log.v("Donation", "Donation App Started");
  33. }
  34. }