Donation Model & Base Class
In order to keep our application design coherent, we now bring in an Base class and a Donation class to manage our Donations. You can continue with your own version of the app or start with the solution from the previous lab - Donation.2.0
First, create a new package called ‘app.models’ and bring in this class here:
1. package app.models;
3. public class Donation
4. {
5. public int amount;
6. public String method;
8. public Donation (int amount, String method)
9. {
10. this.amount = amount;
11. this.method = method;
12. }
13. }
Next, Create a new class called ‘Base’ and add it to the ‘app.activities’ package:
1. public class Base extends Activity
2. {
3. public final int target = 10000;
4. public int totalDonated = 0;
5. public static List <Donation> donations = new ArrayList<Donation>();
7. public boolean newDonation(Donation donation)
8. {
9. boolean targetAchieved = totalDonated > target;
10. if (!targetAchieved)
11. {
12. donations.add(donation);
13. totalDonated += donation.amount;
14. }
15. else
16. {
17. Toast toast = Toast.makeText(this, "Target Exceeded!", Toast.LENGTH_SHORT);
18. toast.show();
19. }
20. return targetAchieved;
21. }
23. @Override
24. public boolean onCreateOptionsMenu(Menu menu)
25. {
26. getMenuInflater().inflate(R.menu.donate, menu);
27. return true;
28. }
30. @Override
31. public boolean onPrepareOptionsMenu (Menu menu){
32. super.onPrepareOptionsMenu(menu);
33. MenuItem report = menu.findItem(R.id.menuReport);
34. MenuItem donate = menu.findItem(R.id.menuDonate);
36. if(donations.isEmpty())
37. report.setEnabled(false);
38. else
39. report.setEnabled(true);
41. if(this instanceof Donate){
42. donate.setVisible(false);
43. if(!donations.isEmpty())
44. report.setVisible(true);
45. }
46. else {
47. report.setVisible(false);
48. donate.setVisible(true);
49. }
51. return true;
52. }
54. public void settings(MenuItem item)
55. {
56. Toast.makeText(this, "Settings Selected", Toast.LENGTH_SHORT).show();
57. }
59. public void report(MenuItem item)
60. {
61. startActivity (new Intent(this, Report.class));
62. }
64. public void donate(MenuItem item)
65. {
66. startActivity (new Intent(this, Donate.class));
67. }
68. }
Notice our List of Donations in the Base class - we will use this list to display our Donations in the Report.
Your project should now look as follows:

