⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 auctionmanagerimpl.java

📁 是一个基于java开发的拍卖系统 非常适合初学者看
💻 JAVA
字号:
package com.wish.jhk.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.wish.jhk.service.AuctionManager;
import com.wish.jhk.Exception.AuctionException;
import com.wish.jhk.util.BeanFactory;

import com.wish.jhk.bean.BidBean;
import com.wish.jhk.bean.ItemBean;

import com.wish.jhk.entity.Bid;
import com.wish.jhk.entity.Item;
import com.wish.jhk.entity.Kind;
import com.wish.jhk.entity.User;

import com.wish.jhk.dao.BidDao;
import com.wish.jhk.dao.ItemDao;
import com.wish.jhk.dao.KindDao;
import com.wish.jhk.dao.StateDao;
import com.wish.jhk.dao.UserDao;

public class AuctionManagerImpl implements AuctionManager {

	// static Logger log = Logger.getLogger(AuctionManagerImpl.class.getName());

	private UserDao userDao;

	private BidDao bidDao;

	private ItemDao itemDao;

	private KindDao kindDao;

	private StateDao stateDao;

	// 增加新的竞价,并对竞价用户发送邮件通知
	// param userId 用户id;
	// param itemId 物品id;
	// param bidPrice 竞价价格
	// bidDao.save(bid);

	public void addBid(int userId, int itemId, double bidPrice)
			throws AuctionException {
		itemDao = (ItemDao) BeanFactory.getBean("itemDao");
		userDao = (UserDao) BeanFactory.getBean("userDao");
		bidDao = (BidDao) BeanFactory.getBean("bidDao");
		try {
			User user = userDao.get(userId);
			Item item = itemDao.get(itemId);
			if (bidPrice > item.getMaxPrice()) {
				item.setMaxPrice(bidPrice);
				itemDao.update(item);
			}
			Bid bid = new Bid();
			bid.setItem(item);
			bid.setUser(user);
			bid.setBidDate(new Date());
			bid.setBidPrice(bidPrice);

			bidDao.save(bid);
		} catch (Exception e) {
			// log.debug(e.getMessage());
			throw new AuctionException("添加竟价出现异常");
		}

	}

	// 添加物品
	// param name 物品名称
	// param desc 物品描述
	// param remark 物品备注
	// param avail 有效天数
	// param kind 物品种类
	// itemDao.save(item);

	public void addItem(String name, String desc, String remark,
			double initPrice, int time, int kind, int userId)
			throws AuctionException {
		kindDao = (KindDao) BeanFactory.getBean("kindDao");
		userDao = (UserDao) BeanFactory.getBean("userDao");
		stateDao = (StateDao) BeanFactory.getBean("stateDao");
		itemDao = (ItemDao) BeanFactory.getBean("itemDao");
		try {
			Kind k = kindDao.get(kind);
			User owner = userDao.get(userId);

			Item item = new Item();
			item.setItemName(name);
			item.setItemDesc(desc);
			item.setItemRemark(remark);
			item.setAddtime(new Date());
			Calendar c = Calendar.getInstance();
			c.add(Calendar.DATE, time);
			item.setEndtime(c.getTime());
			item.setInitPrice(initPrice);
			item.setMaxPrice(initPrice);
			item.setState(stateDao.get(1));
			item.setKind(k);
			item.setUserByOwnerId(owner);

			itemDao.save(item);
		} catch (Exception e) {
			throw new AuctionException("添加物品出现异常");
		}
	}

	// 添加种类
	// param name 种类名称
	// param desc 种类描述
	// kindDao.save(k);
	public void addKind(String name, String desc) throws AuctionException {
		kindDao = (KindDao) BeanFactory.getBean("kindDao");
		try {
			Kind kind = new Kind();
			kind.setKindName(name);
			kind.setKindDesc(desc);
			kindDao.save(kind);
		} catch (Exception e) {
			throw new AuctionException("添加种类出现异常");
		}

	}

	// 查询全部状态
	// return 获得全部种类
	// kindDao.findAll();

	public List<Kind> getAllKind() throws AuctionException {
		kindDao = (KindDao) BeanFactory.getBean("kindDao");

		return kindDao.findAll();
	}

	// 查询用户的全部出价
	// userId 需查询的用户id
	// return 用户的全部出价
	// bidDao.findByUser(userId);

	@SuppressWarnings("unchecked")
	public List<BidBean> getBidByUser(int userId) throws AuctionException {
		bidDao = (BidDao) BeanFactory.getBean("bidDao");
		try {
			List<BidBean> bidbeans = new ArrayList<BidBean>();
			List<Bid> bids = bidDao.findByUser(userId);
			for (Bid bs : bids) {
				BidBean bidbean = new BidBean();
				initBid(bidbean, bs);
				bidbeans.add(bidbean);
			}
			return bidbeans;
		} catch (Exception e) {
			throw new AuctionException("查询用户的全部出价出现异常");
		}
	}

	// 查询全部流拍的物品
	// return 全部流拍物品
	// itemDao.findItemByState(3);

