Correct placement of Methods in OOP

Which Class should contain a method which has dependencies on multiple classes?

I’ve coded up a specific example to avoid generalizations. In this example we have an initial requirement :

customers have accounts and can purchase orders, which contain items.

The application is coded up as per OOP principles. Since the purchase requires an account, which is owned by a customer and the requirement is phrased in terms of a customer making a purchase, customer seems like a good place to put it.

However :

Added Requirement : An advance order can be reserved by a customer if
they have enough funds and purchased at a later date. an advance order
can’t be purchased if it has not been reserved.

Once we add the logic for the advance order the placement of the method on customer seems less certain. We had to re-factor quite a bit to get it in and have a duplicate method ‘Purchase’ on Order

Is having Purchase on Customer consistent with OOP principles and good design? Or is there a better place to put it, do we need a new class?

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace OOP
{
#region Repository Assembly
    public class OrderRepository : IOrderRepository
    {
        public void Add(Order order) { }
    }
    public class AccountRepository : IAccountRepository
    {
        public void Update(Account account) { }
    }
#endregion

#region Models Assembly
    public interface IOrderRepository
    {
        void Add(Order order);
    }

    public interface IAccountRepository
    {
        void Update(Account account);
    }

    public class Customer
    {
        public string Id { get; private set; }
        public string Name { get; private set; }
        public Account Account { get; private set; }

        public Customer(Account account)
        {
            this.Account = account;
        }

        public void Purchase(Order order)
        {
            if (Account.HasFundsAvailable(order.TotalPrice()))
            {
                Account.Debit(order.TotalPrice());
                order.Purchase();
            }
            else
            {
                throw new Exception("not enough funds in account");
            }
        }

        /// added code
        public void Purchase(AdvanceOrder order)
        {
            if (!order.IsReserved())
            {
                throw new Exception("must reserve advanced orders");
            }
            if (Account.HasFundsAvailable(order.TotalPrice()))
            {
                order.Purchase();
            }
            else
            {
                throw new Exception("not enough funds in account");
            }
        }

        /// added code
        public void Reserve(AdvanceOrder order)
        {
            if (Account.HasFundsAvailable(order.TotalPrice()))
            {
                order.Reserve();
            }
            else
            {
                throw new Exception("not enough funds in account");
            }
        }
    }

    public class Account
    {
        private IAccountRepository rep;
        public string Id { get; private set; }
        public string CustomerId { get; private set; }
        public decimal Balance { get; private set; }

        public List<Transaction> Transactions { get; private set; }

        public Account(IAccountRepository rep, decimal balance, List<Transaction> Transactions)
        {
            this.rep = rep;
            this.Balance = balance;
            this.Transactions = Transactions;
        }

        public bool HasFundsAvailable(decimal amount)
        {
            if (this.Balance >= amount)
            {
                return true;
            }
            return false;
        }

        public void Debit(decimal amount)
        {
            if (this.HasFundsAvailable(amount))
            {
                Transaction t = new Transaction(amount);
                this.Transactions.Add(t);
                this.rep.Update(this);
            }
            else
            {
                throw new Exception("not enough funds");
            }
        }
    }

    public class Transaction
    {
        public Transaction(decimal amount)
        {
            this.Amount = amount;
        }
        public string Id { get; private set; }
        public decimal Amount { get; private set; }
    }

    public class Order
    {
        protected IOrderRepository rep;
        public string Id { get; private set; }
        public string Status { get; protected set; }
        public IEnumerable<Item> Items { get; private set; }

        public Order(IOrderRepository rep, string status, List<Item> items)
        {
            this.rep = rep;
            this.Status = status;
            this.Items = items;
        }

        public decimal TotalPrice()
        {
            return this.Items.Sum(i => i.Price);
        }

        public void Purchase()
        {
            this.Status = "Purchased";
            this.rep.Add(this);
        }
    }

    /// added code
    public class AdvanceOrder : Order
    {
        public AdvanceOrder(IOrderRepository rep, string status, List<Item> items)
            : base(rep, status, items)
        {
        }
        public void Reserve()
        {
            this.Status = "Reserved";
            this.rep.Add(this);
        }
        public bool IsReserved()
        {
            return this.Status == "Reserved";
        }
    }

    public class Item
    {
        public string Id { get; private set; }
        public string Name { get; private set; }
        public decimal Price { get; private set; }

        public Item(string name, decimal price)
        {
            this.Name = name;
            this.Price = price;
        }
    }
#endregion

    [TestClass]
    public class Tester
    {
        [TestMethod]
        public void PurchaseTest()
        {
            IOrderRepository orderRep = new OrderRepository();
            Item i = new Item("test",(decimal)10.2);
            List<Item> items = new List<Item>();
            items.Add(i);
            Order o = new Order(orderRep, "new", items);

            IAccountRepository accountRep = new AccountRepository();
            Account a = new Account(accountRep, (decimal)100, new List<Transaction>());

            Customer c = new Customer(a);

            c.Purchase(o);

            Assert.AreEqual("Purchased", o.Status);
        }

        [TestMethod]
        public void ReserveTest()
        {
            IOrderRepository orderRep = new OrderRepository();
            Item i = new Item("test", (decimal)10.2);
            List<Item> items = new List<Item>();
            items.Add(i);
            AdvanceOrder o = new AdvanceOrder(orderRep, "new", items);

            IAccountRepository accountRep = new AccountRepository();
            Account a = new Account(accountRep, (decimal)100, new List<Transaction>());

            Customer c = new Customer(a);

            c.Reserve(o);

            Assert.AreEqual("Reserved", o.Status);

            c.Purchase(o);

            Assert.AreEqual("Purchased", o.Status);
        }
    }
}

10

Is having Purchase on Customer consistent with OOP principles and good
design? Or is there a better place to put it, do we need a new class?

No, it is not, and yes you need a new class. A purchase indicates that there is data to be persisted, and the Customer object should not know about data persistence pertaining to an order, or even about what is being ordered.

Typically you will have a service object that knows how to place an order, so the code becomes:

orderService.placeOrder(Customer customer, Shopping cart cart);

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật