Package net.helipilot50.stocktrade.server

Source Code of net.helipilot50.stocktrade.server.CustomerSOImpl

/*
* Copyright 2011 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.helipilot50.stocktrade.server;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

import net.helipilot50.stocktrade.client.CustomerSO;
import net.helipilot50.stocktrade.server.dao.CustomerDAO;
import net.helipilot50.stocktrade.server.dao.StockDAO;
import net.helipilot50.stocktrade.shared.Customer;
import net.helipilot50.stocktrade.shared.Holding;
import net.helipilot50.stocktrade.shared.InvalidTradeException;
import net.helipilot50.stocktrade.shared.NoSuchHoldingException;
import net.helipilot50.stocktrade.shared.Stock;

import org.springframework.stereotype.Service;

@Service("CustomerSO")
public class CustomerSOImpl implements CustomerSO {
 
  @Resource(name="customerDAO")
  private CustomerDAO customerDAO;

  @Resource(name="stockDAO")
  private StockDAO stockDAO;

  private List<String> stockNameList;
 
  @PostConstruct
  public void init() throws Exception{
    setupStockNameList();
   
  }
  @PreDestroy
  public void destroy(){
   
  }
    /**
     * decrementHolding<p>
     * Import code for DecrementHolding method - CustomerMgr class<br>
     * Method does ff:<br>
     * Declare a variable of type Customer.<br>
     * Declare a variable of type Holding.<br>
     * Call the GetCustomer method to get the Customer with the given name.<br>
     * Call the GetHolding method to get the Holding for the Customer.<br>
     * If a Holding is found, and has more shares than pQuantity:<br>
     * decrement the quantity of the Holding by pQuantity.<br>
     * If a Holding is found, and has the same quantity as pQuantity:<br>
     * delete the Holding from Customer�s HoldingList, using the DeleteRow.<br>
     * If a Holding is found, but has an insufficient quantity to decrement:<br>
     * raise an InvalidTradeException.<br>
     * If a Holding is not found (NoSuchHoldingException is caught):<br>
     * code an exception handler and raise an InvalidTradeException.<br>
     * <p>
     * @param pCustomerName Type: TextData
     * @param pStockName Type: String
     * @param pQuantity Type: int
     * @param pPrice Type: float
     */
    public void decrementHolding(String pCustomerName, String pStockName, int pQuantity, float pPrice) {
        try {
            Customer oneCustomer = null;
            Holding custHolding = null;
            float incomeFromStock = pQuantity*pPrice;
   
            oneCustomer = this.getCustomer(pCustomerName);
            //custHolding = self.GetHolding(pCustomer=oneCustomer,
            //        pGetHoldStockName=pStockName);
            custHolding = customerDAO.SQLSelectHolding(pCustomerName, pStockName);
   
            if (custHolding.getQuantity() > pQuantity) {
                custHolding.setQuantity(custHolding.getQuantity()-pQuantity);
                oneCustomer.setCashBalance(oneCustomer.getCashBalance()+incomeFromStock);
                customerDAO.SQLUpdateHolding(custHolding);
                customerDAO.SQLUpdateCashBal(oneCustomer.getCustomerName(), oneCustomer.getCashBalance());
   
            }
            else if (custHolding.getQuantity() == pQuantity) {
              customerDAO.SQLDeleteHolding(custHolding);
   
                //oneCustomer.HoldingList.DeleteRow(object=custHolding);
                oneCustomer.setCashBalance(oneCustomer.getCashBalance()+incomeFromStock);
                customerDAO.SQLUpdateCashBal(oneCustomer.getCustomerName(), oneCustomer.getCashBalance());
   
            }
            else if (custHolding.getQuantity() < pQuantity) {
                InvalidTradeException badOrder = new InvalidTradeException("Customer "+pCustomerName+" does not own enough stock to place SELL Order");
                throw badOrder;
            }
        }
        catch (NoSuchHoldingException noHolding) {
                InvalidTradeException badOrder = new InvalidTradeException("Customer "+pCustomerName+" does not own this stock and cannot place SELL Order");
                throw badOrder;
        }
    }


    /**
     * getCustomer<p>
     * <p>
     * @param pGetCustCustomerName Type: TextData
     * @return Customer
     */
    public Customer getCustomer(String pGetCustCustomerName) {
        Customer aCustomer = customerDAO.SQLSelectCustomer(pGetCustCustomerName);
        //aCustomer.setHoldingList(customerDAO.SQLSelectHoldingList(pGetCustCustomerName));

        return aCustomer;
    }
    /**
     * getHolding<p>
     * <p>
     * @param pCustomer Type: Customer
     * @param pGetHoldStockName Type: String
     * @return Holding
     */
    public Holding getHolding(Customer pCustomer, String pGetHoldStockName) {
        List<Holding> qq_localVector = pCustomer.getHoldingList();
        if (qq_localVector != null) {
            for (Holding row : qq_localVector) {
                if (row.getStockName().equalsIgnoreCase(pGetHoldStockName)) {
                    return row;
                }
            }
        }
        NoSuchHoldingException noHolding = new NoSuchHoldingException("Customer "+pCustomer.getCustomerName()+" does not own this stock");
        throw noHolding;
    }

    /**
     * incrementHolding<p>
     * Import code for IncrementHolding method - CustomerMgr class<br>
     * Method does ff:<br>
     * Declare a variable of type Customer.<br>
     * Declare a variable of type Holding.<br>
     * Call the GetCustomer method to get the Customer with the given name.<br>
     * Call the GetHolding method to get the Holding for the Customer.<br>
     * If a Holding is found:<br>
     * increment the quantity of the Holding by the pQuantity parameter.<br>
     * If a Holding is not found:<br>
     * implies NoSuchHoldingException is raised in the GetHolding method.<br>
     * Code an exception handler.<br>
     * Create a new Holding object.<br>
     * Fill in its values based on the method parameters.<br>
     * Add it to the Customer�s HoldingList, using the AppendRow method.<br>
     * <p>
     * @param pCustomerName Type: TextData
     * @param pStockName Type: String
     * @param pQuantity Type: int
     * @param pPrice Type: float
     */
    public void incrementHolding(String pCustomerName, String pStockName, int pQuantity, float pPrice) {
        Customer oneCustomer = null;
        Holding custHolding = null;

        float costOfStock = pQuantity*pPrice;

        oneCustomer = this.getCustomer(pCustomerName);

        if (oneCustomer.getCashBalance() > costOfStock) {
            try {
                oneCustomer.setCashBalance(oneCustomer.getCashBalance()-costOfStock);
                //    custHolding = self.GetHolding(pCustomer=oneCustomer,pGetHoldStockName=pStockName);
                custHolding = customerDAO.SQLSelectHolding(pCustomerName, pStockName);
                custHolding.setQuantity(custHolding.getQuantity()+pQuantity);
                customerDAO.SQLUpdateHolding(custHolding);
                customerDAO.SQLUpdateCashBal(oneCustomer.getCustomerName(), oneCustomer.getCashBalance());

            }
            catch (NoSuchHoldingException noHolding) {
                    Holding newHolding = new Holding();
                    newHolding.setCustomerName(pCustomerName);
                    newHolding.setPrice(pPrice);
                    newHolding.setQuantity(pQuantity);
                    newHolding.setStockName(pStockName);
                    customerDAO.SQLInsertHolding(newHolding);
                    customerDAO.SQLUpdateCashBal(oneCustomer.getCustomerName(), oneCustomer.getCashBalance());


                    //        oneCustomer.HoldingList.AppendRow(object=newHolding);
            }
        }
        else {
            InvalidTradeException badOrder = new InvalidTradeException("Customer "+pCustomerName+" doesn't have enough cash to BUY stock");
            throw badOrder;
        }
    }


    /**
     * setupStockNameList<p>
     * <p>
     */
    private void setupStockNameList() {
        List<Stock> stocks = stockDAO.stockList();
        if (stocks != null){
          this.stockNameList = new ArrayList<String>();
          for (Stock stock : stocks){
             this.stockNameList.add(stock.getStockName());
          }
        }
    }
   
  public List<String> getStockNameList() {
    return stockNameList;
  }



}
TOP

Related Classes of net.helipilot50.stocktrade.server.CustomerSOImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.