Skip to content

Commit

Permalink
Updated Gilded Rose Typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
zhendrikse committed Oct 16, 2024
1 parent 94cea0d commit c4365e5
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 128 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import { Item, GildedRose, Sulfuras, BackstagePass, AgedBrie } from '../src/gilded-rose';
import { Item, GildedRose } from '../src/gilded-rose';
import {verify, verifyAsJson} from "approvals/lib/Providers/Jest/JestApprovals";

function convert_items_to_string(items = [] as Array<Item>) {
Expand All @@ -19,22 +19,22 @@ describe('Gilded Rose', () => {
new Item("Foo", -1, 0),
new Item("Foo", -10, 2),
new Item("Foo", -10, 51),
new Sulfuras(-1, 1),
new Sulfuras(0, 10),
new AgedBrie(-1, 0),
new AgedBrie(0, 50),
new BackstagePass(1, 0),
new BackstagePass(0, 0),
new BackstagePass(11, 48),
new BackstagePass(11, 49),
new BackstagePass(10, 48),
new BackstagePass(10, 49),
new BackstagePass(10, 50),
new BackstagePass(5, 47),
new BackstagePass(5, 48),
new BackstagePass(5, 49),
new BackstagePass(6, 47),
new BackstagePass(0, 50),
new Item("Sulfuras, Hand of Ragnaros", -1, 1),
new Item("Sulfuras, Hand of Ragnaros", 0, 10),
new Item("Aged Brie", -1, 0),
new Item("Aged Brie", 0, 50),
new Item("Backstage passes to a TAFKAL80ETC concert", 1, 0),
new Item("Backstage passes to a TAFKAL80ETC concert", 0, 0),
new Item("Backstage passes to a TAFKAL80ETC concert", 11, 48),
new Item("Backstage passes to a TAFKAL80ETC concert", 11, 49),
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 48),
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49),
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 50),
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 47),
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 48),
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49),
new Item("Backstage passes to a TAFKAL80ETC concert", 6, 47),
new Item("Backstage passes to a TAFKAL80ETC concert", 0, 50),
];
const gildedRose = new GildedRose(items);
const updated_items = gildedRose.updateQuality();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

import internal from "stream";

export class Item {
name: string = "";
sellIn: number = 0;
Expand All @@ -13,79 +11,56 @@ export class Item {
this.quality = quality;
}

public incrementQuality() {
if (this.quality < 50)
this.quality = this.quality + 1;
}

public decrementQuality() {
if (this.quality > 0)
this.quality = this.quality - 1;
}

public update() {
this.decrementQuality();
this.sellIn = this.sellIn - 1;
this.decrementQuality();
}

public toString(): string {
return "name: " + this.name + ", sellIn: " + this.sellIn + ", quality: " + this.quality
}
}

export class AgedBrie extends Item {
constructor(sellIn: number, quality: number) {
super("Aged Brie", sellIn, quality);
}

public update() {
this.incrementQuality();
this.sellIn = this.sellIn - 1;
this.incrementQuality();
}
}

export class Sulfuras extends Item {
constructor(sellIn: number, quality: number) {
super("Sulfuras, Hand of Ragnaros", sellIn, quality);
}

public update() {
}
}

export class BackstagePass extends Item {
constructor(sellIn: number, quality: number) {
super("Backstage passes to a TAFKAL80ETC concert", sellIn, quality);
}

public update() {
if (this.quality < 50)
this.quality = this.quality + 1;
if (this.sellIn < 11)
if (this.quality < 50)
this.quality = this.quality + 1;
if (this.sellIn < 6)
if (this.quality < 50)
this.quality = this.quality + 1;

this.sellIn = this.sellIn - 1;

if (this.sellIn < 0)
this.quality = 0;
}
}