	public List<ItemBean> getFailItems() throws AuctionException {
		itemDao = (ItemDao) BeanFactory.getBean("itemDao");
		try {
			List<ItemBean> itembeans = new ArrayList<ItemBean>();
			List<Item> items = itemDao.findItemByState(3);
			for (Item is : items) {
				ItemBean itembean = new ItemBean();
				initItem(itembean, is);
				itembeans.add(itembean);
			}
			return itembeans;
		} catch (Exception e) {
			throw new AuctionException("查询全部流拍的物品出现异常");
		}
	}

	// 根据物品id,获取物品
	// param itemId 物品id;
	// return 该物品的名称
	// itemDao.get(itemId);

	public ItemBean getItem(int itemId) throws AuctionException {
		itemDao = (ItemDao) BeanFactory.getBean("itemDao");
		try {
			ItemBean itembean = new ItemBean();
			Item item = itemDao.get(itemId);
			initItem(itembean, item);
			return itembean;
		} catch (Exception e) {
			throw new AuctionException("根据物品id,获取物品出现异常");
		}
	}

	// 根据赢取者查询物品
	// param winerId 赢取者id
	// return 赢取者获得的全部物品
	// itemDao.findItemByWiner(winerId);

	public List<ItemBean> getItemByWiner(int winerId) throws AuctionException {
		itemDao = (ItemDao) BeanFactory.getBean("itemDao");
		try {
			List<ItemBean> itembeans = new ArrayList<ItemBean>();
			List<Item> items = itemDao.findItemByWiner(winerId);
			for (Item is : items) {
				ItemBean itembean = new ItemBean();
				initItem(itembean, is);
				itembeans.add(itembean);
			}
			return itembeans;
		} catch (Exception e) {
			throw new AuctionException("根据赢取者查询物品出现异常");
		}
	}

	// 根据产品分类,获取当前拍卖的全部商品
	// param kindId 种类id;
	// return 该类的全部产品
	// itemDao.findItemByKind(kindId);

	public List<ItemBean> getItemsByKind(int kindId) throws AuctionException {
		itemDao = (ItemDao) BeanFactory.getBean("itemDao");
		try {
			List<ItemBean> itembeans = new ArrayList<ItemBean>();
			List<Item> items = itemDao.findItemByKind(kindId);
			for (Item is : items) {
				ItemBean itembean = new ItemBean();
				initItem(itembean, is);
				itembeans.add(itembean);
			}
			return itembeans;
		} catch (Exception e) {
			throw new AuctionException("根据产品分类,获取当前拍卖的全部商品出现异常");
		}
	}

	// 根据用户id查找目前仍在拍卖中的物品
	// userId 用户id
	// return 当前用户所有的全部物品。
	// itemDao.findItemByOwner(userId);

	public List<ItemBean> getItemsByOwner(int userId) throws AuctionException {
		itemDao = (ItemDao) BeanFactory.getBean("itemDao");
		kindDao = (KindDao) BeanFactory.getBean("kindDao");
		userDao = (UserDao) BeanFactory.getBean("userDao");
		try {
			List<ItemBean> itembeans = new ArrayList<ItemBean>();
			List<Item> items = itemDao.findItemByOwner(userId);
			// System.out.println("items.size():" + items.size());
			for (Item item : items) {
				ItemBean itembean = new ItemBean();
				initItem(itembean, item);
				itembeans.add(itembean);
			}
			return itembeans;
		} catch (Exception e) {
			throw new AuctionException("根据用户id查找目前仍在拍卖中的物品出现异常");
		}
	}

	// 根据分类id,获取种类名
	// param kindId 种类id;
	// return 该分类的名称
	// kindDao.get(kindId);

	public String getKind(int kindId) throws AuctionException {
		kindDao = (KindDao) BeanFactory.getBean("kindDao");
		return kindDao.get(kindId).getKindName();
	}

	// 根据时间来修改物品的赢取者
	// itemDao.findItemByState(1);
	// bidDao.findUserByItemAndPrice(item.getId() , item.getMaxPrice())
	// itemDao.update(item);

	public void updateWiner() throws AuctionException {
		itemDao = (ItemDao) BeanFactory.getBean("itemDao");
		bidDao = (BidDao) BeanFactory.getBean("bidDao");
		kindDao = (KindDao) BeanFactory.getBean("kindDao");
		stateDao = (StateDao) BeanFactory.getBean("stateDao");
		try {
			List<Item> itemList = itemDao.findItemByState(1);
			for (int i = 0; i < itemList.size(); i++) {
				Item item = (Item) itemList.get(i);
				if (!item.getEndtime().after(new Date())) {
					User user = bidDao.findUserByItemAndPrice(item.getItemId(),
							item.getMaxPrice());
					if (user != null) {
						item.setUserByWinerId(user);
						item.setState(stateDao.get(2));
						itemDao.update(item);
					} else {
						item.setState(stateDao.get(3));
						itemDao.update(item);
					}
				}
			}
		} catch (Exception e) {
			throw new AuctionException("更新出现异常");
		}

	}

	// 根据用户名,密码验证登陆是否成功
	// username 登陆所输入的用户名
	// pass 登陆的密码
	// return userId
	// userDao.findUserByNameAndPass(username , pass);

