How to call method that is in a class - c#

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);
}
}

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);
}

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.

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.

make your Customer class method (TotalDue) Public and NOT static...

Related

adding objects in a list, am i doing it right?

namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("how many footballs would you want?");
int amount = int.Parse(Console.ReadLine());
List<football> ballist = new List<football>();
for (int i = 0; i < amount; i++)
{
Console.WriteLine("how much should football {0} weigh?", i+1);
int weight = int.Parse(Console.ReadLine());
ballist.Add(new football(weight));
}
Console.WriteLine("amount of footballs is {0}", amount);
ballist.ForEach(s => Console.WriteLine(s.GetWeight()));
Console.ReadLine();
}
}
class football
{
private int weight = 0;
public int GetWeight()
{
return weight;
}
public football(int weigh)
{
weight = weigh;
}
}
}
adding objects in a list, am i doing it right?
A possible alternative is to let user input all the weights in one go and generate the list:
Console.WriteLine("please, input footballs' weights separated by comma");
String input = Console.ReadLine();
List<football> ballist = input
.Split(',')
.Select(item => new football(int.Parse(item)))
.ToList();
Some suggestions on Football class
// We usually start classes with capital letter
class Football {
private int m_Weight;
// C# is not Java, so use properties, which are more readable
public int Weight {
get {
return m_Weight;
}
private set {
// validate input
if (value <= 0)
throw new ArgumentOutOfRangeException("value");
m_Weight = value;
}
}
// "weight" - let argument correspond to property
public football(int weight) {
Weight = weight;
}
}
The adding to the list is right. I would recomend you to use property for the Weight.
class football
{
public int weight { get; set; }
}
If you dont want to have any code on the get/set.
public int _weight;
public int weight
{
get
{
//Your Code here
return _weight;
}
set
{
//And Here
_weight = value;
}
}

containing type does not implement interface 'System.IComparable' error

namespace Theprogram.cs
{
class Program
{
static void Main(string[] args)
{
CreditCustomer[] creditCustomer = new CreditCustomer[5];
int x, y;
bool goodNum;
for (x = 0; x < creditCustomer.Length; ++x)
{
creditCustomer[x] = new CreditCustomer();
Console.Write("Enter customer number ");
creditCustomer[x].CustomerNumber = Convert.ToInt32(Console.ReadLine());
goodNum = true;
for (y = 0; y < x; ++y)
{
if (creditCustomer[x].Equals(creditCustomer[y]))
goodNum = false;
}
while (!goodNum)
{
Console.Write("Sorry, the customer number " +
creditCustomer[x].CustomerNumber + " is a duplicate. " +
"\nPlease reenter ");
creditCustomer[x].CustomerNumber = Convert.ToInt32(Console.ReadLine());
goodNum = true;
for (y = 0; y < x; ++y)
{
if (creditCustomer[x].Equals(creditCustomer[y]))
goodNum = false;
}
}
Console.Write("Enter name ");
creditCustomer[x].CustomerName = Console.ReadLine();
Console.Write("Enter age ");
creditCustomer[x].Rate = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter amount due ");
creditCustomer[x].AmountDue = Convert.ToDouble(Console.ReadLine());
}
Array.Sort(creditCustomer);
Array.Sort(customer);
}
}
class Customer : IComparable<Customer>
{
private int customerNumber;
public int CustomerNumber
{
get
{
return customerNumber;
}
set
{
customerNumber = value;
}
}
private string customerName;
public string CustomerName
{
get
{
return customerName;
}
set
{
customerName = value;
}
}
private double amountDue;
public double AmountDue
{
get
{
return amountDue;
}
set
{
amountDue = value;
}
}
public Customer(int num, string name, int amt)
{
CustomerNumber = num;
CustomerName = name;
AmountDue = amt;
}
public Customer(): this(9, "ZZZ", 0)
{
}
public override bool Equals(Object e)
{
bool equal;
Customer temp = (Customer)e;
if (this.CustomerNumber == temp.CustomerNumber)
equal = true;
else
equal = false;
return equal;
}
public override int GetHashCode()
{
return CustomerNumber;
}
public override string ToString()
{
return (GetType() + " Credit Customer " + CustomerNumber + " " + CustomerName +
" Amount Due is " + AmountDue.ToString("C") + " Interest rate is ");
}
protected virtual int IComparable.CompareTo(Object o)
{
int returnVal;
Customer temp = (Customer)o;
if (this.CustomerNumber >
temp.CustomerNumber)
returnVal = 1;
else
if (this.CustomerNumber < temp.CustomerNumber)
returnVal = -1;
else
returnVal = 0;
return returnVal;
}
}
class CreditCustomer : Customer, IComparable<CreditCustomer>
{
public int Rate {get; set;}
int MonthlyPayment { get; set; }
public CreditCustomer(int customerNumber, string customerName, int amountDue, int Rate) : base(customerNumber, customerName, amountDue)
{ }
public CreditCustomer(): this(0, "", 0, 0)
{ }
public override string ToString()
{
return (GetType() + " Credit Customer " + CustomerNumber + " " + CustomerName +
" Amount Due is " + AmountDue.ToString("C") + " Interest rate is " + Rate + " Monthly Payment is " + MonthlyPayment);
}
int IComparable.CompareTo(object o)
{
int returnVal;
CreditCustomer temp = (CreditCustomer)o;
if (this.CustomerNumber > temp.CustomerNumber)
returnVal = 1;
else
if (this.CustomerNumber < temp.CustomerNumber)
returnVal = -1;
else
returnVal = 0;
return returnVal;
}
}
}
I am having trouble implementing Icomparable in C# for my parent and child classes as I get the error:
does not implement interface member'System.IComparable.CompareTo
I really need help on how to implement the Icomparable interface so that I can use the compareTo method on my array of object. Any help would be greatly appreciated.
If you're using Visual Studio and are not sure how to implement an interface, just right click IComparable in the class declaration
class CreditCustomer : Customer, IComparable<CreditCustomer>
and select Implement Interface and any of the two options.
The correct signature for the implementation in CreditCustomer would be either:
public int CompareTo(CreditCustomer other)
{
}
or
int IComparable<CreditCustomer>.CompareTo(CreditCustomer other)
{
}
Note that the second signature would hide the method unless you cast the object to IComparable<CreditCustomer>.
As SLaks mentioned in his comment, there are, in fact, two different interfaces that are named IComparable which can be easily confused:
IComparable is an older, non-generic interface which allows comparison between an object and any other object. There's no type checking - your CreditCustomer can be compared to an int or a List<string> or anything. This interface defines a single method, CompareTo(object obj). This is the method you have in your class.
IComparable<T> is a newer, generic interface. It's very similar to IComparable, but enforces type checking - it only compares T to T, so you don't have to write a lot of boilerplate code making sure that someone didn't try to compare your CreditCustomer to a Dog or something. It also defines a single method - CompareTo(T obj). This is the interface that you marked your class as implementing.
Despite being similarly named, these two are, as far as the compiler is concerned, two different interfaces. If you don't have a CompareTo method that accepts a CreditCustomer argument, you're not implementing IComparable<CreditCustomer>, and that's why it's giving you an error. Either change your method signature to match the interface, or change the interface you're using to one that matches the method signature. Probably the former.