export class GildedRose {
items: Array<Item>;

constructor(items = [] as Array<Item>) {
this.items = items;
}

public updateQuality(): Array<Item> {
this.items.forEach(item => item.update());
updateQuality() {
this.items.forEach(item => this.updateItem(item));
return this.items;
}

private updateItem(item: Item) {
if (item.name == 'Aged Brie') {
if (item.quality < 50)
item.quality = item.quality + 1;
item.sellIn = item.sellIn - 1;
if (item.sellIn < 0)
if (item.quality < 50)
item.quality = item.quality + 1;
} else {
if (item.name == 'Backstage passes to a TAFKAL80ETC concert') {
if (item.quality < 50) {
item.quality = item.quality + 1;
if (item.sellIn < 11)
if (item.quality < 50)
item.quality = item.quality + 1;
if (item.sellIn < 6)
if (item.quality < 50)
item.quality = item.quality + 1;
}
item.sellIn = item.sellIn - 1;
if (item.sellIn < 0)
item.quality = item.quality - item.quality;
} else if (item.name == 'Sulfuras, Hand of Ragnaros') {
// intentionally left blank
} else {
if (item.quality > 0)
item.quality = item.quality - 1;
item.sellIn = item.sellIn - 1;
if (item.sellIn < 0)
if (item.quality > 0)
item.quality = item.quality - 1;
}
}
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import { Item, Shop, Sulfuras, AgedBrie, BackstagePass } from '../src/gilded-rose';
import { Item, GildedRose, Sulfuras, BackstagePass, AgedBrie } from '../src/gilded-rose';
import {verify, verifyAsJson} from "approvals/lib/Providers/Jest/JestApprovals";

function convert_items_to_string(items = [] as Array<Item>) {
Expand Down Expand Up @@ -34,9 +34,9 @@ describe('Gilded Rose', () => {
new BackstagePass(5, 48),
new BackstagePass(5, 49),
new BackstagePass(6, 47),
new BackstagePass(0, 50),
new BackstagePass(0, 50),
];
const gildedRose = new Shop(items);
const gildedRose = new GildedRose(items);
const updated_items = gildedRose.updateQuality();
verify(convert_items_to_string(updated_items));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,97 +1,91 @@
'use strict';

import internal from "stream";

export class Item {
name: string = "";
sellIn: number = 0;
quality: number = 0;

constructor(name:string , sellIn: number, quality: number) {
constructor(name: string, sellIn: number, quality: number) {
this.name = name;
this.sellIn = sellIn;
this.quality = quality;
}

updateQualityAfterSellIn() {
if (this.quality > 0)
this.quality = this.quality - 1;
public incrementQuality() {
if (this.quality < 50)
this.quality = this.quality + 1;
}

updateQualityBeforeSellIn() {
public decrementQuality() {
if (this.quality > 0)
this.quality = this.quality - 1;
}

incrementQuality() {
if (this.quality < 50)
this.quality = this.quality + 1;
}

updateProduct() {
this.updateQualityBeforeSellIn();
public update() {
this.decrementQuality();
this.sellIn = this.sellIn - 1;
this.updateQualityAfterSellIn();
this.decrementQuality();
}

toString() {
public toString(): string {
return "name: " + this.name + ", sellIn: " + this.sellIn + ", quality: " + this.quality
}
}

export class BackstagePass extends Item {
export class AgedBrie extends Item {
constructor(sellIn: number, quality: number) {
super('Backstage passes to a TAFKAL80ETC concert', sellIn, quality);
super("Aged Brie", sellIn, quality);
}

updateQualityBeforeSellIn() {
if (this.quality < 50) {
this.quality = this.quality + 1;
if (this.sellIn < 11)
super.incrementQuality();
if (this.sellIn < 6)
super.incrementQuality();
}
}

updateQualityAfterSellIn() {
if (this.sellIn < 0)
this.quality = 0;
public update() {
this.incrementQuality();
this.sellIn = this.sellIn - 1;
this.incrementQuality();
}
}

export class AgedBrie extends Item {
export class Sulfuras extends Item {
constructor(sellIn: number, quality: number) {
super('Aged Brie', sellIn, quality);
super("Sulfuras, Hand of Ragnaros", sellIn, quality);
}

updateQualityBeforeSellIn() {
super.incrementQuality();
}

updateQualityAfterSellIn() {
super.incrementQuality();
public update() {
}
}

export class Sulfuras extends Item {
export class BackstagePass extends Item {
constructor(sellIn: number, quality: number) {
super('Sulfuras, Hand of Ragnaros', sellIn, quality);
super("Backstage passes to a TAFKAL80ETC concert", sellIn, quality);
}

updateProduct() {
public update() {
if (this.quality < 50)
this.quality = this.quality + 1;
if (this.sellIn < 11)
if (this.quality < 50)
this.quality = this.quality + 1;
if (this.sellIn < 6)
if (this.quality < 50)
this.quality = this.quality + 1;

this.sellIn = this.sellIn - 1;

if (this.sellIn < 0)
this.quality = 0;
}
}

export class Shop {
export class GildedRose {
items: Array<Item>;

constructor(items = [] as Array<Item>) {
this.items = items;
}

updateQuality() {
for (var i = 0; i < this.items.length; i++)
this.items[i].updateProduct();

public updateQuality(): Array<Item> {
this.items.forEach(item => item.update());
return this.items;
}
}

0 comments on commit c4365e5

Please sign in to comment.