From 3cb923056f80d351c3d54b3559287e958541f7f7 Mon Sep 17 00:00:00 2001 From: oleg Date: Thu, 14 May 2026 03:04:29 +0200 Subject: [PATCH] Lazy implementation solution --- .../main/java/com/gildedrose/GildedRose.java | 131 +++++++++++------- .../java/com/gildedrose/LazyGildedRose.java | 70 ++++++++++ 2 files changed, 149 insertions(+), 52 deletions(-) create mode 100644 Java/src/main/java/com/gildedrose/LazyGildedRose.java diff --git a/Java/src/main/java/com/gildedrose/GildedRose.java b/Java/src/main/java/com/gildedrose/GildedRose.java index 87a3b926..feedde84 100644 --- a/Java/src/main/java/com/gildedrose/GildedRose.java +++ b/Java/src/main/java/com/gildedrose/GildedRose.java @@ -1,62 +1,89 @@ package com.gildedrose; -class GildedRose { +import java.util.Date; +import com.gildedrose.Item; + +public class GildedRose { + + public static final String AGED_BRIE = "Aged Brie"; + public static final String CONCERT = "Backstage passes to a TAFKAL80ETC concert"; + public static final String SULFURAS = "Sulfuras, Hand of Ragnaros"; + public static final String CONJURED = "Conjured"; + public static final int MIN_QUALITY = 0; + public static final int MAX_QUALITY = 50; + + public static final int CONCERT_HOTTEST_DAYS = 6; + public static final int CONCERT_HOT_DAYS = 11; + + + + Item[] items; public GildedRose(Item[] items) { this.items = items; } - public void updateQuality() { - for (int i = 0; i < items.length; i++) { - if (!items[i].name.equals("Aged Brie") - && !items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) { - if (items[i].quality > 0) { - if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) { - items[i].quality = items[i].quality - 1; - } - } - } else { - if (items[i].quality < 50) { - items[i].quality = items[i].quality + 1; - - if (items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) { - if (items[i].sellIn < 11) { - if (items[i].quality < 50) { - items[i].quality = items[i].quality + 1; - } - } - - if (items[i].sellIn < 6) { - if (items[i].quality < 50) { - items[i].quality = items[i].quality + 1; - } - } - } - } - } - - if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) { - items[i].sellIn = items[i].sellIn - 1; - } - - if (items[i].sellIn < 0) { - if (!items[i].name.equals("Aged Brie")) { - if (!items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) { - if (items[i].quality > 0) { - if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) { - items[i].quality = items[i].quality - 1; - } - } - } else { - items[i].quality = items[i].quality - items[i].quality; - } - } else { - if (items[i].quality < 50) { - items[i].quality = items[i].quality + 1; - } - } - } - } + //"Aged Brie" and "Backstage passes": + protected boolean isImproving(Item item) { + return item.name.equals(AGED_BRIE) || item.name.equals(CONCERT); } + + //"Normal" and "Conjured" items: + protected boolean isDegrading(Item item) { + return !item.name.equals(SULFURAS) && !isImproving(item); + } + +// public void updateQuality() { +// for (int i = 0; i < items.length; i++) { +// if (!items[i].name.equals("Aged Brie") +// && !items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) { +// if (items[i].quality > 0) { +// if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) { +// items[i].quality = items[i].quality - 1; +// } +// } +// } else { +// if (items[i].quality < 50) { +// items[i].quality = items[i].quality + 1; +// +// if (items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) { +// if (items[i].sellIn < 11) { +// if (items[i].quality < 50) { +// items[i].quality = items[i].quality + 1; +// } +// } +// +// if (items[i].sellIn < 6) { +// if (items[i].quality < 50) { +// items[i].quality = items[i].quality + 1; +// } +// } +// } +// } +// } +// +// if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) { +// items[i].sellIn = items[i].sellIn - 1; +// } +// +// if (items[i].sellIn < 0) { +// if (!items[i].name.equals("Aged Brie")) { +// if (!items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) { +// if (items[i].quality > 0) { +// if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) { +// items[i].quality = items[i].quality - 1; +// } +// } +// } else { +// items[i].quality = items[i].quality - items[i].quality; +// } +// } else { +// if (items[i].quality < 50) { +// items[i].quality = items[i].quality + 1; +// } +// } +// } +// } +// } } diff --git a/Java/src/main/java/com/gildedrose/LazyGildedRose.java b/Java/src/main/java/com/gildedrose/LazyGildedRose.java new file mode 100644 index 00000000..4f722661 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/LazyGildedRose.java @@ -0,0 +1,70 @@ +package java.com.gildedrose; + +import com.gildedrose.Item; + +/* If the amout of items is substantially bigger than day-to-day item removing/adding, + we can calculate item quality on the fly, + instead of constantly decrementing every item quality, by using timestamps: + + */ +public class LazyGildedRose extends com.gildedrose.GildedRose { + + Long startUnixTime; + + public LazyGildedRose(Item[] items) { + super(items); + this.startUnixTime = System.currentTimeMillis()/1000; + } + + //Adjust item quality as if item was placed in the store from day one: + private int getDaysSinceStart() { + var timePassed = System.currentTimeMillis() / 1000 - startUnixTime; + return Math.toIntExact(timePassed) / 24 / 3600; + } + + private void adjustItemQuality(Item item, int daysPassed) { + switch(item.name) { + case AGED_BRIE, CONCERT: item.quality += daysPassed; + break; + case SULFURAS:; + break; + case CONJURED: item.quality -= daysPassed * 2; + break; + default: item.quality -= daysPassed; + } + } + + public void addItem(Item item) { + var daysPassedSinceStart = getDaysSinceStart(); + adjustItemQuality(item, -daysPassedSinceStart); + item.sellIn += daysPassedSinceStart; + } + + public void sellItem(Item item) { + var daysPassedSinceStart = getDaysSinceStart(); + adjustItemQuality(item, daysPassedSinceStart); + + var daysLeft = daysPassedSinceStart - item.sellIn; + if(item.name.equals(CONCERT)) { + if(daysLeft < 0) { + item.quality = 0; + } else { + adjustItemQuality(item, Math.max(CONCERT_HOT_DAYS - daysLeft, 0) + Math.max(CONCERT_HOTTEST_DAYS - daysLeft, 0)); + } + } + //For degrading and outdated items, adjust quality one more time: + else if(isDegrading(item) && daysPassedSinceStart > item.sellIn) { + adjustItemQuality(item, daysLeft); + } + normalizeQuality(item); + } + + private void normalizeQuality(Item item) { + if (item.quality < MIN_QUALITY) { + item.quality = MIN_QUALITY; + } + if (item.quality > MAX_QUALITY) { + item.quality = MAX_QUALITY; + } + } +}