Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.69 KB | None | 0 0
  1. // Version 2
  2. // Fixed display on retirement percentage.
  3. // Removed annual sales tax for vehicle being included.
  4. // Enforced 401k max yearly contriutions.
  5. // Fixed formatting, and duplicate numbers.
  6.  
  7.  
  8. using System;
  9.  
  10. class CostCalculator
  11. {
  12.     private const double GrossIncome = 210000;     
  13.    
  14.     // Exemptions
  15.     private const double HealthInsuranceMontly = 815;       //Based on https://www.coveredca.com/
  16.     private const double RetirementPercent = 12;            // Assuming 8% employer match, and target goal of 20%
  17.     private const double Max401Annual = 23000;              // https://www.fidelity.com/learning-center/smart-money/401k-contribution-limits#:~:text=The%20IRS%20sets%20the%20maximum,2024%2C%20this%20rose%20to%20%2423%2C000.
  18.    
  19.     // Taxes
  20.     private const double StateTaxPercent = 9.875;           // Based on https://maps.cdtfa.ca.gov/ (random street in SF)
  21.     private const double SocialSecurityPercent = 6.2;       // Based on https://www.ssa.gov/oact/cola/cbb.html#:~:text=We%20call%20this%20annual%20limit,for%20employees%20and%20employers%2C%20each.
  22.     private const double MedicarePercent = 1.45;            // Based on https://www.irs.gov/taxtopics/tc751
  23.     private const double AdditionalMedicarePercent = 0.9;   // Also based on https://www.irs.gov/taxtopics/tc751
  24.    
  25.    
  26.     // Renting / Buying Home
  27.     private const double AnnualRent = 14560;                // Based on https://www.apartments.com/rent-market-trends/san-francisco-ca/
  28.     private const double HomePrice = 1200000;               // Based on https://www.zillow.com/home-values/20330/san-francisco-ca/
  29.     private const double PropertyTaxPercent = 1.17;         // Based on https://www.sftaxappeal.com/post/san-francisco-property-tax-calculator#:~:text=For%20the%20fiscal%20year%202023,to%20determine%20your%20tax%20liability.
  30.     private const double MortgageInterestPercent = 7.37;    // Based on https://www.google.com/search?q=current+mortage+rates
  31.     private const int MortgageTermYears = 30;               // Typical term for $$$$
  32.     private const double UtilityMonthlyCost = 240;          // Based on https://californiamoversusa.com/resources/cost-of-living-in-san-francisco-ca/#:~:text=As%20mentioned%20before%2C%20the%20average,example)%20or%20consume%20more%20gas.
  33.  
  34.     // Transportation
  35.     private const double PublicTransitAnnualCost = 1000;    // Based on Monthly Pass: https://www.sfmta.com/getting-around/muni/fares
  36.    
  37.  
  38.     // Car ownership variables... Random numbers for base-model tesla.
  39.     // Fees based on https://www.dmv.ca.gov/portal/vehicle-registration/registration-fees/
  40.     private const double CarPurchasePrice = 40240;         
  41.     private const double CarSalesTaxPercent = 8.6;
  42.     private const double FederalTaxCredit = 7500;
  43.     private const double StateRebate = 2000;
  44.     private const double HomeChargerCost = 2000;
  45.     private const double CarLoanAnnualPercent = 7;
  46.     private const double CarInsurance = 3397;
  47.     private const double CarMaintenance = 538;
  48.     private const double CarTires = 544;
  49.     private const double CarCharging = 1458;
  50.     private const double CarRegistration = 400;
  51.  
  52.     // Flags
  53.     private const bool Renting = false; // True for renting, false for owning
  54.     private const bool UsingPublicTransit = false; // True for public transit, false for owning a car
  55.  
  56.     static void Main(string[] args)
  57.     {
  58.         // Set the culture to US English
  59.         System.Globalization.CultureInfo.CurrentCulture = new System.Globalization.CultureInfo("en-US");
  60.         System.Globalization.CultureInfo.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
  61.  
  62.         // Perform Calculations
  63.         double exemptions = CalculateExemptions(out double healthInsurance, out double retirementContribution);
  64.         double taxableIncome = GrossIncome - exemptions;
  65.         double totalTaxes = CalculateTaxes(taxableIncome, out double federalTax, out double stateTax, out double socialSecurityTax, out double medicareTax, out double additionalMedicareTax);
  66.         double netIncome = GrossIncome - totalTaxes - exemptions;
  67.         double housingCost = CalculateHousingCost(out double propertyTaxes, out double monthlyMortgagePayment, out double annualUtilities);
  68.         double transportationCost = CalculateTransportationCost(out double carLoanAnnualCost);
  69.         double leftOver = netIncome - housingCost - transportationCost;
  70.  
  71.         // Output Tree-Like Breakdown
  72.         Console.WriteLine($"Gross Income: {GrossIncome:C}");
  73.         Console.WriteLine($"Left Over: {leftOver:C}");
  74.         Console.WriteLine();
  75.         Console.WriteLine();
  76.         Console.WriteLine($"├── Exemptions: {exemptions:C}");
  77.         Console.WriteLine($"│   ├── Health Insurance: {healthInsurance:C}");
  78.         Console.WriteLine($"│   ├── 401k Contribution ({RetirementPercent / 100:P0}): {retirementContribution:C}");
  79.         Console.WriteLine($"│   └── Total Exemptions: {exemptions:C}");
  80.         Console.WriteLine($"├── Taxes: {totalTaxes:C}");
  81.         Console.WriteLine($"│   ├── Federal Income Tax: {federalTax:C}");
  82.         Console.WriteLine($"│   ├── California State Tax: {stateTax:C}");
  83.         Console.WriteLine($"│   ├── Social Security Tax: {socialSecurityTax:C}");
  84.         Console.WriteLine($"│   ├── Medicare Tax: {medicareTax:C}");
  85.         Console.WriteLine($"│   └── Additional Medicare Tax: {additionalMedicareTax:C}");
  86.         Console.WriteLine($"├── Net Income: {netIncome:C}");
  87.         Console.WriteLine($"├── Housing: {housingCost:C}");
  88.         if (Renting)
  89.         {
  90.             Console.WriteLine($"│   └── Annual Rent: {AnnualRent:C}");
  91.         }
  92.         else
  93.         {
  94.             Console.WriteLine($"│   ├── Property Taxes: {propertyTaxes:C}");
  95.             Console.WriteLine($"│   └── Monthly Mortgage Payment: {monthlyMortgagePayment:C}");
  96.         }
  97.         Console.WriteLine($"│   └── Utilities: {annualUtilities:C}");
  98.         Console.WriteLine($"├── Transportation: {transportationCost:C}");
  99.         if (UsingPublicTransit)
  100.         {
  101.             Console.WriteLine($"│   └── Public Transit Annual Cost: {PublicTransitAnnualCost:C}");
  102.         }
  103.         else
  104.         {
  105.             Console.WriteLine($"│   ├── Car Loan Annual Cost: {carLoanAnnualCost:C}");
  106.             Console.WriteLine($"│   ├── Insurance: {CarInsurance:C}");
  107.             Console.WriteLine($"│   ├── Maintenance: {CarMaintenance:C}");
  108.             Console.WriteLine($"│   ├── Tires: {CarTires:C}");
  109.             Console.WriteLine($"│   ├── Charging: {CarCharging:C}");
  110.             Console.WriteLine($"│   └── Registration: {CarRegistration:C}");
  111.         }
  112.     }
  113.  
  114.     static double CalculateExemptions(out double healthInsurance, out double retirementContribution)
  115.     {
  116.         healthInsurance = HealthInsuranceMontly * 12;
  117.         double calculatedRetirementContribution = GrossIncome * (RetirementPercent / 100);
  118.  
  119.         // Use the lesser of calculated contribution or max allowed
  120.         retirementContribution = Math.Min(calculatedRetirementContribution, Max401Annual);
  121.         return healthInsurance + retirementContribution;
  122.     }
  123.  
  124.     static double CalculateTaxes(double taxableIncome, out double federalTax, out double stateTax, out double socialSecurityTax, out double medicareTax, out double additionalMedicareTax)
  125.     {
  126.         // Federal Tax Brackets for Single Filers in 2024
  127.         (double rate, double threshold)[] federalBrackets = new (double, double)[]
  128.         {
  129.             (0.10, 11600),
  130.             (0.12, 47150),
  131.             (0.22, 100525),
  132.             (0.24, 191950),
  133.             (0.32, 243725),
  134.             (0.35, 609350),
  135.             (0.37, double.MaxValue)
  136.         };
  137.  
  138.         federalTax = CalculateProgressiveTax(taxableIncome, federalBrackets);
  139.         stateTax = taxableIncome * (StateTaxPercent / 100);
  140.         socialSecurityTax = Math.Min(160000, GrossIncome) * (SocialSecurityPercent / 100);
  141.         medicareTax = GrossIncome * (MedicarePercent / 100);
  142.         additionalMedicareTax = GrossIncome > 200000 ? (GrossIncome - 200000) * (AdditionalMedicarePercent / 100) : 0;
  143.  
  144.         return federalTax + stateTax + socialSecurityTax + medicareTax + additionalMedicareTax;
  145.     }
  146.  
  147.     static double CalculateHousingCost(out double propertyTaxes, out double monthlyMortgagePayment, out double annualUtilities)
  148.     {
  149.         annualUtilities = UtilityMonthlyCost * 12;
  150.         if (Renting)
  151.         {
  152.             propertyTaxes = 0;
  153.             monthlyMortgagePayment = 0;
  154.             return AnnualRent + annualUtilities;
  155.         }
  156.  
  157.         propertyTaxes = HomePrice * (PropertyTaxPercent / 100);
  158.         monthlyMortgagePayment = CalculateMonthlyMortgage(HomePrice, 20, MortgageInterestPercent, MortgageTermYears);
  159.         return (monthlyMortgagePayment * 12) + propertyTaxes + annualUtilities;
  160.     }
  161.  
  162.     static double CalculateTransportationCost(out double carLoanAnnualCost)
  163.     {
  164.         if (UsingPublicTransit)
  165.         {
  166.             carLoanAnnualCost = 0;
  167.             return PublicTransitAnnualCost;
  168.         }
  169.  
  170.         carLoanAnnualCost = CalculateAnnualLoanCost(CarPurchasePrice, 20, CarLoanAnnualPercent, 5);
  171.  
  172.         return carLoanAnnualCost + CarInsurance + CarMaintenance + CarTires + CarCharging + CarRegistration;
  173.     }
  174.  
  175.     static double CalculateProgressiveTax(double income, (double rate, double threshold)[] brackets)
  176.     {
  177.         double tax = 0;
  178.         double remainingIncome = income;
  179.         double previousThreshold = 0;
  180.  
  181.         foreach (var bracket in brackets)
  182.         {
  183.             if (remainingIncome > bracket.threshold - previousThreshold)
  184.             {
  185.                 tax += (bracket.threshold - previousThreshold) * bracket.rate;
  186.                 remainingIncome -= bracket.threshold - previousThreshold;
  187.                 previousThreshold = bracket.threshold;
  188.             }
  189.             else
  190.             {
  191.                 tax += remainingIncome * bracket.rate;
  192.                 break;
  193.             }
  194.         }
  195.         return tax;
  196.     }
  197.  
  198.     static double CalculateMonthlyMortgage(double homePrice, double downPaymentPercent, double annualInterestRate, int termYears)
  199.     {
  200.         double loanAmount = homePrice * ((100 - downPaymentPercent) / 100);
  201.         double monthlyInterestRate = annualInterestRate / 100 / 12;
  202.         int numberOfPayments = termYears * 12;
  203.         return (loanAmount * monthlyInterestRate) /
  204.                (1 - Math.Pow(1 + monthlyInterestRate, -numberOfPayments));
  205.     }
  206.  
  207.     static double CalculateAnnualLoanCost(double purchasePrice, double downPaymentPercent, double annualInterestRate, int termYears)
  208.     {
  209.         double loanAmount = purchasePrice * ((100 - downPaymentPercent) / 100);
  210.         double annualInterestRateDecimal = annualInterestRate / 100;
  211.         return loanAmount * annualInterestRateDecimal;
  212.     }
  213. }
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement