저의 작고 소중한 네 번째 과제(Mortgages)입니다. 이번에는 새롭게 배운 class와 object를 기반으로 코드를 짜보았습니다.
using System;
// Create Mortgage Class
public class Mortgage
{
// Declare properties for Loan, Rate & Term
public double Loan { get; set; }
public double Rate { get; set; }
public int Term {get; set;}
// Create overload method for validation (double)
public static double GetDouble(string prompt, double min, double max)
{
double number = 0.0;
string input;
bool valid;
do
{
Console.Write("{0} ({1} <= range <= {2}) : ", prompt, min, max);
input = Console.ReadLine();
valid = double.TryParse(input, out number) && number >= min & number <= max;
if (!valid) Console.WriteLine("Please enter a number in the correct range");
} while (!valid);
return number;
}
// Create overload method for validation (integer)
public static int GetInt(string prompt, int min, int max)
{
string input;
int number;
bool valid;
do
{
Console.Write("{0} ({1} <= range <= {2}) : ", prompt, min, max);
input = Console.ReadLine();
valid = Int32.TryParse(input, out number) && number >= min & number <= max;
if (!valid) Console.WriteLine("Please enter a number in the correct range");
} while (!valid);
return number;
}
// Setup read-only property for Payment
private double _Payment;
public double Payment
{
get
{
if (_Payment == 0) _Payment = CalcPayment(Loan, Rate, Term);
return _Payment;
}
}
// Setup read-only property for TotalCost
private double _TotalCost;
public double TotalCost
{
get
{
if (_TotalCost == 0) _TotalCost = CalcTotalCost(Payment, Term);
return _TotalCost;
}
}
// Setup read-only property for TotalInterest
private double _TotalInterest;
public double TotalInterest
{
get
{
if (_TotalInterest == 0) _TotalInterest = CalcTotalInterest(TotalCost, Loan);
return _TotalInterest;
}
}
// Create method to calculate the monthly payment amount
public double CalcPayment(double principal, double annualRate, int months)
{
double output = 0;
double Monthrate = annualRate / 12;
output = (principal * (Monthrate * Math.Pow(1 + Monthrate,months))) / (Math.Pow(1 + Monthrate, months) - 1);
return output;
}
// Create method to calculate the total cost
public double CalcTotalCost(double payment, int months)
{
double output = 0;
output = payment * months;
return output;
}
// Create method to calculate the total interest
public double CalcTotalInterest(double cost, double principal)
{
double output = 0;
output = cost - principal;
return output;
}
// Convert all values to a single string
public override string ToString()
{
string output = "";
output = Loan.ToString("C2") + ";" +
Rate.ToString("P") + ";" +
Term.ToString() + ";" +
Payment.ToString("C2") + ";" +
TotalCost.ToString("C2") + ";" +
TotalInterest.ToString("C2");
return output;
}
}
public class Program
{
public static void Main (string[] args)
{
// Create values for the header and the separator
int[] field = new int[] {16,10,8,15,16,16};
string Header = "Loan".PadLeft(field[0]) + "Rate".PadRight(field[1]) + "Months".PadRight(field[2]) + "Payment".PadRight(field[3]) + "Total Cost".PadRight(field[4]) + "Interest".PadRight(field[5]);
string Separator = " ".PadLeft(field[0], '=') + " ".PadLeft(field[1], '=') + " ".PadLeft(field[2], '=') +" ".PadLeft(field[3], '=') +" ".PadLeft(field[4], '=') +" ".PadLeft(field[5], '=');
//Ask for the user to input the mortgage product amount
string s;
int i;
bool IsInt = false;
do
{
Console.WriteLine("How many mortgage products would you like to compare? (0 to 10)");
s = Console.ReadLine();
IsInt = Int32.TryParse(s, out i);
} while (i<0 || i>10 || !IsInt);
Mortgage[] m = new Mortgage[i];
string[] CompileInfo = new string[i];
int j;
//Continue the loop for the amount of mortgage products the user inputted
for(j = 0; j < i; j++)
{
m[j] = new Mortgage();
Console.WriteLine("Mortgage Product #{0}", j+1);
m[j].Loan = Mortgage.GetDouble("Loan $", 10000, 1000000);
m[j].Rate = Mortgage.GetDouble("Rate %", 0.1, 25) / 100;
m[j].Term = Mortgage.GetInt("Term mo", 60, 360);
CompileInfo[j] = m[j].ToString();
}
//Create header and the top separator
Console.WriteLine(Header);
Console.WriteLine(Separator);
//Fill in the table by seperating the information that was stored in a single string
for (int k=0; k < i; k++)
{
string[] SeparateInfo = CompileInfo[k].Split(';');
Console.WriteLine(SeparateInfo[0].PadRight(field[0]) +
SeparateInfo[1].PadRight(field[1]) +
SeparateInfo[2].PadRight(field[2]) +
SeparateInfo[3].PadRight(field[3]) +
SeparateInfo[4].PadRight(field[4]) +
SeparateInfo[5].PadRight(field[5]));
}
//Create bottom separator
string Separator2 = " ".PadLeft((81), '=');
Console.WriteLine(Separator2);
}
}
집을 사기 위해 받는 대출(모기지-mortgage) 상품의 개수를 입력하면 그만큼의 loop를 형성하게 됩니다. 이후 loan (대출금액), rate(이자율), term month(상환하는 달 수)를 입력하면 매달 상환해야 하는 금액, 총 금액 및 이자와 함께 테이블에 저장하게 됩니다.
다음은 결과값입니다.