Skip to content

Commit 55c0504

Browse files
committed
adding simplify conditionals code and instructions
1 parent 7e80c51 commit 55c0504

6 files changed

Lines changed: 473 additions & 0 deletions

File tree

DownloadableCodeProjects/standalone-lab-projects/placeholder.txt

Whitespace-only changes.
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: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace ECommercePricing
6+
{
7+
public enum MembershipLevel { Guest, Silver, Premium }
8+
9+
public class User
10+
{
11+
public MembershipLevel Membership { get; set; }
12+
public bool IsFirstTimeBuyer { get; set; }
13+
}
14+
15+
public class Coupon
16+
{
17+
public string Code { get; set; }
18+
public bool IsValid { get; set; }
19+
public bool IsExpired { get; set; }
20+
public string Type { get; set; } // "percent" or "shipping"
21+
public decimal Value { get; set; } // e.g., 10 for 10% off
22+
}
23+
24+
public class Item
25+
{
26+
public string Name { get; set; }
27+
public string Category { get; set; } // e.g., "Electronics", "Clothing"
28+
public decimal Price { get; set; }
29+
}
30+
31+
public class Order
32+
{
33+
public List<Item> Items { get; set; } = new List<Item>();
34+
public bool IsDomestic { get; set; }
35+
public Coupon Coupon { get; set; }
36+
37+
public decimal GetSubtotal() => Items.Sum(i => i.Price);
38+
public decimal GetSubtotalForCategory(string category) =>
39+
Items.Where(i => i.Category == category).Sum(i => i.Price);
40+
}
41+
42+
public class PricingEngine
43+
{
44+
public static void CalculateFinalPrice(User user, Order order)
45+
{
46+
decimal baseTotal = order.GetSubtotal();
47+
decimal discountPercent = 0m;
48+
decimal shippingCost = order.IsDomestic ? 10m : 25m;
49+
50+
// Membership-based discounts
51+
if (user.Membership == MembershipLevel.Premium)
52+
{
53+
discountPercent += 10;
54+
if (baseTotal > 5000)
55+
discountPercent += 5;
56+
}
57+
else if (user.Membership == MembershipLevel.Silver)
58+
{
59+
discountPercent += 5;
60+
}
61+
else if (user.IsFirstTimeBuyer)
62+
{
63+
discountPercent += 5;
64+
}
65+
66+
// Coupon logic
67+
if (order.Coupon != null)
68+
{
69+
if (order.Coupon.IsValid)
70+
{
71+
if (order.Coupon.Type == "percent")
72+
{
73+
discountPercent += order.Coupon.Value;
74+
}
75+
else if (order.Coupon.Type == "shipping" && order.IsDomestic)
76+
{
77+
shippingCost = 0;
78+
}
79+
}
80+
else if (order.Coupon.IsExpired)
81+
{
82+
Console.WriteLine("Coupon expired. No discount applied.");
83+
}
84+
}
85+
86+
// Bulk purchase discount
87+
if (order.Items.Count >= 10)
88+
{
89+
discountPercent += 5;
90+
}
91+
92+
// Apply discount with category cap
93+
decimal electronicsSubtotal = order.GetSubtotalForCategory("Electronics");
94+
decimal otherSubtotal = baseTotal - electronicsSubtotal;
95+
96+
decimal electronicsDiscount = Math.Min(discountPercent, 10);
97+
decimal discountedElectronics = electronicsSubtotal * (1 - electronicsDiscount / 100);
98+
decimal discountedOther = otherSubtotal * (1 - discountPercent / 100);
99+
100+
decimal finalPrice = discountedElectronics + discountedOther + shippingCost;
101+
102+
Console.WriteLine($"Base Total: ${baseTotal:F2}");
103+
Console.WriteLine($"Discount Applied: {discountPercent}% (Electronics capped at 10%)");
104+
Console.WriteLine($"Shipping Cost: ${shippingCost:F2}");
105+
Console.WriteLine($"Final Price: ${finalPrice:F2}");
106+
}
107+
}
108+
109+
class Program
110+
{
111+
static void Main(string[] args)
112+
{
113+
var user = new User
114+
{
115+
Membership = MembershipLevel.Premium,
116+
IsFirstTimeBuyer = false
117+
};
118+
119+
var coupon = new Coupon
120+
{
121+
Code = "SAVE10",
122+
IsValid = true,
123+
IsExpired = false,
124+
Type = "percent",
125+
Value = 10
126+
};
127+
128+
var order = new Order
129+
{
130+
IsDomestic = true,
131+
Coupon = coupon,
132+
Items = new List<Item>
133+
{
134+
new Item { Name = "Laptop", Category = "Electronics", Price = 1500 },
135+
new Item { Name = "Headphones", Category = "Electronics", Price = 200 },
136+
new Item { Name = "Shoes", Category = "Clothing", Price = 120 },
137+
new Item { Name = "Jacket", Category = "Clothing", Price = 180 },
138+
new Item { Name = "Watch", Category = "Accessories", Price = 250 },
139+
new Item { Name = "Backpack", Category = "Accessories", Price = 90 },
140+
new Item { Name = "T-shirt", Category = "Clothing", Price = 30 },
141+
new Item { Name = "Socks", Category = "Clothing", Price = 20 },
142+
new Item { Name = "Tablet", Category = "Electronics", Price = 800 },
143+
new Item { Name = "Charger", Category = "Electronics", Price = 50 }
144+
}
145+
};
146+
147+
PricingEngine.CalculateFinalPrice(user, order);
148+
}
149+
}
150+
}
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: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
using System;
2+
3+
namespace LoanApprovalSystem
4+
{
5+
public enum ApprovalStatus
6+
{
7+
Approved,
8+
ConditionallyApproved,
9+
Declined
10+
}
11+
12+
public class LoanDecision
13+
{
14+
public ApprovalStatus Status { get; set; }
15+
public double InterestRate { get; set; }
16+
public double ApprovedAmount { get; set; }
17+
public string Notes { get; set; }
18+
}
19+
20+
public class Applicant
21+
{
22+
public int CreditScore { get; set; }
23+
public double AnnualIncome { get; set; }
24+
public int EmploymentYears { get; set; }
25+
public double DebtToIncomeRatio { get; set; }
26+
public double RequestedLoanAmount { get; set; }
27+
public double CollateralValue { get; set; }
28+
public bool HasCriminalRecord { get; set; }
29+
public bool IsCitizen { get; set; }
30+
}
31+
32+
public class LoanEvaluator
33+
{
34+
public LoanDecision Evaluate(Applicant applicant)
35+
{
36+
var decision = new LoanDecision();
37+
38+
if (applicant.CreditScore >= 750)
39+
{
40+
if (applicant.AnnualIncome >= 60000)
41+
{
42+
if (applicant.EmploymentYears >= 2)
43+
{
44+
if (applicant.DebtToIncomeRatio <= 0.35)
45+
{
46+
if (applicant.RequestedLoanAmount <= 500000)
47+
{
48+
if (applicant.CollateralValue >= 0.8 * applicant.RequestedLoanAmount)
49+
{
50+
if (!applicant.HasCriminalRecord)
51+
{
52+
if (applicant.IsCitizen)
53+
{
54+
decision.Status = ApprovalStatus.Approved;
55+
decision.InterestRate = 3.5;
56+
decision.ApprovedAmount = applicant.RequestedLoanAmount;
57+
decision.Notes = "Approved at best rate.";
58+
}
59+
else
60+
{
61+
decision.Status = ApprovalStatus.Approved;
62+
decision.InterestRate = 4.0;
63+
decision.ApprovedAmount = applicant.RequestedLoanAmount;
64+
decision.Notes = "Approved for non-citizen at slightly higher rate.";
65+
}
66+
}
67+
else
68+
{
69+
decision.Status = ApprovalStatus.ConditionallyApproved;
70+
decision.InterestRate = 4.5;
71+
decision.ApprovedAmount = applicant.RequestedLoanAmount;
72+
decision.Notes = "Conditional approval pending background review.";
73+
}
74+
}
75+
else
76+
{
77+
decision.Status = ApprovalStatus.Approved;
78+
decision.InterestRate = 4.25;
79+
decision.ApprovedAmount = 0.8 * applicant.CollateralValue;
80+
decision.Notes = "Approved with lower amount due to insufficient collateral.";
81+
}
82+
}
83+
else
84+
{
85+
decision.Status = ApprovalStatus.Approved;
86+
decision.InterestRate = 4.0;
87+
decision.ApprovedAmount = 500000;
88+
decision.Notes = "Approved with cap at $500,000.";
89+
}
90+
}
91+
else
92+
{
93+
decision.Status = ApprovalStatus.Approved;
94+
decision.InterestRate = 5.0;
95+
decision.ApprovedAmount = applicant.RequestedLoanAmount * 0.8;
96+
decision.Notes = "Approved with high debt ratio penalty.";
97+
}
98+
}
99+
else
100+
{
101+
decision.Status = ApprovalStatus.Approved;
102+
decision.InterestRate = 5.25;
103+
decision.ApprovedAmount = applicant.RequestedLoanAmount * 0.75;
104+
decision.Notes = "Approved with short employment history penalty.";
105+
}
106+
}
107+
else
108+
{
109+
decision.Status = ApprovalStatus.Approved;
110+
decision.InterestRate = 5.5;
111+
decision.ApprovedAmount = applicant.RequestedLoanAmount * 0.7;
112+
decision.Notes = "Approved with low income cap.";
113+
}
114+
}
115+
else if (applicant.CreditScore >= 650)
116+
{
117+
if (applicant.AnnualIncome >= 50000)
118+
{
119+
if (applicant.DebtToIncomeRatio <= 0.4)
120+
{
121+
decision.Status = ApprovalStatus.Approved;
122+
decision.InterestRate = 6.5;
123+
decision.ApprovedAmount = Math.Min(applicant.RequestedLoanAmount, 250000);
124+
decision.Notes = "Approved at higher interest rate.";
125+
}
126+
else
127+
{
128+
decision.Status = ApprovalStatus.ConditionallyApproved;
129+
decision.InterestRate = 7.0;
130+
decision.ApprovedAmount = applicant.RequestedLoanAmount * 0.6;
131+
decision.Notes = "Conditional approval with debt reduction plan.";
132+
}
133+
}
134+
else
135+
{
136+
decision.Status = ApprovalStatus.Declined;
137+
decision.Notes = "Declined due to low income.";
138+
}
139+
}
140+
else
141+
{
142+
decision.Status = ApprovalStatus.Declined;
143+
decision.Notes = "Declined due to low credit score.";
144+
}
145+
146+
return decision;
147+
}
148+
}
149+
150+
class Program
151+
{
152+
static void Main(string[] args)
153+
{
154+
var applicant = new Applicant
155+
{
156+
CreditScore = 760,
157+
AnnualIncome = 70000,
158+
EmploymentYears = 3,
159+
DebtToIncomeRatio = 0.30,
160+
RequestedLoanAmount = 400000,
161+
CollateralValue = 350000,
162+
HasCriminalRecord = false,
163+
IsCitizen = true
164+
};
165+
166+
var evaluator = new LoanEvaluator();
167+
var decision = evaluator.Evaluate(applicant);
168+
169+
Console.WriteLine($"Status: {decision.Status}");
170+
Console.WriteLine($"Interest Rate: {decision.InterestRate}%");
171+
Console.WriteLine($"Approved Amount: ${decision.ApprovedAmount}");
172+
Console.WriteLine($"Notes: {decision.Notes}");
173+
}
174+
}
175+
}

0 commit comments

Comments
 (0)