Account.cs
using System;
//References to PetShop specific libraries
//PetShop busines entity library
using PetShop.Model;
//PetShop DAL interfaces
using PetShop.IDAL;
namespace PetShop.BLL {
/// <summary>
/// A business Component used to manage accounts
/// The PetShop.Model.Account is used in most methods
/// and is used to store serializable information about an account
/// </summary>
public class Account {
/// <summary>
/// Method to login into the system. The user must supply a username and password
/// </summary>
/// <param name="userId">Unique identifier for a user</param>
/// <param name="password">Password for a user</param>
/// <returns>If the login is successful it returns information abount the account</returns>
public AccountInfo SignIn(string userId, string password) {
// Validate input
if ((userId.Trim() == string.Empty) || (password.Trim() == string.Empty))
return null;
// Get an instance of the account DAL using the DALFactory
IAccount dal = PetShop.DALFactory.Account.Create();
// Try to sign in with the given credentials
AccountInfo account = dal.SignIn(userId, password);
// Return the account
return account;
}
/// <summary>
/// Returns the address information for a specific user
/// </summary>
/// <param name="userId">Unique identifier for an account/customer</param>
/// <returns>Returns the address information for the user</returns>
public AddressInfo GetAddress(string userId) {
// Validate input
if (userId.Trim() == string.Empty)
return null;
// Get an instance of the account DAL using the DALFactory
IAccount dal = PetShop.DALFactory.Account.Create();
// Return the address information for the given userId from the DAL
return dal.GetAddress(userId);
}
/// <summary>
/// A method to insert a new Account
/// </summary>
/// <param name="account">An account entity with information about the new account</param>
public void Insert(AccountInfo account) {
// Validate input
if (account.UserId.Trim() == string.Empty)
return;
// Get an instance of the account DAL using the DALFactory
IAccount dal = PetShop.DALFactory.Account.Create();
// Call the DAL to insert the account
dal.Insert(account);
}
/// <summary>
/// A method to update an existing account
/// </summary>
/// <param name="account">An account entity with information about the account to be updated</param>
public void Update(AccountInfo account) {
// Validate input
if (account.UserId.Trim() == string.Empty)
return;
// Get an instance of the account DAL using the DALFactory
IAccount dal = PetShop.DALFactory.Account.Create();
// Send the udpated account information to the DAL
dal.Update(account);
}
}
}
Cart.cs
using System;
using System.Collections;
//References to PetShop specific libraries
//PetShop busines entity library
using PetShop.Model;
namespace PetShop.BLL {
/// <summary>
/// An object to represent a customer's shopping cart
/// </summary>
[Serializable]
public class Cart : IEnumerable {
/// <summary>
/// Internal storage for a cart
/// </summary>
private ArrayList _items = new ArrayList();
private decimal _total=0;
/// <summary>
/// Returns an enumerator for the cart items in a cart
/// </summary>
/// <returns></returns>
public IEnumerator GetEnumerator() {
return _items.GetEnumerator();
}
// Properties
public decimal Total {
get { return _total; }
set { _total = value; }
}
/// <summary>
/// Returns number of items in cart
/// </summary>
public int Count {
get { return _items.Count; }
}
/// <summary>
/// Return CartItem representation of object at a given address
/// </summary>
public CartItemInfo this[int index] {
get { return (CartItemInfo)_items[index]; }
}
/// <summary>
/// Add an item to the cart
/// </summary>
/// <param name="ItemId">ItemId of item to add</param>
public void Add(string ItemId) {
foreach (CartItemInfo cartItem in _items) {
if (ItemId == cartItem.ItemId) {
cartItem.Quantity++;
cartItem.InStock = (GetInStock(ItemId) - cartItem.Quantity) >= 0 ? true : false;
_total = _total+(cartItem.Price*cartItem.Quantity);
return;
}
}
Item item = new Item();
ItemInfo data = item.GetItem(ItemId);
CartItemInfo newItem = new CartItemInfo(ItemId,data.Name, (data.Quantity >= 1), 1, (decimal)data.Price);
_items.Add(newItem);
_total = _total+(data.Price);
}
/// <summary>
/// Remove item from the cart based on itemId
/// </summary>
/// <param name="itemId">ItemId of item to remove</param>
public void Remove(string itemId) {
foreach (CartItemInfo item in _items) {
if (itemId == item.ItemId) {
_items.Remove(item);
_total = _total-(item.Price*item.Quantity);
return;
}
}
}
/// <summary>
/// Removes item from cart at specific index
/// </summary>
/// <param name="index">Element number of item to remove</param>
public void RemoveAt(int index) {
CartItemInfo item = (CartItemInfo)_items[index];
_total = _total-(item.Price*item.Quantity);
_items.RemoveAt(index);
}
/// <summary>
/// Returs internal array list of cart items
/// </summary>
/// <returns></returns>
public ArrayList GetCartItems() {
return _items;
}
/// <summary>
/// Method to convert internal array of cart items to order line items
/// </summary>
/// <returns>New array list of order line items</returns>
public ArrayList GetOrderLineItems() {
ArrayList orderLineItems = new ArrayList();
int lineNum = 1;
foreach (CartItemInfo item in _items) {
LineItemInfo lineItem = new LineItemInfo(item.ItemId, item.Name, lineNum, item.Quantity, item.Price);
orderLineItems.Add(lineItem);
lineNum++;
}
return orderLineItems;
}
/// <summary>
/// Internal method to get the stock level of an item
/// </summary>
/// <param name="ItemId">Unique identifier of item to get stock level of</param>
/// <returns></returns>
private int GetInStock(string ItemId){
Inventory inventory = new Inventory();
return inventory.CurrentQuantityInStock(ItemId);
}
}
}
Account.cs与Cart.cs相关内容
190

被折叠的 条评论
为什么被折叠?



