Browse Source

Add project files.

master
Holly McFarland 1 year ago
parent
commit
779e540247
12 changed files with 409 additions and 0 deletions
  1. +43
    -0
      COMP-10068 Assignment 5.sln
  2. +8
    -0
      Problem1/Class1.cs
  3. +7
    -0
      Problem1/Problem1.csproj
  4. +8
    -0
      Problem2/Class1.cs
  5. +7
    -0
      Problem2/Problem2.csproj
  6. +8
    -0
      Problem3/Class1.cs
  7. +7
    -0
      Problem3/Problem3.csproj
  8. +67
    -0
      Problem4/Art.cs
  9. +110
    -0
      Problem4/Auctioneer.cs
  10. +99
    -0
      Problem4/Buyer.cs
  11. +7
    -0
      Problem4/Problem4.csproj
  12. +38
    -0
      Problem4/Unsubscriber.cs

+ 43
- 0
COMP-10068 Assignment 5.sln View File

@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31105.61
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Problem2", "Problem2\Problem2.csproj", "{AA252DB0-98EE-4DF6-9270-F1F0C5C43AA0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Problem3", "Problem3\Problem3.csproj", "{CC54CBF8-B944-475F-AC14-CC9D74B5C047}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Problem4", "Problem4\Problem4.csproj", "{B8C9240E-2204-4138-88F6-2291BF9F8E07}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Problem1", "Problem1\Problem1.csproj", "{212B5B50-DB14-40AD-A04D-9AD9AF24E650}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AA252DB0-98EE-4DF6-9270-F1F0C5C43AA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AA252DB0-98EE-4DF6-9270-F1F0C5C43AA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AA252DB0-98EE-4DF6-9270-F1F0C5C43AA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AA252DB0-98EE-4DF6-9270-F1F0C5C43AA0}.Release|Any CPU.Build.0 = Release|Any CPU
{CC54CBF8-B944-475F-AC14-CC9D74B5C047}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CC54CBF8-B944-475F-AC14-CC9D74B5C047}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC54CBF8-B944-475F-AC14-CC9D74B5C047}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC54CBF8-B944-475F-AC14-CC9D74B5C047}.Release|Any CPU.Build.0 = Release|Any CPU
{B8C9240E-2204-4138-88F6-2291BF9F8E07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B8C9240E-2204-4138-88F6-2291BF9F8E07}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8C9240E-2204-4138-88F6-2291BF9F8E07}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8C9240E-2204-4138-88F6-2291BF9F8E07}.Release|Any CPU.Build.0 = Release|Any CPU
{212B5B50-DB14-40AD-A04D-9AD9AF24E650}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{212B5B50-DB14-40AD-A04D-9AD9AF24E650}.Debug|Any CPU.Build.0 = Debug|Any CPU
{212B5B50-DB14-40AD-A04D-9AD9AF24E650}.Release|Any CPU.ActiveCfg = Release|Any CPU
{212B5B50-DB14-40AD-A04D-9AD9AF24E650}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E7E6C061-7D4D-4FF7-811B-9C6AB1EC983E}
EndGlobalSection
EndGlobal

+ 8
- 0
Problem1/Class1.cs View File

@ -0,0 +1,8 @@
using System;
namespace Problem1
{
public class Class1
{
}
}

+ 7
- 0
Problem1/Problem1.csproj View File

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

+ 8
- 0
Problem2/Class1.cs View File

@ -0,0 +1,8 @@
using System;
namespace Problem2
{
public class Class1
{
}
}

+ 7
- 0
Problem2/Problem2.csproj View File

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

+ 8
- 0
Problem3/Class1.cs View File

@ -0,0 +1,8 @@
using System;
namespace Problem3
{
public class Class1
{
}
}

+ 7
- 0
Problem3/Problem3.csproj View File

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

+ 67
- 0
Problem4/Art.cs View File

@ -0,0 +1,67 @@
using System;
// I Scott McFarland, 000778232, certify that this material is my original work. No other person's work has been used without due acknowledgement. I have not made my work available to anyone else.
namespace Problem4
{
/// <summary>
/// Art for sale at the aution
/// </summary>
public class Art
{
/// <summary>
/// The year the art was created
/// </summary>
public int Year { get; set; }
/// <summary>
/// The starting price for the aution
/// </summary>
public int Price { get; set; }
/// <summary>
/// Number of bids that have been placed on this piece of artwork
/// </summary>
public int NumBids { get; set; }
/// <summary>
/// The current highest bidder on the art
/// </summary>
public IObserver<Art> HighBidder { get; set; }
/// <summary>
/// Place a bid on the object
/// </summary>
/// <param name="price">The new price of the art</param>
/// <param name="bidder">The buyer placing the bid</param>
/// <returns>Whether the bid was sucessfully placed</returns>
public bool Bid(int price, IObserver<Art> bidder)
{
if (price <= this.Price)
{
throw new ArgumentException("New price must be greater than current price");
}
// Fail to place additional bids after 5 or if the current winning buyer
// is trying to bid again
if (this.NumBids == 5 || this.HighBidder == bidder)
{
return false;
}
this.Price = price;
this.NumBids++;
this.HighBidder = bidder;
return true;
}
public Art(int year, int price)
{
this.Year = year;
this.Price = price;
this.NumBids = 0;
this.HighBidder = null;
}
}
}

+ 110
- 0
Problem4/Auctioneer.cs View File

