I've refactored my previous inventory system, and added a few features like removing items from the Inventory
class, easily obtaining the current selected item through Inventory.GetSelectedItem
, and some things have been renamed as well. My concerns are once again:
- Is this the correct usage of F#'s type/class system? Should this be made in a more "functional" way?
- Am I using getters/setters correctly? Do I need to declare the mutable variables internalName in my types, or is this automatically done?
- Is my code properly styled?
Item.fs
namespace InventorySystem
open System
/// <summary>
/// Represents an item. This type is only
/// with the Inventory type.
/// </summary>
/// <param name="name">The item's name.</param>
/// <param name="count">The amount of this specific item.</param>
type Item(name: string, count: int) =
let mutable _count = count
let mutable _name = name
member this.Name
with get() = _name
and set(value: string) = _name <- value
member this.Count
with get() = _count
and set(value: int) = _count <- value
/// <summary>
/// Increment or decrement the how much of this
/// specific item you have.
/// </summary>
/// <param name="amount">The amount to change by.</param>
member this.ChangeCount(amount: int) =
if this.Count + amount >= 0 then
this.Count <- this.Count + amount
else
printfn "You cannot have less than zero items."
/// <summary>
/// Return this type in the following format:
/// Name={item name}, Count={item count}.
/// </summary>
override this.ToString() =
String.Format("Name={0}, Count={1}", this.Name, this.Count)
Inventory.fs
namespace InventorySystem
open System
/// <summary>
/// This type represents an inventory, containing items.
/// </summary>
/// <param name="items">A list of items.</param>
type Inventory(items: Item list) =
let mutable _items = items
let mutable _selectedItem = 0
member this.Items
with get() = _items
and set(value: Item list) = _items <- value
member this.SelectedItem
with get() = _selectedItem
and set(value: int) = _selectedItem <- value
/// <summary>
/// Add an item to the list of items.
/// </summary>
/// <param name="item">The item to add.</param>
member this.AddItem(item: Item) =
this.Items <- item :: this.Items
/// <summary>
/// Remove an item from the inventory based
/// the item's index.
/// </summary>
/// <param name="itemIndex">The index of the item to remove.</param>
member this.RemoveItem(itemIndex: int) =
let mutable resultList = []
for index = 0 to this.Items.Length - 1 do
if index <> itemIndex then
resultList <- this.Items.[index] :: resultList
this.Items <- resultList
/// <summary>
/// Change the currently selected item.
/// </summary>
/// <param name="amount">The amount to change the selected item by.</param>
member this.ChangeSelectedItem(amount: int) =
if this.SelectedItem + amount < this.Items.Length && this.SelectedItem + amount >= 0 then
this.SelectedItem <- this.SelectedItem + amount
else
printfn "The currently selected item must stay in the range 0 -> Items.Length."
/// <summary>
/// Get the currently selected item.
/// </summary>
member this.GetSelectedItem() =
this.Items.[this.SelectedItem]
/// <summary>
/// Return this type in the following format:
/// Items={items}, Selected item={selected item}
/// </summary>
override this.ToString() =
String.Format("Items={0}, Selected Item={1}", this.Items, this.Items.[this.SelectedItem])
Test.fs
open System
open InventorySystem
[<EntryPoint>]
let main argv =
let myInventory = new Inventory([
new Item("Sword of Death", 1);
new Item("Gold", 1)
])
myInventory.AddItem(new Item("Silver", 5))
myInventory.AddItem(new Item("Copper", 10))
Console.WriteLine(myInventory)
myInventory.RemoveItem(1)
Console.WriteLine(myInventory)
Console.WriteLine(myInventory.GetSelectedItem())
myInventory.ChangeSelectedItem(1)
Console.WriteLine(myInventory.GetSelectedItem())
Console.WriteLine(myInventory)
Console.ReadKey()
0
item_id -> item
. That's just my personal 'feeling', so take it with a huge grain of salt :) Oh, also the indexer on a linked list isO(n)
, as is any other index-based access, hence my reservations about the indexed approach overall... \$\endgroup\$