Storing user-provided values

I am trying to store user-provided values in order to calculate their sum and average for an assignment. I am able to print the values, but not store them.
I am not looking for the answer, but only for guidance.
Thank you for any and all responses.
public class average {
public int num;
public double avg;
public average() { } // no argument method
public average(int nums, double avgt) {
num = nums;
avg = avgt;
}
public int Num {
get { return num; } // used to get the contents from the main()
set { num = value; }
}
public double Avg {
get { return avg; }
set { avg = value; }
}
public void print_info() { // prints the final results of code
// Cannot get the values to store properly
Console.WriteLine("The average is {0}, the sum of 10 numbers is {1}", (avg/10),num);
Console.WriteLine("Press any key to continue . . .");
Console.ReadLine();
}
{class assignment1 // wrapper class for main
static void Main(string[] args) {
int num0;
int val;
int nums = 0;
double avgt = 0.0;
average num1 = new average(nums, avgt);
for (val = 0; val < 10; val++) { // loop for user input
Console.Write("Enter an integer value: ");
num0 = Convert.ToInt32(Console.ReadLine());
}
num1.print_info(); // calls print_info() method
}
}
}

Refer variable from different class

using System;
namespace Matrix_Algebra
{
public struct S_Matrix_size
{
public int size_R, size_C;
public S_Matrix_size(int r, int c)
{
this.size_R = r;
this.size_C = c;
}
}
public class C_Matrix_entries
{
public C_Matrix_entries()
{
int r, c;
Console.WriteLine("Enter number of rows and columns ");
r = Convert.ToInt32(Console.ReadLine());
c = Convert.ToInt32(Console.ReadLine());
S_Matrix_size size = new S_Matrix_size(r,c);
double[,] entry = new double [size.size_R,size.size_C];
Console.WriteLine("Enter the entries from first row [left to right] to the last row ");
for (int i = 0; i<size.size_R; i++)
{
Console.WriteLine("Enter the {0} row", i + 1);
for (int j = 0; j<size.size_C;j++)
{
entry[i, j] = Convert.ToDouble(Console.ReadLine());
}
}
}
}
}
namespace Row_Reduce_Algebra
{
using Matrix_Algebra;
public class TEST
{
static void Main()
{
C_Matrix_entries matrix_no1 = new C_Matrix_entries();
for (int i = 0; i < **matrix_no1.size**; i++)
{
}
}
}
}
As the title says, I'm trying to access a variable from a class instance, but don't know how to do it properly.
You can't access matrix_no1.size because size is inaccessible.
Add a public property to your C_Matrix_entries class, and reference it in Main().
public class C_Matrix_entries
{
public S_Matrix_size size { get; private set; }
public C_Matrix_entries()
{
...
size = new S_Matrix_size(r,c);
As #GrantWinney rightfully pointed out (as I was shaping up a working reply for you), you cannot access matrix_no1.size because it is inaccessible. (It is also out of scope being that matrix_no1 is a local variable declared in the default C_Matrix_entries constructor.)
Based on your code, here is an end-to-end working example of how to fix the problem using a somewhat different public property added to C_Matrix_entries. Beyond the flavor of the new S_Matrix_size public property you add to C_Matrix_entries (i.e. Grant Winney's will work too), you will need to compute the product of its size_R and size_C properties in your loop's setup.
using System;
namespace Matrix_Algebra
{
public struct S_Matrix_size
{
public int size_R, size_C;
public S_Matrix_size(int r, int c)
{
this.size_R = r;
this.size_C = c;
}
}
public class C_Matrix_entries
{
private S_Matrix_size _size;
public C_Matrix_entries()
{
int r, c;
Console.WriteLine("Enter number of rows and columns ");
r = Convert.ToInt32(Console.ReadLine());
c = Convert.ToInt32(Console.ReadLine());
_size = new S_Matrix_size(r,c);
double[,] entry = new double [_size.size_R,_size.size_C];
Console.WriteLine("Enter the entries from first row [left to right] to the last row ");
for (int i = 0; i<_size.size_R; i++)
{
Console.WriteLine("Enter the {0} row", i + 1);
for (int j = 0; j<_size.size_C;j++)
{
entry[i, j] = Convert.ToDouble(Console.ReadLine());
}
}
}
public S_Matrix_size Size { get { return _size; } }
}
}
namespace Row_Reduce_Algebra
{
using Matrix_Algebra;
public class TEST
{
static void Main()
{
C_Matrix_entries matrix_no1 = new C_Matrix_entries();
for (int i = 0; i < matrix_no1.Size.size_R * matrix_no1.Size.size_C; i++)
{
// TODO: something useful
Console.WriteLine(i); // FORNOW
}
}
}
}

Comparing user input to previously entered values

I am having a problem getting my program to check the input previous and compare it so the user cannot duplicate an order number. Right now the program will run all the way through and look the way I want, but it accepts duplicate order numbers while I would like it to inform the user and ask them to reenter. The problem seems to be that my check[y] array does not contain any value when I compare it. How do I get this array to contain the previously entered order numbers so it will display the error message. I am just going to post the entire code so you can see what I have done so far. Others have suggested the List type, but I am a student and have not learned this yet. I believe I am supposed to use the Equals method. Any help would be appreciated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assignment6_Hergott
{
class Order : IComparable <Order>
{
public int orderNumber { get; set; }
public string customerName { get; set; }
public int quanityOrdered { get; set; }
public double total;
public const double priceEach = 19.95;
public Order()
{
}
public Order(int number, string name, int quanity)
{
number = orderNumber;
name = customerName;
quanity = quanityOrdered;
}
public double totalPrice
{
get
{
return total;
}
}
public int CompareTo(Order o)
{
return this.orderNumber.CompareTo(o.orderNumber);
}
public override bool Equals(Object e)
{
bool equal;
Order temp = (Order)e;
if (orderNumber == temp.orderNumber)
equal = true;
else
equal = false;
return equal;
}
public override int GetHashCode()
{
return Convert.ToInt32(orderNumber);
}
public override string ToString()
{
return "ShippedOrder " + orderNumber + " " + customerName + " " + quanityOrdered +
" # " + priceEach + " each.";
}
}
class ShippedOrder : Order
{
public const int shippingFee = 4;
public override string ToString()
{
return base.ToString() + " Shipping is " + shippingFee + " Total is " + totalPrice;
}
}
class Program
{
static void Main(string[] args)
{
double sum = 0;
ShippedOrder[] orderArray = new ShippedOrder[5];
ShippedOrder[] check = new ShippedOrder[5];
bool wrong = true;
for (int x = 0; x < orderArray.Length; ++x)
{
orderArray[x] = new ShippedOrder();
Console.Write("Enter order number: ");
orderArray[x].orderNumber = Convert.ToInt32(Console.ReadLine());
for (int y = 0; y < x; y++)
{
check[y] = new ShippedOrder();
if (orderArray[x].Equals(check[y]))
wrong = false;
while (!wrong)
{
Console.WriteLine("Sorry, the order number {0} is a duplicate.
\nPlease reenter: ", orderArray[x].orderNumber);
for (y = 0; y < x; y++)
{
if (orderArray[x].Equals(check[y]))
wrong = false;
}
check[y] = orderArray[x];
}
}
Console.Write("Enter cusomer name: ");
orderArray[x].customerName = Console.ReadLine();
Console.Write("Enter quanity: ");
orderArray[x].quanityOrdered = Convert.ToInt32(Console.ReadLine());
orderArray[x].total = orderArray[x].quanityOrdered * Order.priceEach +
ShippedOrder.shippingFee;
sum += orderArray[x].total;
}
Array.Sort(orderArray);
for (int x = 0; x < orderArray.Length; x++)
{
Console.WriteLine(orderArray[x].ToString());
}
Console.WriteLine();
Console.WriteLine("Total for all orders is {0:c} ", sum);
}
}
}
I had a few minutes so I changed my answer to show you one way it could be done. If you just copy/paste this you'll be doing yourself a disservice, though (and your instructor will probably be able to tell). Take a look at the solution and see how it differs from yours. I hesitated to post a full solution but I thought this might be an okay way for you to figure out what you'd done wrong.
namespace ConsoleApplication2
{
using System;
using System.Linq;
public class Order : IComparable<Order>
{
public const double PriceEach = 19.95;
public Order()
{
}
public Order(int number, string name, int quanity)
{
this.OrderNumber = number;
this.CustomerName = name;
this.QuanityOrdered = quanity;
}
public int OrderNumber { get; set; }
public string CustomerName { get; set; }
public int QuanityOrdered { get; set; }
public int CompareTo(Order o)
{
return this.OrderNumber.CompareTo(o.OrderNumber);
}
public override bool Equals(object e)
{
int compareTo;
int.TryParse(e.ToString(), out compareTo);
return this.OrderNumber == compareTo;
}
public override int GetHashCode()
{
return Convert.ToInt32(this.OrderNumber);
}
public override string ToString()
{
return "Shipped order number " + this.OrderNumber + " for customer " + this.CustomerName + " " + this.QuanityOrdered +
" # $" + PriceEach + " each.";
}
}
public class ShippedOrder : Order
{
public const int ShippingFee = 4;
public double TotalPrice
{
get
{
return (this.QuanityOrdered * PriceEach) + ShippingFee;
}
}
public override string ToString()
{
return base.ToString() + " Shipping is $" + ShippingFee + ". Total is $" + this.TotalPrice;
}
}
public class Program
{
private static readonly int[] OrderNumbers = new int[5];
private static readonly ShippedOrder[] ShippedOrders = new ShippedOrder[5];
public static void Main(string[] args)
{
double sum = 0;
for (var i = 0; i < OrderNumbers.Length; i++)
{
OrderNumbers[i] = InputOrderNumber();
var name = InputCustomerName();
var quantity = InputQuantity();
ShippedOrders[i] = new ShippedOrder { CustomerName = name, QuanityOrdered = quantity, OrderNumber = OrderNumbers[i] };
sum += ShippedOrders[i].TotalPrice;
}
Array.Sort(ShippedOrders);
foreach (var t in ShippedOrders)
{
Console.WriteLine(t.ToString());
}
Console.WriteLine();
Console.WriteLine("Total for all orders is {0:c} ", sum);
Console.WriteLine();
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
private static int InputOrderNumber()
{
Console.Write("Enter order number: ");
var parsedOrderNumber = InputNumber();
if (ShippedOrders.Any(shippedOrder => shippedOrder != null && shippedOrder.OrderNumber.Equals(parsedOrderNumber)))
{
Console.WriteLine("Order number {0} is a duplicate.", parsedOrderNumber);
return InputOrderNumber();
}
return parsedOrderNumber;
}
private static string InputCustomerName()
{
Console.Write("Enter customer name: ");
var customerName = Console.ReadLine();
if (customerName == null || string.IsNullOrEmpty(customerName.Trim()))
{
Console.WriteLine("Customer name may not be blank.");
return InputCustomerName();
}
return customerName;
}
private static int InputQuantity()
{
Console.Write("Enter quantity: ");
return InputNumber();
}
private static int InputNumber()
{
int parsedInput;
var input = Console.ReadLine();
if (!int.TryParse(input, out parsedInput))
{
Console.WriteLine("Enter a valid number.");
return InputNumber();
}
return parsedInput;
}
}
}
my check[y] array does not contain any value when I compare it. How do
I get this array to contain the previously entered order numbers
If you want check[] to contain order numbers, it needs to be of the type of order number (an int, in this case). So whenever you add a new order to the orderArray, also add its number to the check array. Then you can test against earlier numbers.
If this doesn't solve your problem, add a follow-up question as a comment telling us what you tried and we can go from there.

Categories

Resources