Hi,can somebody help me to call the boolean method in foreach in client. Here is my code:
Client

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {

            var list = new List<Tuple<int, string>>();

            list.Add(Tuple.Create(100, "Andy"));
            list.Add(Tuple.Create(200, "John"));
            list.Add(Tuple.Create(300, "Sally"));

            foreach (var item in list)
            {
                Console.WriteLine(item.Item1.ToString());
                Console.WriteLine(item.Item2);
            }

            Console.ReadLine();

}}}

Invoice class

using System;

namespace BusinessEntities
{
    public class Invoice
    {
        private int amount;
        private string name;
        private string creditCardNo;
        private DateTime expiryDate;

        public Invoice()
        {

        }
        public Invoice(int amount,string name)
        {
            this.amount = amount;
            this.name = name;
        }
        public int Amount  // public accessors
        {
            get { return amount; }  // get accessor
            set { amount = value; } // set accessor
        }
        public string Name // public accessors
        {
            get { return name; } // get accessor
            set { name = value; } // set accessor
        }
        public string CreditCardNo      // public accessor
        {
            get { return creditCardNo; }
        }
        public DateTime ExpiryDate      // public accessor
        {
            get { return expiryDate; }
        } // constructors not shown
    }
}

CustomerFactory

using Interfaces;
using System;

namespace FactoriesAndServiceManagers
{
public static class CustomerServiceFactory  // static class
{
public static ICustomerService Create()
      {
        return new CustomerServiceManager();
      }
}

public static class CardServiceFactory // static class
{
       public static ICreditCardValidationSErvice Create()
       {
             return new CreditCardValidationServiceManager();
       }
}

public class CustomerServiceManager : ICustomerService
{
        public bool ValidateCustomer(string customerName)
        {
            // Method under construction...
// Production code goes here which invokes Web 
// service and passes the customerName
            // If the customer is valid return true

            return true;  // for simplicity just return true now

            // if customer is invalid return false
            // return false
        }
}

public class CreditCardValidationServiceManager : 
ICreditCardValidationSErvice
{
        public bool ValidateCardTransaction(int amount, 
string name, string cardNo, DateTime expiryDate)
        {
        // Method under construction...            
// Production code goes here which invokes Web 
// service and passes the details.
            // If the transaction is valid return true

            return true; // for simplicity just return true now

            // if transaction is invalid return false
            // return false
        }

    }
}

ICustomerService

namespace Interfaces
{
    public interface ICustomerService
    {
        bool ValidateCustomer(string customerName);
    }

    public interface ICreditCardValidationSErvice
    {
        bool ValidateCardTransaction(int amount, string name,
string cardNo, System.DateTime expiryDate);
    }
}

InvoiceManager

using Interfaces;
using BusinessEntities;
using FactoriesAndServiceManagers;
using System;

namespace InvMgr
{
    public class InvoiceManager
    {
        private   ICustomerService custService;
        private   ICreditCardValidationSErvice cardService;

        public  InvoiceManager()
        {
            custService = CustomerServiceFactory.Create();  // factory
            cardService = CardServiceFactory.Create();  // factory
        }

        public  bool ProcessInvoice (Invoice anInvoice)
        {
            // Business logic goes here, only some logic shown. 
            //Stage 1 - Some basic validation on the invoice
            if ((anInvoice.Amount == 0) || (anInvoice.Name == null))  
            return false; // invalid invoice, failed basic validation

            //Stage 2 - Check the customer is a valid trading partner.
            if (!custService.ValidateCustomer(anInvoice.Name))
            return false; // invalid invoice - as customer is not 
// a valid trading partner

            //Stage 3 - Check the credit card is good for amount owed
            if (!cardService.ValidateCardTransaction(anInvoice.Amount,anInvoice.Name,anInvoice.CreditCardNo,anInvoice.ExpiryDate))
return false; // invalid invoice, card can't take 
  // transaction

// Gets to here, a good invoice so return true!
            return true;
        }

    }
}

So in client i managed to display 3 names and amount ,but how to call on each invoice in foreach the ProcessInvoice,to print date as well.I do not understand how to call that method inside foreach.I tried InvoiceManager.ProcessInvoice(?? ) but what to pass as the argument.

Recommended Answers

All 5 Replies

An Invoice object? It has to match the argument type in your method declaration -- public bool ProcessInvoice (Invoice anInvoice) {...}.

   Invoice invoice1 = new Invoice();
            Invoice invoice2 = new Invoice();
            Invoice invoice3 = new Invoice();

            invoice1.Amount = 200;
            invoice1.Name = "Bob";

            invoice2.Amount = 100;
            invoice2.Name = "Billy";

            invoice3.Amount = 500;
            invoice3.Name = "Karl";
            var List = new List<Tuple<int, string>>();
            List.Add(Tuple.Create(invoice1.Amount, invoice1.Name));
            List.Add(Tuple.Create(invoice2.Amount, invoice2.Name));
            List.Add(Tuple.Create(invoice3.Amount, invoice3.Name));

            foreach (var item in List)
            {
                InvoiceManager invoiceManager = new InvoiceManager();
                var result = InvoiceManager.ProcessInvoice(invoice1.Amount);//not accepting paramerts like this
                Console.WriteLine(item.Item1.ToString());
                Console.WriteLine(item.Item2);
            }

            Console.ReadLine();
            What should go into arguments in this code to match ? It does not work if i do as above.

According to your InvoiceManager class, the ProcessInvoice method accepts a parameter of type Invoice and returns a boolean.
invoice1.Amount on line 21 in the code above is of type int.

You're using Tuples with values from the invoice instances. This won't show up as invoice instances. You can make a List<Invoice> and add each instance you want to it. Also you were using the InvoiceManager class to call the method instead of the instance of the InvoiceManager:

var invoiceList = new List<Invoice>();
invoiceList.AddRange
    (new Invoice[]  
    {
        new Invoice(200, "Bob"),
        new Invoice(100, "Billy"),
        new Invoice(500,"Karl")
    }
    );
foreach (var invoice in List)
{
    InvoiceManager invoiceManager = new InvoiceManager();
    var result = invoiceManager.ProcessInvoice(invoice);
    Console.WriteLine(invoice.Amount.ToString());
    Console.WriteLine(invoice.Name);
}
Console.ReadLine();

Notice how the more descriptive names make it much clearer to understand the code.

Back to the same snippets above,i modified customer service factory :

public class CustomerServiceManager : ICustomerService
{

        public bool ValidateCustomer(string customerName)
        {
            ICustomerService service = CustomerServiceFactory.Create();
            // Method under construction...
            // Production code goes here which invokes Web 
            // service and passes the customerName
            // If the customer is valid return true
            if (service.ValidateCustomer(customerName))
            {return true;  }
            // for simplicity just return true now

            // if customer is invalid return false
            return false;
        }
}

And want to test this method,here is my logic,but it does not work for me :

      [TestMethod]
        public void ProcessInvoice_CustomerValidation()
        {
            var invoiceList = new List<Invoice>();
            invoiceList.AddRange
                (new Invoice[]
                  {
                  new Invoice(22, "Boby")

                  }
                );
            foreach (var invoice in invoiceList)
            {

                CustomerServiceManager csf = new CustomerServiceManager();

                 var result = csf.ValidateCustomer(invoice.Name);

                 if(invoiceList.Contains("Boby"))

               //Something is not right here

            }
              Assert.IsFalse(result, " Name is in the list");

        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.