Inventory.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import java.util.ArrayList;
  2. public class Inventory {
  3. private Item[] items;
  4. private ArrayList<String> specialItems;
  5. public Inventory(Item[] items) {
  6. super();
  7. this.items = items;
  8. this.specialItems = new ArrayList<String>();
  9. this.specialItems.add("Aged Brie");
  10. this.specialItems.add("Backstage passes to a TAFKAL80ETC concert");
  11. this.specialItems.add("Sulfuras, Hand of Ragnaros");
  12. this.specialItems.add("Conjured");
  13. }
  14. public void updateQuality() {
  15. for (int i = 0; i < items.length; i++) {
  16. //updateSulfurusQuality(i);
  17. updateNormalItemQuality(i);
  18. updateAgedBrieQuality(i);
  19. updateBackStageQuality(i);
  20. updateConjuredQuality(i);
  21. updateSellIn(i);
  22. }
  23. }
  24. public void updateSellIn(int i) {
  25. if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
  26. items[i].setSellIn(items[i].getSellIn() - 1);
  27. }
  28. }
  29. public boolean determineSpecialQuality(int i) {
  30. if (specialItems.contains(items[i].getName())) {
  31. return true;
  32. }
  33. return false;
  34. }
  35. public boolean pastSellIn(int i) {
  36. if (items[i].getSellIn() < 0) {
  37. return true;
  38. }
  39. return false;
  40. }
  41. public void updateAgedBrieQuality(int index) {
  42. if (items[index].getName().equals("Aged Brie")) {
  43. if (pastSellIn(index)) {
  44. changeQuality(index, 2);
  45. } else {
  46. changeQuality(index, 1);
  47. }
  48. pastMaxQuality(index);
  49. }
  50. }
  51. public void updateBackStageQuality(int index) {
  52. if (items[index].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
  53. if (items[index].getSellIn() > 10) {
  54. changeQuality(index, 1);
  55. } else if (items[index].getSellIn() > 5) {
  56. changeQuality(index, 2);
  57. } else if (items[index].getSellIn() >= 0) {
  58. changeQuality(index, 3);
  59. } else {
  60. items[index].setQuality(0);
  61. }
  62. pastMaxQuality(index);
  63. }
  64. }
  65. public void updateConjuredQuality(int index) {
  66. if (items[index].getName().equals("Conjured")){
  67. changeQuality(index, -2);
  68. pastZeroQuality(index);
  69. }
  70. }
  71. public void updateSulfurusQuality(int i) {
  72. if (items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
  73. //change nothing
  74. }
  75. }
  76. public void updateNormalItemQuality(int index) {
  77. if (!determineSpecialQuality(index)) {
  78. if (pastSellIn(index)) {
  79. changeQuality(index, -2);
  80. } else {
  81. changeQuality(index, -1);
  82. }
  83. pastZeroQuality(index);
  84. }
  85. }
  86. public void changeQuality(int index, int amount) {
  87. items[index].setQuality(items[index].getQuality() + amount);
  88. }
  89. public void pastMaxQuality(int index) {
  90. if (items[index].getQuality() > 50) {
  91. items[index].setQuality(50);
  92. }
  93. }
  94. public void pastZeroQuality(int index) {
  95. if (items[index].getQuality() < 0) {
  96. items[index].setQuality(0);
  97. }
  98. }
  99. }