@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.Text;
// I Scott McFarland, 000778232, certify that this material is my original work. No other person's work has been used without due acknowledgement. I have not made my work available to anyone else.
namespace Problem4
{
/// <summary>
/// An auctioneer to run the auction
/// </summary>
public class Auctioneer : IObservable<Art>
{
/// <summary>
/// Buyers present at the aution
/// </summary>
private List<IObserver<Art>> buyers;
/// <summary>
/// Art for sale
/// </summary>
private List<Art> allArt;
/// <summary>
/// Inform a particular buyer about a piece of art
/// </summary>
/// <param name="piece">The piece of art</param>
/// <param name="buyer">The buyer</param>
/// <returns>Whether the buyer bid on the art</returns>
public bool ShowToBuyer(Art piece, IObserver<Art> buyer)
{
int numBids = piece.NumBids;
buyer.OnNext(piece);
return numBids != piece.NumBids;
}
/// <summary>
/// Inform all buyers about a piece of art
/// Run when art is added or bid on
/// </summary>
/// <param name="piece">The piece of art to inform buyers about</param>
public void Update(Art piece)
{
// If this is a new piece of art, add it to the list
if (!allArt.Contains(piece)) {
allArt.Add(piece);
}
foreach (IObserver<Art> buyer in buyers.ToArray())
{
// If the buyer bid on the piece
if (ShowToBuyer(piece, buyer))
{
// If the buyer won the piece
if (piece.NumBids == 5)
{
// Remove the art and buyer from internal lists
allArt.Remove(piece);
buyer.OnCompleted();
buyers.Remove(buyer);
// We no longer have to worry about showing off this art
return;
}
// If the buyer didn't win the piece, end the current Update and start a new one
Update(piece);
return;
}
}
}
/// <summary>
/// Add a buyer to the aution
/// </summary>
/// <param name="buyer">The buyer being added</param>
/// <returns>A handle by which the buyer can unsubscribe</returns>
public IDisposable Subscribe(IObserver<Art> buyer)
{
if (!buyers.Contains(buyer))
{
buyers.Add(buyer);
}
return new Unsubscriber<Art>(buyers, buyer);
}
/// <summary>
/// Inform a buyer about all available art
/// Used to catch up new subscribers
/// </summary>
/// <param name="buyer">The buyer to inform</param>
public void CatchUp(IObserver<Art> buyer)
{
foreach (Art piece in allArt.ToArray())
{
// Update any piece the new buyer bids on
if (ShowToBuyer(piece, buyer))
{
Update(piece);
}
}
}
public Auctioneer()
{
buyers = new List<IObserver<Art>>();
allArt = new List<Art>();
}
}
}

+ 99
- 0
Problem4/Buyer.cs View File

@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Text;
// I Scott McFarland, 000778232, certify that this material is my original work. No other person's work has been used without due acknowledgement. I have not made my work available to anyone else.
namespace Problem4
{
public class Buyer : IObserver<Art>
{
/// <summary>
/// Handle for unsubscribing from the auction
/// </summary>
private IDisposable unsubscriber;
/// <summary>
/// The maximum this buyer can spend
/// </summary>
public int budget { get; set; }
/// <summary>
/// The art the buyer has purchased, if any
/// </summary>
public Art purchased { get; set; }
/// <summary>
/// The most recent art that the buyer bid on, used for setting `purchased`
/// </summary>
private Art recentBid { get; set; }
/// <summary>
/// Run if the auctioneer sends an error message
/// Stubbed out, never occurs
/// </summary>
public virtual void OnError(Exception error)
{
// Stub
}
/// <summary>
/// Run when the buyer wins a bid and is removed from the auction
/// </summary>
public virtual void OnCompleted()
{
purchased = recentBid;
}
/// <summary>
/// Run whenever a piece of art is updated (added to the auction or bid on)
///
/// The default buyer wants to buy any available art for the minimum price,
/// and will voluntarily leave the auction if any piece of art becomes too
/// expensive for their budget
///
/// Other buyer classes could inheret from this one and override this behaviour
/// </summary>
/// <param name="piece">The art for sale</param>
public virtual void OnNext(Art piece)
{
if (budget > piece.Price)
{
// Record the bid if it was placed sucessfully
if (piece.Bid(piece.Price + 1, this))
{
recentBid = piece;
}
}
else
{
Unsubscribe();
}
}
/// <summary>
/// Subscribe to an auctioneer's auction
/// </summary>
/// <param name="auctioneer">The auctioneer to subscribe to</param>
public virtual void Subscribe(Auctioneer auctioneer)
{
unsubscriber = auctioneer.Subscribe(this);
auctioneer.CatchUp(this);
}
/// <summary>
/// Unsubscribe from the auction
/// </summary>
public virtual void Unsubscribe()
{
unsubscriber.Dispose();
}
public Buyer(int budget)
{
this.budget = budget;
}
}
}

+ 7
- 0
Problem4/Problem4.csproj View File

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

+ 38
- 0
Problem4/Unsubscriber.cs View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Text;
// I Scott McFarland, 000778232, certify that this material is my original work. No other person's work has been used without due acknowledgement. I have not made my work available to anyone else.
namespace Problem4
{
internal class Unsubscriber<Art> : IDisposable
{
/// <summary>
/// List of all buyers
/// </summary>
private List<IObserver<Art>> buyers;
/// <summary>
/// The buyer who can unsubscribe using this object
/// </summary>
private IObserver<Art> buyer;
/// <summary>
/// Unsubscribe from the auction
/// </summary>
public void Dispose()
{
if (buyers.Contains(buyer))
{
buyers.Remove(buyer);
}
}
internal Unsubscriber(List<IObserver<Art>> buyers, IObserver<Art> buyer)
{
this.buyers = buyers;
this.buyer = buyer;
}
}
}

Loading…
Cancel
Save