这是我的主要课程。
我基于我之前做过的另一个例子,它以同样的方式工作,这是我第一次使用arraylist所以也许它与我有关并且我不确定。我只需要它来显示和arraylist工作正常我认为其他一切正常。
现在我收到错误:
Console.WriteLine("{0,-12} {1,-12} {2,-12} {3,-12} {4,-12} {5,-12} {6,-12}",
plan[idx].GetCardNumber(), plan[idx].GetCreditBalance(), plan[idx].GetMonthlyPayment(),
plan[idx].GetAnnualRate(), plan[idx].MonthsToPayOff(), plan[idx].TotalPayment(),
plan[idx].CompanyProfit());
说:
'对象'不包含___的定义,例如(.getcreditbalance
完整的计划:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace Assignment1
{
class Program
{
static void Main(string[] args)
{
ArrayList plan = new ArrayList();
int count = 0;
char option;
Console.WriteLine("--= Credit Card Calculator =-- ");
Console.WriteLine();
do
{
option = ReadMenuOption();
switch (option)
{
case 'E':
count = count + 1;
AddPaymentPlan(count, plan);
break;
case 'V':
Display(count, plan);
break;
case 'X':
Console.WriteLine("Goodybye.");
break;
default:
Console.WriteLine("Invalid option.");
break;
}
} while (option != 'X');
}
static char ReadMenuOption()
{
char option;
Console.WriteLine("Options Menu");
Console.WriteLine("====================");
Console.WriteLine("E - Enter calculation information");
Console.WriteLine("V - View calculation report");
Console.WriteLine("X - Exit");
option = Console.ReadLine().ToUpper()[0];
return option;
}
static void Display(int count, ArrayList plan)
{
if (count == 0)
{
Console.WriteLine("There are no values to show!");
}
else
{
for (int idx = 0; idx < plan.Count; idx = idx + 1)
{
Console.WriteLine("{0,-12} {1,-12} {2,-12} {3,-12} {4,-12} {5,12} {6,-12}", " Card Number", "Card Balance", "Monthly Payment", "APR", "Months to Pay", "Total Payment", "Company Profit");
Console.WriteLine("{0,-12} {1,-12} {2,-12} {3,-12} {4,-12} {5,-12} {6,-12}", plan[idx].GetCardNumber(), plan[idx].GetCreditBalance(), plan[idx].GetMonthlyPayment(), plan[idx].GetAnnualRate(), plan[idx].MonthsToPayOff(), plan[idx].TotalPayment(), plan[idx].CompanyProfit());
}
}
}
static int AddPaymentPlan(int count, ArrayList plan)
{
PaymentPlan p = new PaymentPlan();
do
{
try
{
Console.Write("Enter card number: ");
p.SetCardNumber(long.Parse(Console.ReadLine()));
}
catch
{
Console.WriteLine("Invalid card number");
}
} while (p.GetCardNumber().Equals(""));
do
{
try
{
Console.Write("Enter card balance: ");
p.SetCreditBalance(double.Parse(Console.ReadLine()));
}
catch
{
Console.WriteLine("Invalid card balance.");
}
} while (p.GetCreditBalance().Equals(""));
do
{
try
{
Console.Write("Enter payment amount: ");
p.SetMonthlyPayment(double.Parse(Console.ReadLine()));
}
catch
{
Console.WriteLine("Invalid payment amount.");
}
} while (p.GetMonthlyPayment().Equals(""));
do
{
try
{
Console.Write("Enter annual rate: ");
p.SetAnnualRate(double.Parse(Console.ReadLine()));
}
catch
{
Console.WriteLine("Invalid");
}
} while (p.GetAnnualRate().Equals(""));
plan.Add(p);
Console.WriteLine("It will take {0} months to pay off your credit card", p.MonthsToPayOff());
Console.WriteLine();
count = count + 1;
return count;
}
}
}
and my Payment plan class :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment1
{
class PaymentPlan
{
private long _cardNumber;
private double _creditBalance;
private double _monthlyPayment;
private double _annualRate;
public PaymentPlan()
{
_cardNumber = 0;
_creditBalance = 0;
_monthlyPayment = 0;
_annualRate = 0;
}
public PaymentPlan(long cardNumber, double creditBalance, double monthlyPayment, double annualRate)
{
SetCardNumber(cardNumber);
SetCreditBalance(creditBalance);
SetMonthlyPayment(monthlyPayment);
SetAnnualRate(annualRate);
}
public long GetCardNumber()
{
return _cardNumber;
}
public void SetCardNumber(long cardNumber)
{
if (cardNumber < 0)
{
throw new Exception("Has to be a positive number");
}
_cardNumber = cardNumber;
}
public double GetCreditBalance()
{
return _creditBalance;
}
public void SetCreditBalance(double creditBalance)
{
if (creditBalance < 0)
{
throw new Exception("Has to be a positive number");
}
_creditBalance = creditBalance;
}
public double GetMonthlyPayment()
{
return _monthlyPayment;
}
public void SetMonthlyPayment(double monthlyPayment)
{
if (monthlyPayment < 0)
{
throw new Exception("Has to be a positive number");
}
_monthlyPayment = monthlyPayment;
}
public double GetAnnualRate()
{
return _annualRate;
}
public void SetAnnualRate(double annualRate)
{
if (annualRate < 0 || annualRate > 1)
{
throw new Exception("Has to be a positive number between 0.0 & 1.0");
}
_annualRate = annualRate;
}
public double MonthsToPayOff()
{
double months;
double daily;
double monthly;
double tinycalc;
double innerbrac;
double bottom;
double upperbrac;
daily = _annualRate / 365;
//calculates number of months to be returned to main and displayed for user
monthly = _creditBalance / _monthlyPayment;
tinycalc = -1.0 / 30;
innerbrac = Math.Pow(1 + daily, 30);
bottom = Math.Log(1 + daily);
upperbrac = Math.Log(1 + (_monthlyPayment * (1 - innerbrac)));
months = Math.Round(tinycalc * (upperbrac / bottom));
return months;
}
public double TotalPayment()
{
return GetCreditBalance() * (MonthsToPayOff() * GetMonthlyPayment());
}
public double CompanyProfit()
{
return TotalPayment() - _creditBalance;
}
}
}
答案 0 :(得分:3)
ArrayList
是存储集合的过时方式。
每个项目都存储为object
,在您将项目转换回原始类型之前,您无法访问原始类型的任何属性或方法。
((PaymentPlan)plan[idx]).GetCardNumber()
更好的是,将ArrayList
替换为List<PaymentPlan>
。 List<T>
构造是类型安全的,因此您不必在每次要访问时使用它们来调用原始类型和强制转换项目。
var plans = new List<PaymentPlan>();
plans.Add(new PaymentPlan(12345, ...));
Console.WriteLine(plans[idx].GetCardNumber());