Skip to content

Commit bbf1c5f

Browse files
committed
Adding Ex10 draft material from Researcher. Code projects and lab instructions.
1 parent 7cc4722 commit bbf1c5f

9 files changed

Lines changed: 887 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# This workflow is triggered on two events: workflow_dispatch and push.
2+
#
3+
# - The workflow_dispatch event allows you to manually trigger the workflow from GitHub's UI.
4+
# - The push event triggers the workflow whenever there's a push to the master branch, but only
5+
# if the changes include files in the LearnModuleExercises/SampleApps/** directory.
6+
#
7+
# The defaults section sets the default shell for all run commands in the workflow to PowerShell (pwsh).
8+
#
9+
# The workflow consists of a single job named create_zip, which runs on the latest version of Ubuntu.
10+
#
11+
# This job has three steps:
12+
#
13+
# 1. The Checkout step uses the actions/checkout@v4 action to checkout the repository's code onto the
14+
# runner. This is a common first step in most workflows as it allows subsequent steps to operate on
15+
# the codebase.
16+
#
17+
# 2. The Create SampleApps zip step changes the current directory to ./LearnModuleExercises/SampleApps
18+
# and then creates a zip file of all the files in that directory, including those in the .vscode
19+
# subdirectory. The -r option is used to zip directories recursively and the -q option is used to run
20+
# the command quietly without printing a lot of output. The resulting zip file is saved in the
21+
# ../Downloads directory with the name SampleApps.zip.
22+
#
23+
# 3. The Commit and push step uses the Endbug/add-and-commit@v7 action to add the newly created zip file
24+
# to the repository, commit the changes with the message 'Updating Zip for API source files', and then
25+
# push the changes back to the repository. The add input is set to the path of the zip file and the push
26+
# input is set to true to enable pushing.
27+
#
28+
# This workflow is useful for automatically packaging and versioning sample applications whenever changes
29+
# are made to them.
30+
#
31+
name: CreateGHCopilotEx10SamplesZip
32+
on:
33+
workflow_dispatch:
34+
push:
35+
branches:
36+
- 'main'
37+
paths:
38+
- DownloadableCodeProjects/standalone-lab-projects/implement-performance-profiling/**
39+
40+
defaults:
41+
run:
42+
shell: bash
43+
44+
jobs:
45+
create_zip:
46+
runs-on: ubuntu-latest
47+
steps:
48+
- name: Checkout
49+
uses: actions/checkout@v4
50+
- name: Create GHCopilotEx10 SampleApps zip
51+
run: |
52+
# Ensure Downloads directory exists
53+
mkdir -p ./DownloadableCodeProjects/Downloads
54+
55+
# Change to source directory
56+
cd ./DownloadableCodeProjects/standalone-lab-projects/implement-performance-profiling
57+
58+
# Check if there are any git-tracked files to zip
59+
if [ -z "$(git ls-files)" ]; then
60+
echo "No git-tracked files found in the source directory"
61+
exit 1
62+
fi
63+
64+
# Remove existing zip file and create new one
65+
rm -f ../../Downloads/GHCopilotEx10LabApps.zip
66+
zip -r -q ../../Downloads/GHCopilotEx10LabApps.zip $(git ls-files)
67+
68+
# Verify zip file was created
69+
if [ ! -f ../../Downloads/GHCopilotEx10LabApps.zip ]; then
70+
echo "Failed to create zip file"
71+
exit 1
72+
fi
73+
74+
echo "Successfully created GHCopilotEx10LabApps.zip"
75+
- name: Commit and push
76+
uses: Endbug/add-and-commit@v7
77+
with:
78+
add: '["DownloadableCodeProjects/Downloads/GHCopilotEx10LabApps.zip"]'
79+
message: 'Updating Zip with sample app source files'
80+
push: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// See https://aka.ms/new-console-template for more information
2+
Console.WriteLine("Hello, World!");
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
2+
ContosoOnlineStore/
3+
├── Program.cs
4+
├── Product.cs
5+
├── ProductCatalog.cs
6+
├── Order.cs
7+
├── OrderItem.cs
8+
├── OrderProcessor.cs
9+
├── InventoryManager.cs
10+
├── EmailService.cs
11+
├── ContosoOnlineStore.csproj
12+
└── README.md
13+
14+
15+
# 🛒 ContosoOnlineStore
16+
17+
## Program.cs
18+
```csharp
19+
using System;
20+
using System.Diagnostics;
21+
22+
namespace ContosoOnlineStore
23+
{
24+
class Program
25+
{
26+
static async Task Main(string[] args)
27+
{
28+
Console.WriteLine("=== Contoso Online Store ===");
29+
30+
ProductCatalog catalog = new ProductCatalog();
31+
InventoryManager inventory = new InventoryManager(catalog);
32+
EmailService emailService = new EmailService();
33+
OrderProcessor processor = new OrderProcessor(catalog, inventory, emailService);
34+
35+
Order order = new Order();
36+
order.Items.Add(new OrderItem(2, 5));
37+
order.Items.Add(new OrderItem(7, 3));
38+
order.Items.Add(new OrderItem(4, 10));
39+
order.Items.Add(new OrderItem(9, 2));
40+
41+
Stopwatch sw = Stopwatch.StartNew();
42+
decimal total = await processor.FinalizeOrder(order);
43+
sw.Stop();
44+
45+
Console.WriteLine($"\nOrder processed. Total Cost = ${total:F2}");
46+
Console.WriteLine($"Processing Time = {sw.ElapsedMilliseconds} ms");
47+
48+
Console.WriteLine("Remaining stock:");
49+
foreach (var item in order.Items)
50+
{
51+
Product prod = catalog.GetProductById(item.ProductId);
52+
int stock = inventory.GetStockLevel(item.ProductId);
53+
Console.WriteLine($"- {prod.Name}: {stock} units");
54+
}
55+
}
56+
}
57+
}
58+
```
59+
60+
## Product.cs
61+
```csharp
62+
namespace ContosoOnlineStore
63+
{
64+
public class Product
65+
{
66+
public int Id { get; }
67+
public string Name { get; }
68+
public decimal Price { get; }
69+
public int InitialStock { get; }
70+
71+
public Product(int id, string name, decimal price, int initialStock)
72+
{
73+
Id = id;
74+
Name = name;
75+
Price = price;
76+
InitialStock = initialStock;
77+
}
78+
}
79+
}
80+
```
81+
82+
## ProductCatalog.cs
83+
```csharp
84+
using System.Collections.Generic;
85+
using System.Linq;
86+
87+
namespace ContosoOnlineStore
88+
{
89+
public class ProductCatalog
90+
{
91+
private List<Product> _products;
92+
93+
public ProductCatalog()
94+
{
95+
_products = new List<Product>
96+
{
97+
new Product(1, "Phone", 299.99m, 50),
98+
new Product(2, "Headphones", 99.99m, 200),
99+
new Product(3, "Laptop", 1499.00m, 20),
100+
new Product(4, "Monitor", 389.50m, 75),
101+
new Product(5, "Charger", 19.99m, 500),
102+
new Product(6, "Speaker", 79.95m, 120),
103+
new Product(7, "SSD", 159.49m, 60),
104+
new Product(8, "Keyboard", 49.99m, 150),
105+
new Product(9, "Webcam", 39.99m, 300),
106+
new Product(10, "Earbuds", 129.99m, 80)
107+
};
108+
}
109+
110+
public Product GetProductById(int productId)
111+
{
112+
// Performance Issue: Linear search
113+
return _products.FirstOrDefault(p => p.Id == productId);
114+
}
115+
116+
public List<Product> GetAllProducts() => _products;
117+
}
118+
}
119+
```
120+
121+
## Order.cs
122+
```csharp
123+
using System.Collections.Generic;
124+
125+
namespace ContosoOnlineStore
126+
{
127+
public class Order
128+
{
129+
public List<OrderItem> Items { get; }
130+
131+
public Order()
132+
{
133+
Items = new List<OrderItem>();
134+
}
135+
}
136+
}
137+
```
138+
139+
## OrderItem.cs
140+
```csharp
141+
namespace ContosoOnlineStore
142+
{
143+
public class OrderItem
144+
{
145+
public int ProductId { get; set; }
146+
public int Quantity { get; set; }
147+
148+
public OrderItem(int productId, int quantity)
149+
{
150+
ProductId = productId;
151+
Quantity = quantity;
152+
}
153+
}
154+
}
155+
```
156+
157+
## InventoryManager.cs
158+
```csharp
159+
using System.Collections.Generic;
160+
161+
namespace ContosoOnlineStore
162+
{
163+
public class InventoryManager
164+
{
165+
private Dictionary<int, int> _stockByProductId;
166+
167+
public InventoryManager(ProductCatalog catalog)
168+
{
169+
_stockByProductId = new Dictionary<int, int>();
170+
foreach (var product in catalog.GetAllProducts())
171+
{
172+
_stockByProductId[product.Id] = product.InitialStock;
173+
}
174+
}
175+
176+
public int GetStockLevel(int productId)
177+
{
178+
return _stockByProductId.ContainsKey(productId) ? _stockByProductId[productId] : 0;
179+
}
180+
181+
public void UpdateStockLevels(Order order)
182+
{
183+
foreach (OrderItem item in order.Items)
184+
{
185+
if (_stockByProductId.ContainsKey(item.ProductId))
186+
{
187+
_stockByProductId[item.ProductId] -= item.Quantity;
188+
}
189+
}
190+
}
191+
}
192+
}
193+
```
194+
195+
## EmailService.cs
196+
```csharp
197+
using System;
198+
using System.Threading.Tasks;
199+
200+
namespace ContosoOnlineStore
201+
{
202+
public class EmailService
203+
{
204+
public async Task SendConfirmationAsync(Order order)
205+
{
206+
Console.WriteLine("Sending confirmation email...");
207+
await Task.Delay(2000); // Performance Issue: Simulated blocking call
208+
Console.WriteLine("Email sent.");
209+
}
210+
}
211+
}
212+
```
213+
214+
## OrderProcessor.cs
215+
```csharp
216+
using System.Collections.Generic;
217+
using System.Threading.Tasks;
218+
219+
namespace ContosoOnlineStore
220+
{
221+
public class OrderProcessor
222+
{
223+
private ProductCatalog _catalog;
224+
private InventoryManager _inventory;
225+
private EmailService _emailService;
226+
227+
public OrderProcessor(ProductCatalog catalog, InventoryManager inventory, EmailService emailService)
228+
{
229+
_catalog = catalog;
230+
_inventory = inventory;
231+
_emailService = emailService;
232+
}
233+
234+
public decimal CalculateOrderTotal(Order order)
235+
{
236+
decimal total = 0;
237+
var productCache = new Dictionary<int, Product>();
238+
239+
foreach (OrderItem item in order.Items)
240+
{
241+
if (!productCache.ContainsKey(item.ProductId))
242+
{
243+
productCache[item.ProductId] = _catalog.GetProductById(item.ProductId);
244+
}
245+
246+
var prod = productCache[item.ProductId];
247+
if (prod != null)
248+
{
249+
total += prod.Price * item.Quantity;
250+
}
251+
}
252+
253+
return total;
254+
}
255+
256+
public async Task<decimal> FinalizeOrder(Order order)
257+
{
258+
decimal total = CalculateOrderTotal(order);
259+
_inventory.UpdateStockLevels(order);
260+
await _emailService.SendConfirmationAsync(order);
261+
return total;
262+
}
263+
}
264+
}
265+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// See https://aka.ms/new-console-template for more information
2+
Console.WriteLine("Hello, World!");

0 commit comments

Comments
 (0)