Reputation: 67
Learning C# on my own (not homework). I wrote a TotalDue method to calculate grand total of all customer balances due (from array). Placed it within the Customer class so it would have access to the data. I cannot figure out how to call this method in main. How do I get the total to display?
class Program
{
static void Main(string[] args)
{
Customer[] customers = new Customer[2];
string customer;
int id;
double due;
// GET DATA AND FILL ARRAY
for (int x = 0; x < customers.Length; ++x)
{
GetData(out customer, out id, out due);
customers[x] = new Customer(customer, id, due);
}
// SORT ARRAY - NEEDS ICOMPARABLE<Customer> - PART 1
Array.Sort(customers);
// PRINT ARRAY WITH TOSTRING() OVERRIDE
for (int x = 0; x < customers.Length; ++x)
{
Console.WriteLine(customers[x].ToString());
}
//DON'T KNOW HOW TO CALL THE TOTAL DUE METHOD...
Console.ReadLine();
}
class Customer : IComparable<Customer> // SORT ARRAY - PART 2
{
private string CustomerName { get; set; }
private int IdNumber { get; set; }
private double BalanceDue { get; set; }
// CONSTRUCTOR
public Customer(string customer, int id, double due)
{
CustomerName = customer;
IdNumber = id;
BalanceDue = due;
}
//SORT THE ARRAY - PART 3
public int CompareTo(Customer x)
{
return this.IdNumber.CompareTo(x.IdNumber);
}
// OVERRIDE TOSTRING TO INCLUDE ALL INFO + TOTAL
public override string ToString()
{
return ("\nCustomer: " + CustomerName + "\nID Number: " + IdNumber + "\nBalance Due: " + BalanceDue.ToString("C2"));
}
// TOTAL DUE FOR ALL CUSTOMERS
static void TotalDue(Customer [] customers)
{
double Total = 0;
for (int x = 0; x < customers.Length; ++x)
Total += customers[x].BalanceDue;
Console.WriteLine("Total Amount Due: {0}", Total.ToString("C2"));
}
}
// GET DATA METHOD
static void GetData(out string customer, out int id, out double due)
{
Console.Write("Please enter Customer Name: ");
customer = Console.ReadLine();
Console.Write("Please enter ID Number: ");
int.TryParse(Console.ReadLine(), out id);
Console.Write("Please enter Balance Due: $");
double.TryParse(Console.ReadLine(), out due);
}
}
Upvotes: 0
Views: 118
Reputation: 3781
Make TotalDue
method public as the default access modifier in C# is private and then try this.
class Customer : IComparable<Customer> // SORT ARRAY - PART 2
{
public static void TotalDue(Customer [] customers)
{
double Total = 0;
for (int x = 0; x < customers.Length; ++x)
Total += customers[x].BalanceDue;
Console.WriteLine("Total Amount Due: {0}", Total.ToString("C2"));
}
}
static void Main(string[] args)
{
// ...........
//............
Customer.TotalDue(customers);
}
Upvotes: 2
Reputation: 3772
Add public access modifier with your static method and call it with name of class or remove the static keyword and make it instance and call class method.
Upvotes: 0
Reputation:
The default access modifier in C# is private
. Since your TotalDue
method does not specify anything else, it is private
. You can change this to public
and call it from your Main
.
Upvotes: 0
Reputation: 1916
make your Customer class method (TotalDue) Public and NOT static...
Upvotes: -1