package com.gildedrose;

class GildedRose {
	private static final String SULFURAS = "Sulfuras, Hand of Ragnaros";
	private static final String BACKSTAGE_PASSES = "Backstage passes to a TAFKAL80ETC concert";
	private static final String AGED_BRIE = "Aged Brie";
	Item[] items;

	public GildedRose(Item[] items) {
		this.items = items;
	}

	public void updateQuality() {
		for (int i = 0; i < items.length; i++) {

			Item item = items[i];
			decrementSellIn(item);

			updateQuality(item);
			if (item.sellIn < 0) {
				updateQualityWhenNegativeSellIn(item);
			}
		}

	}

	public void updateQuality(Item item) {
		if (!item.name.equals(AGED_BRIE) && !item.name.equals(BACKSTAGE_PASSES)) {
			if (item.quality > 0) {
				if (!item.name.equals(SULFURAS)) {
					item.quality = item.quality - 1;
				}
			}
		} else {
			if (item.quality < 50) {
				item.quality = item.quality + 1;

				if (item.name.equals(BACKSTAGE_PASSES)) {
					if (item.sellIn < 10) {
						if (item.quality < 50) {
							item.quality = item.quality + 1;
						}
					}

					if (item.sellIn < 5) {
						if (item.quality < 50) {
							item.quality = item.quality + 1;
						}
					}
				}
			}
		}

	}

	public void updateQualityWhenNegativeSellIn(Item item) {
		if (item.name.equals(AGED_BRIE)) {
			if (item.quality < 50) {
				item.quality = item.quality + 1;
			}
			return;
		}
		if (item.name.equals(BACKSTAGE_PASSES)) {
			item.quality = 0;
			return;
		}
		if (!item.name.equals(SULFURAS)) {
			if (item.quality > 0) {
				item.quality = item.quality - 1;
			}
			return;
		}
	}

	public void decrementSellIn(Item item) {
		if (!item.name.equals(SULFURAS)) {
			item.sellIn = item.sellIn - 1;
		}
	}
}

@@ -37,16 +37,103 @@
 dRose %7B%0A
+%09private static final int QUALITY_MIN = 0;%0A%09private static final int QUALITY_MAX = 50;%0A
 %09private
@@ -783,33 +783,43 @@
 (item.quality %3E 
-0
+QUALITY_MIN
 ) %7B%0A%09%09%09%09if (!ite
@@ -854,39 +854,35 @@
 %09%09%09%09
-item.quality = item.quality - 1
+decrementQualityByOne(item)
 ;%0A%09%09
@@ -927,49 +927,54 @@
 y %3C 
-50) %7B%0A%09%09%09%09item.quality = item.quality + 1
+QUALITY_MAX) %7B%0A%09%09%09%09incrementQualityByOne(item)
 ;%0A%0A%09
@@ -1064,34 +1064,43 @@
 (item.quality %3C 
-50
+QUALITY_MAX
 ) %7B%0A%09%09%09%09%09%09%09item.
@@ -1099,38 +1099,34 @@
 %09%09%09i
-tem.quality = item.quality + 1
+ncrementQualityByOne(item)
 ;%0A%09%09
@@ -1184,34 +1184,43 @@
 (item.quality %3C 
-50
+QUALITY_MAX
 ) %7B%0A%09%09%09%09%09%09%09item.
@@ -1219,38 +1219,34 @@
 %09%09%09i
-tem.quality = item.quality + 1
+ncrementQualityByOne(item)
 ;%0A%09%09
@@ -1270,32 +1270,218 @@
 %7D%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%7D%0A%0A
+%09public int decrementQualityByOne(Item item) %7B%0A%09%09return item.quality = item.quality - 1;%0A%09%7D%0A%0A%09public int incrementQualityByOne(Item item) %7B%0A%09%09return item.quality = item.quality + 1;%0A%09%7D%0A%0A
 %09public void upd
@@ -1585,49 +1585,54 @@
 y %3C 
-50) %7B%0A%09%09%09%09item.quality = item.quality + 1
+QUALITY_MAX) %7B%0A%09%09%09%09incrementQualityByOne(item)
 ;%0A%09%09
@@ -1711,17 +1711,27 @@
 ality = 
-0
+QUALITY_MIN
 ;%0A%09%09%09ret
@@ -1802,48 +1802,54 @@
 y %3E 
-0) %7B%0A%09%09%09%09item.quality = item.quality - 1
+QUALITY_MIN) %7B%0A%09%09%09%09decrementQualityByOne(item)
 ;%0A%09%09

add constants for quality values