	public Integer validLogin(String username, String pass)
			throws AuctionException {
		userDao = (UserDao) BeanFactory.getBean("userDao");
		try {
			// userDao = (UserDao) BeanFactory.getBean("userDao");
			User user = userDao.findUserByNameAndPass(username, pass);
			if (user == null)
				return null;
			return user.getUserId();
		} catch (Exception e) {
			throw new AuctionException("登入验证出现异常");
		}
	}

	// 将一个Bid PO转换成BidBean的VO
	// param bb BidBean的VO
	// param bid Bid的PO
	//     
	@SuppressWarnings("unused")
	private void initBid(BidBean bidbean, Bid bid) {
		itemDao = (ItemDao) BeanFactory.getBean("itemDao");
		userDao = (UserDao) BeanFactory.getBean("userDao");
		bidbean.setId(bid.getBidId());
		bidbean.setItem(itemDao.get(bid.getItem().getItemId()).getItemName());
		bidbean.setUser(userDao.get(bid.getUser().getUserId()).getUsername());
		bidbean.setPrice(bid.getBidPrice());
		bidbean.setBidDate(bid.getBidDate());
	}

	// 将一个Item PO转换成ItemBean的VO
	// param ib ItemBean的VO
	// param item Item的PO
	@SuppressWarnings("unused")
	private void initItem(ItemBean itembean, Item item) {
		bidDao = (BidDao) BeanFactory.getBean("bidDao");
		kindDao = (KindDao) BeanFactory.getBean("kindDao");
		userDao = (UserDao) BeanFactory.getBean("userDao");
		stateDao = (StateDao) BeanFactory.getBean("stateDao");
		itembean.setId(item.getItemId());
		itembean.setName(item.getItemName());
		itembean.setDesc(item.getItemDesc());
		itembean.setRemark(item.getItemRemark());
		itembean.setAddTime(item.getAddtime());
		itembean.setEndTime(item.getEndtime());
		itembean.setInitPrice(item.getInitPrice());
		if (item.getMaxPrice() != null) {
			itembean.setMaxPrice(item.getMaxPrice());
		}
		itembean.setKind(kindDao.get(item.getKind().getKindId()).getKindName());
		itembean.setState(stateDao.get(item.getState().getStateId())
				.getStateName());
		itembean.setOwner(userDao.get(item.getUserByOwnerId().getUserId())
				.getUsername());
		if (item.getUserByWinerId() != null) {
			itembean.setWiner(userDao.get(item.getUserByWinerId().getUserId())
					.getUsername());
		}

	}

	public void register(String username, String userpass, String email)
			throws AuctionException {
		userDao = (UserDao) BeanFactory.getBean("userDao");
		try {
			User user = new User();

			user.setUsername(username);
			user.setUserpass(userpass);
			user.setEmail(email);

			userDao.save(user);
		} catch (Exception e) {
			throw new AuctionException("注册出现异常");
		}

	}

	public int validateRegister(String username) throws AuctionException {
		userDao = (UserDao) BeanFactory.getBean("userDao");
		try {
			return userDao.get(username);
		} catch (Exception e) {
			throw new AuctionException("用户名验证出现异常");
		}
	}

	// public static void main(String arg[]) throws AuctionException {
	// AuctionManagerImpl a = new AuctionManagerImpl();
	// AuctionManagerImpl auctionmanager = new AuctionManagerImpl();
	// AuctionManager auctionmanager = (AuctionManagerImpl) BeanFactory
	// .getBean("auctionmanager");
	// auctionmanager.addKind("硬件", "赢家阿斗发");
	// if (auctionauctionmanagermanager == null) {
	// System.out.println("null");
	// }
	// System.out.println(auctionmanager.validLogin("11", "tomcat"));//
	// 有问题
	//
	// List<Item> items = auctionmanager.getItemByWiner(1);
	// for (Item its : items) {
	// System.out.println(its.getItemId());
	// }
	//
	// List<Bid> bids = auctionmanager.getBidByUser(1);
	// for (Bid bs : bids) {
	// System.out.println(bs.getBidPrice());
	// }
	//
	// List<Kind> kinds = a.getAllKind();
	// for (Kind ss : kinds) {
	// System.out.println(ss.getKindId());
	// }
	//
	// a.addKind("手表2", "全进口卡通手表2"); //乱码问题
	// ItemBean item = auctionmanager.getItem(1);
	// System.out.println(item.getId());
	// List<ItemBean> itembeans = auctionmanager.getItemsByOwner(1);
	//
	// for (ItemBean is : itembeans) {
	// System.out.println(is.getId());
	// System.out.println(is.getName());
	// System.out.println(is.getDesc());
	// System.out.println(is.getRemark());
	// System.out.println(is.getKind());
	// System.out.println(is.getAddTime());
	// System.out.println(is.getEndTime());
	// System.out.println(is.getInitPrice());
	// System.out.println(is.getMaxPrice());
	// System.out.println(is.getOwner());
	// System.out.println(is.getState());
	// System.out.println(is.getWiner());
	// }
	// }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -