2012-12-04 4 views
0
public mainForm() 
    { 
     InitializeComponent(); 
    } 
    string[,] summary = new string[10, 4]; 


    private void exitButton_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 

    private void calculateButton_Click(object sender, EventArgs e) 
    { 
      decimal monthlyInvestment = 
        Convert.ToDecimal (monthlyInvestmentTextBox.Text); 
      decimal yearlyInterestRate = 
        Convert.ToDecimal (interestRateTextBox.Text); 
      int years = 
       Convert.ToInt32(yearsTextBox.Text); 

      int months = years * 12; 
      decimal monthlyInterestRate = yearlyInterestRate/12/100; 

      decimal futureValue = 0; 
      for (int i = 0; i < months; i++) 
      { 
       futureValue = (futureValue + monthlyInvestment) 
         * (1 + monthlyInterestRate); 
      } 
      futureValueTextBox.Text = futureValue.ToString("c"); 
      monthlyInvestmentTextBox.Focus(); 
    } 

이 프로그램은 비율과 연도에 따라 투자의 미래 가치를 계산합니다. 사용자가 계산을 히트하면 배열 10x4에 최대 10 개의 계산을 저장해야합니다. 4 투자, 이자율, 연도 및 미래 가치. 나가기 버튼을 클릭하면 이전 계산으로 10 메시지 상자가 나타납니다. 이 일을 어떻게 하죠? 감사.직사각형 배열을 사용하여 값 저장

+2

사각형의 배열을 사용하지 말고 투자, 비율, 연도 및 미래 값에 대한 속성으로 'FutureInvestments' 클래스를 만듭니다. 그런 다음 10 개를 저장해야하는 경우 List

답변

0
public class FutureInvestments 
{ 
public decimal monthlyInvestment {get;set;} 
public decimal yearlyInterestRate {get;set;} 
public decimal futureValue {get;set;} 
public decimal monthlyInterestRate {get;set;} 
public decimal monthlyInvestment {get;set;} 

    public string Calculate() 
    { 
    int months = years * 12; 
    monthlyInterestRate = yearlyInterestRate/12/100; 
    for (int i = 0; i < months; i++) 
    { 
    futureValue = (futureValue + monthlyInvestment) * (1 + monthlyInterestRate); 
    } 
    return futureValue.ToString("c"); 
    } 
} 

public mainForm() 
{ 
    InitializeComponent(); 
} 

List<FutureInvestments> summary = new List<FutureInvestments>(); 

private void calculateButton_Click(object sender, EventArgs e) 
{ 
    //Instantiate class - passing in ReadOnly parameters 
    var futureInv = new FutureInvestment() {monthlyInvestment = Convert.ToDecimal (monthlyInvestmentTextBox.Text), monthlyInvestment = Convert.ToDecimal(monthlyInvestmentTextBox.Text), yearlyInterestRate = Convert.ToDecimal (interestRateTextBox.Text) years = Convert.ToInt32(yearsTextBox.Text)); 

    futureInv.Calculate(); 

    //Store the calculation for showing user when application closes 
    summary.Add(futureInv); 
} 

private void exitButton_Click(object sender, EventArgs e) 
{ 
    foreach(var item in summary) 
    { 
     MessageBox.Show("Future value is: " + item.FutureValue.ToString()); 
    } 
    this.Close(); 
} 
0

@ 제레미 톰슨이 맞습니다. 클래스가 UI에 대한 것임을 강조하고, Investment 클래스는 다른 모든 것을 처리합니다.

public partial class mainForm : Form 
{ 
    private Stack<Investment> Investments; 

    public mainForm() 
    { 
     InitializeComponent(); 
     Investments = new Stack<Investment>(); 
    } 

    private void calculateButton_Click(object sender, EventArgs e) 
    { 
     Investment thisInvestment = new Investment(Convert.ToDecimal(monthlyInvestmentTextBox.Text), 
                Convert.ToDecimal(interestRateTextBox.Text), 
                Convert.ToInt32(yearsTextBox.Text)); 
     Investments.Push(thisInvestment); 
     futureValueTextBox.Text = thisInvestment.futureValue.ToString("c"); 
     monthlyInvestmentTextBox.Focus(); 

    } 

    private void exitButton_Click(object sender, EventArgs e) 
    { 
     int count = Math.Min(10, Investments.Count); 
     StringBuilder sb = new StringBuilder(); 
     for (int i = 1; i <= count; i++) 
     { 
      sb.AppendLine(Investments.Pop().ToString()); 
     } 
     MessageBox.Show(sb.ToString()); 
    } 
} 

public class Investment 
{ 
    public Investment(decimal monthlyInvestment, decimal yearlyInterestRate, int years) 
    { 
     this.monthlyInvestment = monthlyInvestment; 
     this.yearlyInterestRate = yearlyInterestRate; 
     this.years = years; 
    } 

    public decimal monthlyInvestment { get; set; } 
    public decimal yearlyInterestRate { get; set; } 
    public decimal monthlyInterestRate 
    { 
     get 
     { 
      return yearlyInterestRate/12/100; 
     } 
    } 
    public int years { get; set; } 
    public int months 
    { 
     get 
     { 
      return years * 12; 
     } 
    } 
    public decimal futureValue 
    { 
     get 
     { 
      decimal retVal = 0; 
      for (int i = 0; i < months; i++) 
      { 
       retVal = (futureValue + monthlyInvestment) 
         * (1 + monthlyInterestRate); 
      } 
      return retVal; 
     } 
    } 

    public override string ToString() 
    { 
     return string.Format("Investment of {0:c} for {1:n} years at {2:p} pa yields {3:c}", monthlyInvestment,years,monthlyInterestRate,futureValue); 
    } 
} 
관련 문제