Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .vs/Lab07-Collections/v15/.suo
Binary file not shown.
3 changes: 3 additions & 0 deletions .vs/ProjectSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"CurrentProjectSetting": null
}
8 changes: 8 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"ExpandedNodes": [
"",
"\\CustomCollection"
],
"SelectedNode": "\\CustomCollection\\README.md",
"PreviewInSolutionExplorer": false
}
Binary file modified .vs/slnx.sqlite
Binary file not shown.
Binary file added CustomCollection/.vs/CustomCollection/v15/.suo
Binary file not shown.
Empty file.
Binary file not shown.
31 changes: 31 additions & 0 deletions CustomCollection/CustomCollection.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomCollection", "CustomCollection\CustomCollection.csproj", "{6BEE06ED-987E-4FBA-BA70-932F9F34A826}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XUnitTestCollection", "XUnitTestCollection\XUnitTestCollection.csproj", "{B31A80C9-84D6-4AB4-8492-8EE8E39E3324}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6BEE06ED-987E-4FBA-BA70-932F9F34A826}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6BEE06ED-987E-4FBA-BA70-932F9F34A826}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6BEE06ED-987E-4FBA-BA70-932F9F34A826}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6BEE06ED-987E-4FBA-BA70-932F9F34A826}.Release|Any CPU.Build.0 = Release|Any CPU
{B31A80C9-84D6-4AB4-8492-8EE8E39E3324}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B31A80C9-84D6-4AB4-8492-8EE8E39E3324}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B31A80C9-84D6-4AB4-8492-8EE8E39E3324}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B31A80C9-84D6-4AB4-8492-8EE8E39E3324}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5FBC7E45-DC38-425D-B51B-7F544FABD971}
EndGlobalSection
EndGlobal
12 changes: 12 additions & 0 deletions CustomCollection/CustomCollection/CustomCollection.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="xunit" Version="2.3.1" />
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions CustomCollection/CustomCollection/Product.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CustomCollection
{
public class Product
{
public string Name { get; set; }
public ProductType Type { get; set; }
public enum ProductType : int
{
Desktop = 1,
Laptop,
Tablet,
SmartPhone,
GameConsole,
}

public Product(string name, ProductType type)
{
Name = name;
Type = type;
}
}
}
77 changes: 77 additions & 0 deletions CustomCollection/CustomCollection/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;

namespace CustomCollection
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");

Product iPhoneX = new Product("iPhone X", Product.ProductType.SmartPhone);
Product SurfaceBook2 = new Product("Surface Book 2", Product.ProductType.Laptop);
Product Pixel2XL = new Product("Pixel 2 XL", Product.ProductType.SmartPhone);
Product SurfacePro4 = new Product("Surface Pro 4", Product.ProductType.Tablet);
Product Area51 = new Product("Area 51", Product.ProductType.Desktop);

Console.WriteLine("Instatiating Store with 5 products.");
Store<Product> bestBuy = new Store<Product>
{
iPhoneX, SurfaceBook2, Pixel2XL, SurfacePro4, Area51
};

Product PS4Slim = new Product("PS4 Slim", Product.ProductType.GameConsole);
Product PS4Pro = new Product("PS4 Pro", Product.ProductType.GameConsole);
Product XBoxOne = new Product("XBox One", Product.ProductType.GameConsole);
Product XBoxOneX = new Product("XBox One X", Product.ProductType.GameConsole);
Product Switch = new Product("Switch", Product.ProductType.GameConsole);

Console.WriteLine("Called Add() on 5 products");
bestBuy.Add(PS4Slim);
bestBuy.Add(PS4Pro);
bestBuy.Add(XBoxOne);
bestBuy.Add(XBoxOneX);
bestBuy.Add(Switch);

Console.WriteLine("Called GetAtIndex(Pixel2XL)");
Console.WriteLine("Index of Pixel 2 XL: " + bestBuy.GetAtIndex(Pixel2XL));

Console.WriteLine("Called ViewAll");
ViewAll(bestBuy.items);

