Skip to content

Commit

Permalink
Merge pull request #40 from BoBoBaSs84/39-refac-implement-the-members…
Browse files Browse the repository at this point in the history
…-of-inotifypropertychanged-interface

Done.
  • Loading branch information
BoBoBaSs84 committed Sep 3, 2022
2 parents f9181d3 + e3ff043 commit 3d0987c
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 144 deletions.
File renamed without changes.
66 changes: 66 additions & 0 deletions Classes/BaseTypes/NotifyBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Los.Santos.Dope.Wars.Classes.BaseTypes;

/// <summary>
/// The <see cref="NotifyBase"/> class.
/// <remarks>
/// The <see cref="NotifyBase"/> class implements the following interfaces:
/// <list type="bullet">
/// <item>The members of the <see cref="INotifyPropertyChanged"/> interface.</item>
/// <item>The members of the <see cref="INotifyPropertyChanging"/> interface.</item>
/// </list>
/// </remarks>
/// </summary>
public abstract class NotifyBase : INotifyPropertyChanged, INotifyPropertyChanging
{
/// <summary>
/// Sets a new value for a property and notifies about the change.
/// </summary>
/// <remarks>
/// Will throw an exception of type <see cref="ArgumentNullException"/> if <paramref name="propertyName"/> is <see langword="null"/>.
/// </remarks>
/// <typeparam name="T"></typeparam>
/// <param name="field">The referenced field.</param>
/// <param name="newValue">The new value for the property.</param>
/// <param name="propertyName">The property name.</param>
/// <exception cref="ArgumentNullException"></exception>
protected void SetProperty<T>(ref T field, T newValue, [CallerMemberName] string? propertyName = null)
{
if (propertyName is null)
throw new ArgumentNullException(nameof(propertyName));

if (!Equals(field, newValue))
{
RaisePropertyChanging(propertyName);
field = newValue;
RaisePropertyChanged(propertyName);
}
}

/// <inheritdoc/>
public event PropertyChangedEventHandler? PropertyChanged;
/// <inheritdoc/>
public event PropertyChangingEventHandler? PropertyChanging;

/// <summary>
/// The method is used to raise the 'property changed' event.
/// </summary>
/// <remarks>
/// The calling member's name will be used as the parameter.
/// </remarks>
/// <param name="propertyName">The name of the property, can be <see langword="null"/>.</param>
protected virtual void RaisePropertyChanged([CallerMemberName] string? propertyName = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

/// <summary>
/// The method is used to raise the 'property changing' event.
/// </summary>
/// <remarks>
/// The calling member's name will be used as the parameter.
/// </remarks>
/// <param name="propertyName">The name of the property, can be <see langword="null"/>.</param>
protected virtual void RaisePropertyChanging([CallerMemberName] string? propertyName = null) =>
PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
78 changes: 31 additions & 47 deletions Classes/Drug.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,22 @@
using System.Xml.Serialization;
using Los.Santos.Dope.Wars.Classes.BaseTypes;
using System.ComponentModel;
using System.Xml.Serialization;

namespace Los.Santos.Dope.Wars.Classes;

/// <summary>
/// The <see cref="Drug"/> class is the root element for the drug class.
/// </summary>
[XmlRoot(ElementName = nameof(Drug), IsNullable = false)]
public class Drug
public class Drug : NotifyBase
{
#region fields
private int _quantity;
private int quantity;
#endregion

#region ctor
/// <summary>
/// Empty constructor for <see cref="Drug"/>
/// </summary>
public Drug()
{
Name = string.Empty;
Description = string.Empty;
CurrentPrice = default;
AveragePrice = default;
PurchasePrice = default;
Quantity = default;
}

/// <summary>
/// The <see cref="Enums.DrugType"/> constructor for <see cref="Drug"/>
/// </summary>
/// <param name="drugType"></param>
/// <param name="description"></param>
/// <param name="marketValue"></param>
public Drug(Enums.DrugType drugType, string description, int marketValue)
{
Name = drugType.GetName();
Description = description;
CurrentPrice = default;
AveragePrice = marketValue;
PurchasePrice = default;
Quantity = default;
}

/// <summary>
/// The <see cref="string"/> constructor for <see cref="Drug"/>
/// Initializes a new instance of the <see cref="Drug"/> class.
/// </summary>
/// <param name="drugName"></param>
/// <param name="description"></param>
Expand All @@ -56,54 +29,65 @@ public Drug(string drugName, string description, int marketValue)
AveragePrice = marketValue;
PurchasePrice = default;
Quantity = default;
PropertyChanged += OnDrugPropertyChanged;
}
#endregion

#region properties
/// <summary>
/// The <see cref="Name"/> property
/// The <see cref="Name"/> property is the name of the drug.
/// </summary>
[XmlText]
public string Name { get; set; }

/// <summary>
/// The <see cref="Description"/> property
/// The <see cref="Description"/> property is the description of the drug.
/// </summary>
[XmlIgnore]
public string Description { get; set; }

/// <summary>
/// The <see cref="CurrentPrice"/> property
/// The <see cref="CurrentPrice"/> property is the current price of the drug.
/// </summary>
[XmlIgnore]
public int CurrentPrice { get; set; }

/// <summary>
/// The <see cref="AveragePrice"/> property
/// The <see cref="AveragePrice"/> property is the normal market price of the drug.
/// </summary>
[XmlIgnore]
public int AveragePrice { get; set; }

/// <summary>
/// The <see cref="PurchasePrice"/> property
/// The <see cref="PurchasePrice"/> property is the price the drug has been purchased.
/// </summary>
[XmlAttribute(AttributeName = nameof(PurchasePrice))]
public int PurchasePrice { get; set; }

/// <summary>
/// The <see cref="Quantity"/> property, if the quantity gets set to 0 the <see cref="PurchasePrice"/> property goes 0 too
/// The <see cref="Quantity"/> property is the amount of the drug.
/// </summary>
[XmlAttribute(AttributeName = nameof(Quantity))]
public int Quantity
public int Quantity { get => quantity; set => SetProperty(ref quantity, value); }
#endregion

#region private methods
/// <summary>
/// The event trigger method if a property that notifies has changed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnDrugPropertyChanged(object sender, PropertyChangedEventArgs e)
{
get => _quantity;
set
if (sender is not Drug drug)
return;
if (e is null)
return;

if (Equals(e.PropertyName, nameof(drug.Quantity)))
{
_quantity = value;
if (value.Equals(0))
{
PurchasePrice = value;
}
if (Equals(drug.Quantity, 0))
drug.PurchasePrice = Quantity;
}
}
#endregion
Expand Down
59 changes: 28 additions & 31 deletions Classes/PlayerStash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,55 @@
namespace Los.Santos.Dope.Wars.Classes;

/// <summary>
/// The <see cref="DealerStash"/> class for player types, inherits from <see cref="Stash"/>
/// The <see cref="PlayerStash"/> class.
/// </summary>
/// <remarks>
/// Inherits from the <see cref="Stash"/> base class.
/// Implements the members of the <see cref="IPlayerStash"/> interface.
/// </remarks>
public class PlayerStash : Stash, IPlayerStash
{
#region ctor
/// <summary>
/// The standard constructor for the <see cref="PlayerStash"/> class
/// Initializes a new instance of the <see cref="PlayerStash"/> class.
/// </summary>
public PlayerStash()
{
Drugs = new();
}
#endregion
public PlayerStash() => Drugs = new();

#region IPlayerStash members
/// <inheritdoc/>
/// <inheritdoc cref="IPlayerStash.BuyDrug(string, int, int)"/>
public void BuyDrug(string drugName, int drugQuantity, int drugPrice)
{
Drug drug = Drugs.Where(x => x.Name.Equals(drugName, StringComparison.Ordinal)).SingleOrDefault();

if (drug.Quantity.Equals(0))
drug.PurchasePrice = drugPrice;
else
drug.PurchasePrice = ((drug.Quantity * drug.PurchasePrice) + (drugQuantity * drugPrice)) / (drug.Quantity + drugQuantity);

CalculateNewPurchasePrice(drugName, drugQuantity, drugPrice);
AddToStash(drugName, drugQuantity);

Game.Player.Money -= drugPrice * drugQuantity;
}
/// <inheritdoc/>

/// <inheritdoc cref="IPlayerStash.MoveIntoInventory(string, int, int)"/>
public void MoveIntoInventory(string drugName, int drugQuantity, int drugPrice)
{
Drug drug = Drugs.Where(x => x.Name.Equals(drugName, StringComparison.Ordinal)).SingleOrDefault();

if (drug.Quantity.Equals(0))
drug.PurchasePrice = drugPrice;
else
drug.PurchasePrice = ((drug.Quantity * drug.PurchasePrice) + (drugQuantity * drugPrice)) / (drug.Quantity + drugQuantity);

CalculateNewPurchasePrice(drugName, drugQuantity, drugPrice);
AddToStash(drugName, drugQuantity);
}
/// <inheritdoc/>
/// <inheritdoc cref="IPlayerStash.SellDrug(string, int, int)"/>
public void SellDrug(string drugName, int drugQuantity, int drugPrice)
{
RemoveFromStash(drugName, drugQuantity);

Game.Player.Money += drugPrice * drugQuantity;
}
/// <inheritdoc/>
public void TakeFromInventory(string drugName, int drugQuantity)
{
/// <inheritdoc cref="IPlayerStash.TakeFromInventory(string, int)"/>
public void TakeFromInventory(string drugName, int drugQuantity) =>
RemoveFromStash(drugName, drugQuantity);
}
#endregion

/// <summary>
/// The method calculaties the new purchase price, when adding drug into the player stash.
/// </summary>
/// <param name="drugName">The name of the drug.</param>
/// <param name="drugQuantity">The quantity / amount of the drug.</param>
/// <param name="drugPrice">The current price of the drug.</param>
private void CalculateNewPurchasePrice(string drugName, int drugQuantity, int drugPrice)
{
Drug drug = Drugs.Where(x => x.Name.Equals(drugName, StringComparison.Ordinal)).Single();
drug.PurchasePrice = drug.Quantity.Equals(0) ? drugPrice
: ((drug.Quantity * drug.PurchasePrice) + (drugQuantity * drugPrice)) / (drug.Quantity + drugQuantity);
}
}
32 changes: 16 additions & 16 deletions Interfaces/IPlayerStash.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
namespace Los.Santos.Dope.Wars.Interfaces;

/// <summary>
/// The <see cref="IPlayerStash"/> interface, provides methods specifically for the player
/// The <see cref="IPlayerStash"/> interface, provides th epublic methods for the player stash.
/// </summary>
public interface IPlayerStash
{
/// <summary>
/// The <see cref="BuyDrug(string, int, int)"/> method is used to manipulate the player stash
/// The method is used to manipulate the player stash by adding drugs through a buy transaction.
/// </summary>
/// <param name="drugName"></param>
/// <param name="drugQuantity"></param>
/// <param name="drugPrice"></param>
/// <param name="drugName">The name of the drug.</param>
/// <param name="drugQuantity">The quantity / amount of the drug.</param>
/// <param name="drugPrice">The current price of the drug.</param>
void BuyDrug(string drugName, int drugQuantity, int drugPrice);

/// <summary>
/// The <see cref="SellDrug(string, int, int)"/> method is used to manipulate the player stash
/// The method is used to manipulate the player stash by adding drugs through a sell transaction.
/// </summary>
/// <param name="drugName"></param>
/// <param name="drugQuantity"></param>
/// <param name="drugPrice"></param>
/// <param name="drugName">The name of the drug.</param>
/// <param name="drugQuantity">The quantity / amount of the drug.</param>
/// <param name="drugPrice">The current price of the drug.</param>
void SellDrug(string drugName, int drugQuantity, int drugPrice);

/// <summary>
/// The <see cref="MoveIntoInventory(string, int, int)"/> method is used to make transfers into player inventories
/// The method is used to make transfers into player inventories by adding drug into a inventory.
/// </summary>
/// <param name="drugName"></param>
/// <param name="drugQuantity"></param>
/// <param name="drugPrice"></param>
/// <param name="drugName">The name of the drug.</param>
/// <param name="drugQuantity">The quantity / amount of the drug.</param>
/// <param name="drugPrice">The current price of the drug.</param>
void MoveIntoInventory(string drugName, int drugQuantity, int drugPrice);

/// <summary>
/// The <see cref="TakeFromInventory(string, int)"/> method is used to make transfers from player inventories
/// The method is used to make transfers into player inventories by removing drug from a inventory.
/// </summary>
/// <param name="drugName"></param>
/// <param name="drugQuantity"></param>
/// <param name="drugName">The name of the drug.</param>
/// <param name="drugQuantity">The quantity / amount of the drug.</param>
void TakeFromInventory(string drugName, int drugQuantity);
}
Loading

0 comments on commit 3d0987c

Please sign in to comment.