Console.WriteLine("Calling Remove() on 9 items. Switch will be the only one left.");
bestBuy.Remove(SurfacePro4);
bestBuy.Remove(PS4Pro);
bestBuy.Remove(Pixel2XL);
bestBuy.Remove(XBoxOne);
bestBuy.Remove(iPhoneX);
bestBuy.Remove(PS4Slim);
bestBuy.Remove(Area51);
bestBuy.Remove(XBoxOneX);
bestBuy.Remove(SurfaceBook2);

Console.ReadLine();
}

/// <summary>
/// Used to print all items in the collection to the screen.
/// </summary>
/// <param name="items">The collection of items to print</param>
public static void ViewAll(Product[] items)
{
/* If I want to implement as a non-static method attached to Store class:
*
* PropertyInfo property = typeof(T).GetProperty("Name");
* Console.WriteLine(property.GetValue(items[i]));
*/

Console.WriteLine("\nAll items currently in the collection: ");
for (int i = 0; i < items.Length; i++)
{
if (items[i] == null) break;
Console.WriteLine(items[i].Name);
}
Console.WriteLine("");
}
}
}
119 changes: 119 additions & 0 deletions CustomCollection/CustomCollection/Store.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace CustomCollection
{
public class Store<T> : IEnumerable<T>
{
public T[] items = new T[10];
public int count = 0;

/// <summary>
/// Add an item to the collection. Also determines if collection array is getting full and increases by 50% if it gets more than 70% full.
/// </summary>
/// <param name="item">Object to add to collection</param>
public void Add(T item)
{
Console.WriteLine("Collection size before add: " + items.Length);
Console.WriteLine("Property Count: " + count);
if (count > (int)Math.Ceiling((items.Length * 0.7)))
{
Console.WriteLine("Array > 70% capacity! Increasing array size by 50%!");
T[] newArray = new T[(int)Math.Ceiling((items.Length * 1.5))];
for (int i = 0; i < items.Length; i++)
{
newArray[i] = items[i];
}
items = newArray;
}
items[count] = item;
count++;
Console.WriteLine("Collection size after add: " + items.Length);
Console.WriteLine("Property Count: " + count);
Console.WriteLine("");
}

/// <summary>
/// Remove an item from the collection. Does this by shifting all items after the passed in object to the left by 1. Also checks beforehand if the collection array is less than 30% capacity, and shrinks by 50% if so.
/// </summary>
/// <param name="item">Object to remove from collection.</param>
public void Remove(T item)
{
Console.WriteLine("Collection size before remove: " + items.Length);
Console.WriteLine("Property Count: " + count);
for (int i=0; i < items.Length; i++)
{
if(EqualityComparer<T>.Default.Equals(items[i], item))
{
Console.WriteLine("Found item at index " + i + " during remove method.");
// check if size is a quarter full and should shrink by 1/2
if (count < (int)Math.Ceiling((items.Length * 0.3)))
{
// if so create new array 1/2 size to transfer contents without item trying to remove.
Console.WriteLine("Array < 30% capacity! Decreasing array size by 50%!");
T[] newArray = new T[(int)Math.Ceiling((items.Length * 0.5))];
int temp = 0;
for (int k = 0; k < newArray.Length - 1; k++)
{
if (k == i) temp++;
newArray[k] = items[temp];
temp++;
}
items = newArray;
}
else
{
// if not, start at i (location of item to remove) and move everything over by 1.
Console.WriteLine("Removing item by shifting everything after it over by one!");
int temp = i + 1;
for (int k = i; k < items.Length - 1; k++)
{
items[k] = items[temp];
temp++;
}
}
count--;
break;
}
}
Console.WriteLine("Collection size after remove: " + items.Length);
Console.WriteLine("Property Count: " + count);
Console.WriteLine("");
}

/// <summary>
/// Simply retrieves an item from the collection at a specific index and returns that index location.
/// </summary>
/// <param name="index">index to grab object from.</param>
/// <returns>The index location of the object.</returns>
public int GetAtIndex(T item)
{
int index = 0;
for (int i = 0; i < count; i++)
{
if (EqualityComparer<T>.Default.Equals(items[i], item))
{
index = i;
break;
}
}
return index;
}

public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < count; i++)
{
yield return items[i];
}
}

IEnumerator IEnumerable.GetEnumerator()
{
//Magic don't touch.
return GetEnumerator();
}
}
}
Loading