节选自北大青鸟S2项目-宠物商店

  希望这个可以帮助到大家,先感谢大家的阅读和点赞。

  DAO层

Account

package cn.bdqn.dao;import java.util.List;import cn.bdqn.entity.Account;public interface AccountDao {
public abstract int updateAccount(String sql,Object[] param);public abstract List<Account> getPetStoreAccount(String sql,Object[] param);
}

BaseDao

package cn.bdqn.dao;import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;public class BaseDao {public static String driver;public static String url;public static String user;public static String password;Connection conn = null;static {init();}public static void init() {Properties param = new Properties();String configFile = "database.properties";InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(configFile);try {param.load(is);} catch (IOException e) {e.printStackTrace();}url = param.getProperty("url");user = param.getProperty("user");password = param.getProperty("password");}public Connection getConn() {Connection conn = null;try {Class.forName("com.mysql.cj.jdbc.Driver");conn = DriverManager.getConnection(url,user,password);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}return conn;} public void closeAll(Connection conn,PreparedStatement pstmt,ResultSet rs) {if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (pstmt != null) {try {pstmt.close();} catch (SQLException e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}/*** sql语句*/public int executeSQL(String preparedSql,Object[] param) {Connection conn = null;PreparedStatement pstmt = null;int num = 0; try {conn = getConn();pstmt = conn.prepareStatement(preparedSql);if (param != null) {for (int i = 0; i < param.length; i++) {pstmt.setObject(i+1, param[i]);}}num = pstmt.executeUpdate();}catch (ClassCastException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}finally {this.closeAll(conn, pstmt, null);}return num;}
}

PetDao

package cn.bdqn.dao;import java.util.List;import cn.bdqn.entity.Pet;public interface PetDao {
public abstract List<Pet> getAllPet();
public abstract List<Pet> selectPet(String sql,String[] param);
public abstract int updatePet(String sql,Object[] param);
}

PetOwnerDao

package cn.bdqn.dao;import java.util.List;import cn.bdqn.entity.PetOwner;public interface PetOwnerDao {
public abstract List<PetOwner> getAllOwner();
public abstract int updateOwner(String sql,String[] param);
public abstract PetOwner selectOwner(String sql,String[] param);}

PetStoreDao

package cn.bdqn.dao;import java.util.List;import cn.bdqn.entity.PetStore;public interface PetStoreDao {
public abstract List<PetStore> getAllStore();
public abstract PetStore getPetStore(String sql,String[] param);
public abstract int updateStore(String sql,Object[] param);
}

DaoImpl实现

AccountDaoImpl

package cn.bdqn.dao.impl;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import cn.bdqn.dao.AccountDao;
import cn.bdqn.dao.BaseDao;
import cn.bdqn.entity.Account;public class AccountDaoImpl extends BaseDao implements AccountDao{private Connection conn = null;private PreparedStatement pstmt = null;private ResultSet rs = null;private List<Account> accountList;@Overridepublic int updateAccount(String sql, Object[] param) {int count = super.executeSQL(sql, param);return count;}@Overridepublic List<Account> getPetStoreAccount(String sql, Object[] param) {List<Account> accounts = new ArrayList<Account>();try {conn = getConn();pstmt = conn.prepareStatement(sql);if (param != null) {for (int i = 0; i < param.length; i++) {pstmt.setString(i+1, (String) param[i]);}}rs = pstmt.executeQuery();Account account = null;while (rs.next()) {account = new Account();account.setId(rs.getInt(1));account.setDealType(rs.getInt(2));account.setPetId(rs.getInt(3));account.setSellerId(rs.getInt(4));account.setBuyerId(rs.getInt(5));account.setPrice(rs.getDouble(6));account.setDealTime(rs.getDate(7));accountList.add(account);}} catch (SQLException e) {e.printStackTrace();}catch (Exception e) {e.printStackTrace();}finally {super.closeAll(conn, pstmt, rs);}return accountList;}}

PetDaoImpl

package cn.bdqn.dao.impl;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import cn.bdqn.dao.BaseDao;
import cn.bdqn.dao.PetDao;
import cn.bdqn.entity.Pet;/*** 宠物数据库操作类*/
public class PetDaoImpl extends BaseDao implements PetDao {private Connection conn = null; // 保存数据库连接private PreparedStatement pstmt = null; // 用于执行SQL语句private ResultSet rs = null; // 用户保存查询结果集/* (non-Javadoc)* @see cn.jbit.epetShop.dao.impl.PetDao#getAllPet()*/public List<Pet> getAllPet() {List<Pet> petList = new ArrayList<Pet>();try {String preparedSql = "select id,name,typeName,health,love,birthday,owner_id,store_id from pet ";conn = getConn(); // 得到数据库连接pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象rs = pstmt.executeQuery(); // 执行SQL语句while (rs.next()) {Pet pet = new Pet();pet.setId(rs.getInt(1));pet.setName(rs.getString(2));pet.setTypeName(rs.getString(3));pet.setHealth(rs.getInt(4));pet.setLove(rs.getInt(5));pet.setBirthday(rs.getDate(6));pet.setOwnerId(rs.getInt(7));pet.setStoreId(rs.getInt(8));petList.add(pet);}} catch (SQLException e) {e.printStackTrace();}catch (ClassCastException e) {// TODO: handle exception} finally {super.closeAll(conn, pstmt, rs);}return petList;}/* (non-Javadoc)* @see cn.jbit.epetShop.dao.impl.PetDao#selectPet(java.lang.String, java.lang.String[])*/public List<Pet> selectPet(String sql, String[] param) {List<Pet> petList = new ArrayList<Pet>();try {conn = getConn(); // 得到数据库连接pstmt = conn.prepareStatement(sql); // 得到PreparedStatement对象if (param != null) {for (int i = 0; i < param.length; i++) {pstmt.setString(i + 1, param[i]); // 为预编译sql设置参数}}rs = pstmt.executeQuery(); // 执行SQL语句while (rs.next()) {Pet pet = new Pet();pet.setId(rs.getInt(1));pet.setName(rs.getString(2));pet.setTypeName(rs.getString(3));pet.setHealth(rs.getInt(4));pet.setLove(rs.getInt(5));pet.setBirthday(rs.getDate(6));pet.setOwnerId(rs.getInt(7));pet.setStoreId(rs.getInt(8));petList.add(pet);}} catch (SQLException e) {e.printStackTrace();}catch (ClassCastException e) {e.printStackTrace();} finally {super.closeAll(conn, pstmt, rs);}return petList;}/** (non-Javadoc)* * @see cn.jbit.epetShop.dao.impl.PetDao#updatePet(java.lang.String,* java.lang.Object[])*/public int updatePet(String sql, Object[] param) {int count = super.executeSQL(sql, param);return count;}}

PetOwnerDaoImpl

package cn.bdqn.dao.impl;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import cn.bdqn.dao.BaseDao;
import cn.bdqn.dao.PetOwnerDao;
import cn.bdqn.entity.PetOwner;/***宠物主人数据库操作类*/public class PetOwnerDaoImpl extends BaseDao implements PetOwnerDao {private Connection conn = null; // 保存数据库连接private PreparedStatement pstmt = null; // 用于执行SQL语句private ResultSet rs = null; // 用户保存查询结果集/* (non-Javadoc)* @see cn.jbit.epetShop.dao.impl.PetOwnerDao#getAllOwner()*/public List<PetOwner> getAllOwner() {List<PetOwner> ownerList = new ArrayList<PetOwner>();try {String preparedSql = "select * from petowner ";conn = getConn(); // 得到数据库连接pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象rs = pstmt.executeQuery(); // 执行SQL语句while (rs.next()) {PetOwner petOwner = new PetOwner();petOwner.setId(rs.getInt(1));petOwner.setName(rs.getString(2));petOwner.setPassword(rs.getString(3));petOwner.setMoney(rs.getDouble(4));ownerList.add(petOwner);}} catch (SQLException e) {e.printStackTrace();} finally {super.closeAll(conn, pstmt, rs);}return ownerList;}/* (non-Javadoc)* @see cn.jbit.epetShop.dao.impl.PetOwnerDao#updateOwner(java.lang.String, java.lang.String[])*/public int updateOwner(String sql, String[] param) {int count = super.executeSQL(sql, param);return count;}/* (non-Javadoc)* @see cn.jbit.epetShop.dao.impl.PetOwnerDao#selectOwner(java.lang.String, java.lang.String[])*/public PetOwner selectOwner(String sql, String[] param) {PetOwner owner = null;try {conn = getConn(); // 得到数据库连接pstmt = conn.prepareStatement(sql); // 得到PreparedStatement对象if (param != null) {for (int i = 0; i < param.length; i++) {pstmt.setString(i + 1, param[i]); // 为预编译sql设置参数}}rs = pstmt.executeQuery(); // 执行SQL语句while (rs.next()) {owner = new PetOwner();owner.setId(rs.getInt(1));owner.setName(rs.getString(2));owner.setPassword(rs.getString(3));owner.setMoney(rs.getDouble(4));}} catch (Exception e) {e.printStackTrace();} finally {super.closeAll(conn, pstmt, rs);}return owner;}}

PetStoreDaoImpl

package cn.bdqn.dao.impl;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import cn.bdqn.dao.BaseDao;
import cn.bdqn.dao.PetDao;
import cn.bdqn.dao.PetStoreDao;
import cn.bdqn.entity.Pet;
import cn.bdqn.entity.PetStore;/***宠物商店数据库操作类*/
public class PetStoreDaoImpl extends BaseDao implements PetDao, PetStoreDao {private Connection conn = null; // 保存数据库连接private PreparedStatement pstmt = null; // 用于执行SQL语句private ResultSet rs = null; // 用户保存查询结果集/* (non-Javadoc)* @see cn.jbit.epetShop.dao.impl.PetStoreDao#getAllStore()*/public List<PetStore> getAllStore() {List<PetStore> storeList = new ArrayList<PetStore>();try {String preparedSql = "select * from petstore ";conn = getConn(); // 得到数据库连接pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象rs = pstmt.executeQuery(); // 执行SQL语句while (rs.next()) {PetStore petStore = new PetStore();petStore.setId(rs.getInt(1));petStore.setName(rs.getString(2));petStore.setPassword(rs.getString(3));petStore.setBalance(rs.getDouble(4));storeList.add(petStore);}} catch (SQLException e) {e.printStackTrace();} finally {super.closeAll(conn, pstmt, rs);}return storeList;}/* (non-Javadoc)* @see cn.jbit.epetShop.dao.impl.PetStoreDao#getPetStore(java.lang.String, java.lang.String[])*/public PetStore getPetStore(String sql, String[] param) {PetStore petStore = null;try {conn = getConn(); // 得到数据库连接pstmt = conn.prepareStatement(sql); // 得到PreparedStatement对象if (param != null) {for (int i = 0; i < param.length; i++) {pstmt.setString(i + 1, param[i]); // 为预编译sql设置参数}}rs = pstmt.executeQuery(); // 执行SQL语句while (rs.next()) {petStore = new PetStore();petStore.setId(rs.getInt(1));petStore.setName(rs.getString(2));petStore.setPassword(rs.getString(3));petStore.setBalance(rs.getDouble(4));}} catch (SQLException e) {e.printStackTrace();} finally {super.closeAll(conn, pstmt, rs);}return petStore;}/* (non-Javadoc)* @see cn.jbit.epetShop.dao.impl.PetStoreDao#updateStore(java.lang.String, java.lang.Object[])*/public int updateStore(String sql, Object[] param) {int count = super.executeSQL(sql, param);return count;}@Overridepublic List<Pet> getAllPet() {// TODO Auto-generated method stubreturn null;}@Overridepublic List<Pet> selectPet(String sql, String[] param) {// TODO Auto-generated method stubreturn null;}@Overridepublic int updatePet(String sql, Object[] param) {// TODO Auto-generated method stubreturn 0;}}

entity

Account

package cn.bdqn.entity;import java.util.Date;/*** @author 宠物商店台帐类* */
public class Account {/*** 帐单标识符*/private long id;/*** 交易类型,1--代表商店卖给宠物主人 2--代表宠物主人卖给商店 3---宠物主人之间交易*/private int dealType;/*** 宠物标识符*/private long petId;/*** 卖家标识符*/private long sellerId;/*** 买家标识符*/private long buyerId;/*** 交易价格*/private double price;/*** 交易时间*/private Date dealTime;/*** @return the buyerId*/public long getBuyerId() {return buyerId;}/*** @param buyerId the buyerId to set*/public void setBuyerId(long buyerId) {this.buyerId = buyerId;}/*** @return the dealTime*/public Date getDealTime() {return dealTime;}/*** @param dealTime the dealTime to set*/public void setDealTime(Date dealTime) {this.dealTime = dealTime;}/*** @return the dealType*/public int getDealType() {return dealType;}/*** @param dealType the dealType to set*/public void setDealType(int dealType) {this.dealType = dealType;}/*** @return the id*/public long getId() {return id;}/*** @param id the id to set*/public void setId(long id) {this.id = id;}/*** @return the petId*/public long getPetId() {return petId;}/*** @param petId the petId to set*/public void setPetId(long petId) {this.petId = petId;}/*** @return the price*/public double getPrice() {return price;}/*** @param price the price to set*/public void setPrice(double price) {this.price = price;}/*** @return the sellerId*/public long getSellerId() {return sellerId;}/*** @param sellerId the sellerId to set*/public void setSellerId(long sellerId) {this.sellerId = sellerId;}}

Pet

package cn.bdqn.entity;import java.util.Date;/*** 宠物实体类* */
public class Pet {/** 宠物标识符*/private long id;/*** 宠物名称*/private String name;/*** 宠物类别*/private String typeName;/*** 宠物健康指数*/private int health;/*** 宠物爱心*/private int love;/*** 宠物生日*/private Date birthday;/*** 宠物所属宠物主人标识符*/private int ownerId;/*** 宠物所属宠物商店标识符*/private long storeId;/*** @return the ownerId*/public int getOwnerId() {return ownerId;}/*** @param ownerId*            the ownerId to set*/public void setOwnerId(int ownerId) {this.ownerId = ownerId;}/*** @return the birthdate*/public Date getBirthday() {return birthday;}/*** @return the storeId*/public long getStoreId() {return storeId;}/*** @param storeId*            the storeId to set*/public void setStoreId(long storeId) {this.storeId = storeId;}/*** @param birthdate*            the birthday to set*/public void setBirthday(Date birthday) {this.birthday = birthday;}/*** @return the health*/public int getHealth() {return health;}/*** @param health*            the health to set*/public void setHealth(int health) {this.health = health;}/*** @return the id*/public long getId() {return id;}/*** @param id*            the id to set*/public void setId(long id) {this.id = id;}/*** @return the love*/public int getLove() {return love;}/*** @param love*            the love to set*/public void setLove(int love) {this.love = love;}/*** @return the name*/public String getName() {return name;}/*** @param name*            the name to set*/public void setName(String name) {this.name = name;}/*** @return the typeName*/public String getTypeName() {return typeName;}/*** @param typeName*            the typeName to set*/public void setTypeName(String typeName) {this.typeName = typeName;}
}

PetOwner

package cn.bdqn.entity;
/**  * 宠物主人实体类  *  */
public class PetOwner {
/**   * 宠物主人标识符   */
private int id;/**   * 宠物主人名称   */
private String name;/**   * 宠物主人密码   */
private String password;/**   * 宠物主人元宝数   */
private double money;public double getMoney() {   return money;  }public void setMoney(double money) {   this.money = money;  }public int getId() {   return id;  }public void setId(int id) {   this.id = id;  }public String getName() {   return name;  }public void setName(String name) {   this.name = name;  }public String getPassword() {   return password;  }public void setPassword(String password) {   this.password = password;  }
}

PetStore

/**  *  */
package cn.bdqn.entity;/** * 宠物商店实体类 * */
public class PetStore {/** * 宠物商店id */private long id;/** * 宠物商店名称 */private String name;/** * 宠物商店密码 */private String password;/** * 宠物商店资金 */private double balance;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}/** * @return the balance 得到宠物商店结余 */public double getBalance() {return balance;}public void setBalance(double balance) {this.balance = balance;}
}

Service

Accountable

package cn.bdqn.service;import java.util.List;import cn.bdqn.entity.Account;
import cn.bdqn.entity.Pet;
import cn.bdqn.entity.PetOwner;public interface Accountable {/*** 查询宠物商店台帐信息*/public List<Account> account(long storeId);/*** 修改宠物商店台帐信息*/public int modifyAccount(Pet pet, PetOwner owner);}

Breadable

package cn.bdqn.service;import cn.bdqn.entity.Pet;public interface Breadable {/*** 宠物繁殖* @return*/public Pet bread(String pet);
}

Buyable

package cn.bdqn.service;import cn.bdqn.entity.Pet;/*** 买宠物* @author ASUS**/public interface Buyable {/*** 宠物主人储存宠物* @param pet*/public void buy(Pet pet);}

PetFactory

package cn.bdqn.service;import cn.bdqn.entity.Pet;public interface PetFactory {public Pet breadNewPet(String[] petPream);}

PetOwnerService

package cn.bdqn.service;import java.util.List;import cn.bdqn.entity.Pet;
import cn.bdqn.entity.PetOwner;
/*** @author  宠物主人接口* */
public interface PetOwnerService extends Buyable,Sellable{/*** 宠物主人登录*/public PetOwner login();/*** 根据宠物主人标识符(id)获得到该宠物主人所有宠物信息* @param ownerId* @return*/public List<Pet> getMyPet(int ownerId);}

PetStoryFactory

package cn.bdqn.service;/*** @author  宠物商店工厂接口*/public interface PetStoreFactory {/*** 创建宠物商店方法*/public void createPetStore();}

PetStoreService

package cn.bdqn.service;
/*** @author 宠物商店接口*/import java.util.List;import cn.bdqn.entity.Pet;
import cn.bdqn.entity.PetOwner;
import cn.bdqn.entity.PetStore;public interface PetStoreService extends Accountable,Breadable,Buyable,Sellable{/*** 查询出所有库存宠物*/public List<Pet> getPetsInstock(long storeId);/*** 查询出所有新培育的宠物*/public List<Pet> getPetsBread();/*** 查询出所有新培育的宠物*/public double charge(Pet pet);/*** 根据宠物主人信息修改宠物信息*/public int modifyPet(Pet pet, PetOwner petOwner,PetStore store); /*** 修改宠物信息*/public int modifyOwner(PetOwner owner, Pet pet, int type);/*** 根据宠物商店标识符查询宠物商店*/public PetStore getPetStore(long id);/*** 修改宠物商店信息*/public int modifyStore(Pet pet, int type);/*** 宠物商店登录*/public PetStore login();/*** 查询出所有宠物主人正在出售的宠物*/public List<Pet> getPetSelling();
}

Sellable

package cn.bdqn.service;import cn.bdqn.entity.Pet;/*** 宠物卖接口*/
public interface Sellable {/***  卖宠物*/public void sell(Pet pet);
}

ServiceImpl实现方法

PetFactoryImpl

package cn.bdqn.service.impl;import cn.bdqn.entity.Pet;
import cn.bdqn.service.PetFactory;/*** 宠物工厂实现类*/
public class PetFactoryImpl implements PetFactory {/*** 宠物工程培育新品种宠物*/public Pet breadNewPet(String[] petParam) {Pet pet = new Pet();pet.setName(petParam[0]);pet.setTypeName(petParam[1]);pet.setHealth(Integer.parseInt(petParam[2]));pet.setLove(Integer.parseInt(petParam[3]));pet.setStoreId(Integer.parseInt(petParam[4]));return pet;}
}

PetOwnerServiceImpl

package cn.bdqn.service.impl;import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;import cn.bdqn.dao.AccountDao;
import cn.bdqn.dao.PetDao;
import cn.bdqn.dao.PetOwnerDao;
import cn.bdqn.dao.impl.AccountDaoImpl;
import cn.bdqn.dao.impl.PetDaoImpl;
import cn.bdqn.dao.impl.PetOwnerDaoImpl;
import cn.bdqn.dao.impl.PetStoreDaoImpl;
import cn.bdqn.entity.Pet;
import cn.bdqn.entity.PetOwner;
import cn.bdqn.entity.PetStore;
import cn.bdqn.service.PetOwnerService;
import cn.bdqn.service.PetStoreService;/*** @author  宠物主人实现类*/
public class PetOwnerServiceImpl implements PetOwnerService {/*** 宠物主人购买库存宠物,根据页面获得到的序号,来实际调用购买库存宠物或者购买新培育的宠物*/public void buy(Pet pet) {String sql = "select * from petowner where id=?";String param[] = { String.valueOf(pet.getOwnerId()) };PetOwnerDao ownerDao = new PetOwnerDaoImpl();PetOwner owner = ownerDao.selectOwner(sql, param);PetStoreService petStore = (PetStoreService) new PetStoreServiceImpl();int updatePet = petStore.modifyPet(pet, owner, null);// 更新宠物信息if (updatePet > 0) {// 更新宠物主人的信息int updateOwner = petStore.modifyOwner(owner, pet, 0);if (updateOwner > 0) {// 更新宠物商店的信息int updateStore = petStore.modifyStore(pet, 1);if (updateStore > 0) {// 更新宠物商店台帐信息int insertAccount = petStore.modifyAccount(pet, owner);if (insertAccount > 0) {System.out.println("台帐正确插入一条信息");}}}}}/*** 宠物主人向宠物商店卖出自己宠物*/public void sell(Pet pet) {PetDaoImpl petDao = new PetDaoImpl();PetOwnerDaoImpl ownerDao = new PetOwnerDaoImpl();String updatesql = "update pet set owner_id = null where id=?";Object[] param = { pet.getId() };int updatePet = petDao.executeSQL(updatesql, param);// 更新宠物信息if (updatePet > 0) {// 更新宠物主人的信息String ownersql = "select * from petowner where id=?";String ownerparam[] = { String.valueOf(pet.getOwnerId()) };PetOwner owner = ownerDao.selectOwner(ownersql, ownerparam);String updateOwnerSql = "update petowner set money=? where id=?";Object[] ownerParam = { (owner.getMoney() + 3), owner.getId() };int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);if (updateOwner > 0) {// 更新宠物商店的信息PetStoreServiceImpl store = new PetStoreServiceImpl();PetStore petStore = ((PetStoreService) store).getPetStore(pet.getStoreId());String updateStore = "update petstore set balance=? where id=?";Object[] storeParam = { (petStore.getBalance() - 3),petStore.getId() };PetStoreDaoImpl storeDao = new PetStoreDaoImpl();int updatestore = storeDao.executeSQL(updateStore, storeParam);if (updatestore > 0) {// 更新宠物商店台帐信息String insertsql = "insert into account(deal_type,pet_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?, ?)";String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());Object[] accountParam = { 2, pet.getId(), owner.getId(),pet.getStoreId(), 3, date };AccountDao accountDao = new AccountDaoImpl();int insertAccount = accountDao.updateAccount(insertsql,accountParam);if (insertAccount > 0) {System.out.println("台帐正确插入一条信息");}}}}}/*** 宠物主人登录*/public PetOwner login() {Scanner input = new Scanner(System.in);// 1、输入主人姓名System.out.println("请先登录,请您输入主人的名字:");String ownerName = input.nextLine().trim();System.out.println("请您输入主人的密码:");String ownerPassword = input.nextLine().trim();PetOwnerDao ownerDao = new PetOwnerDaoImpl();String sql = "select * from petowner where name=? and password=?";String[] param = { ownerName, ownerPassword };PetOwner owner = ownerDao.selectOwner(sql, param);if (null != owner) {System.out.println("-------恭喜您成功登录-------");System.out.println("-------您的基本信息:-------");System.out.println("名字:" + owner.getName());System.out.println("元宝数:" + owner.getMoney());}return owner;}/*** * 根据宠物主人标识符(id)获得到该宠物主人所有宠物信息*/public List<Pet> getMyPet(int ownerId) {List<Pet> petList = new ArrayList<Pet>();String sql = "select * from pet where owner_id=?";String[] param = { String.valueOf(ownerId) };PetDao petDao = new PetDaoImpl();petList = petDao.selectPet(sql, param);return petList;}}

PetStoreFactoryImpl

package cn.bdqn.service.impl;
import java.util.Scanner;import cn.bdqn.dao.PetStoreDao;
import cn.bdqn.dao.impl.PetStoreDaoImpl;
import cn.bdqn.service.PetStoreFactory;/*** @author 宠物商店工程实现类*/
public class PetStoreFactoryImpl implements PetStoreFactory {/*** 创建宠物商店*/public void createPetStore() {Scanner input = new Scanner(System.in);System.out.println("请在下面输入宠物商店属性:");System.out.println("请输入宠物商店名字:");String storeName = input.nextLine();System.out.println("请输入宠物商店的密码(英文加数字):");String storePassword = input.nextLine();System.out.println("请输入宠物商店的资金(整数):");String petBalance = input.nextLine();String sql = "insert into petstore(`name`,`password`,balance) values(?,?,?)";Object[] param = { storeName, storePassword, petBalance };PetStoreDao storeDao = (PetStoreDao) new PetStoreDaoImpl();int count = storeDao.updateStore(sql, param);if (count > 0) {System.out.println("成功创建了一个宠物商店,商店名字叫" + storeName);}}}

PetStoreServiceImpl

package cn.bdqn.service.impl;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;import cn.bdqn.dao.AccountDao;
import cn.bdqn.dao.PetDao;
import cn.bdqn.dao.PetOwnerDao;
import cn.bdqn.dao.PetStoreDao;
import cn.bdqn.dao.impl.AccountDaoImpl;
import cn.bdqn.dao.impl.PetDaoImpl;
import cn.bdqn.dao.impl.PetOwnerDaoImpl;
import cn.bdqn.dao.impl.PetStoreDaoImpl;
import cn.bdqn.entity.Account;
import cn.bdqn.entity.Pet;
import cn.bdqn.entity.PetOwner;
import cn.bdqn.entity.PetStore;
import cn.bdqn.service.PetFactory;
import cn.bdqn.service.PetStoreService;/*** 宠物商店实现类*/
public class PetStoreServiceImpl implements PetStoreService {/*** 为宠物定价*/public double charge(Pet pet) {// 编写定价规则Date date = new Date();double price = 0;int age = date.getYear() - pet.getBirthday().getYear();if (age > 5) {price = 3;} else {price = 5;}return price;}/*** 查询出所有库存宠物*/public List<Pet> getPetsInstock(long storeId) {PetDao petDao = new PetDaoImpl();String[] param = { String.valueOf(storeId) };String sql = "";// 当storeId不为0时,要执行查询指定商店库存宠物if(storeId!=0){sql = "select * from pet where owner_id is null and store_id=?";           }   // 当storeId为0时,要执行查询所有商店的库存宠物if (0 == storeId) {sql = "select * from pet where owner_id is null";param = null;}List<Pet> petList = petDao.selectPet(sql, param);return petList;}/*** 查询出所有新培育的宠物*/public List<Pet> getPetsBread() {PetDao petDao = new PetDaoImpl();String sql = "select * from pet where owner_id is null and typename not in (?,?)";String[] petParam = { "dog", "penguin" };List<Pet> petList = petDao.selectPet(sql, petParam);return petList;}/*** 查询宠物商店账目*/public List<Account> account(long storeId) {String sql = "select * from account where deal_type=? and seller_id=? union select * from account where deal_type=? and buyer_id=?";String[] param = { "1", String.valueOf(storeId), "2",String.valueOf(storeId) };AccountDao accountDao = new AccountDaoImpl();List<Account> list = accountDao.getPetStoreAccount(sql, param);return list;}/*** 培育宠物*/public Pet bread(String petType) {Scanner input = new Scanner(System.in);System.out.println("请在下面输入宠物属性:");System.out.println("请输入宠物名字:");String petName = input.nextLine();System.out.println("请输入宠物健康指数(整数):");String petHealth = input.nextLine();System.out.println("请输入宠物爱心指数(整数):");String petLove = input.nextLine();System.out.println("请输入宠物所属宠物商店的标识符(整数):");String StoreId = input.nextLine();String[] petParam = { petName, petType, petHealth, petLove, StoreId };PetFactory petFactory = new PetFactoryImpl();Pet pet = petFactory.breadNewPet(petParam);SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd");String petBirthday = simpleDate.format(new Date());String sql = "insert into pet(`name`,typeName,health,love,birthday,store_id) values(?,?,?,?,?,?)";Object[] param = { pet.getName(), pet.getTypeName(),pet.getHealth(), pet.getLove(), petBirthday, pet.getStoreId() };PetDao petDao = new PetDaoImpl();int count = petDao.updatePet(sql, param);if (count > 0) {System.out.println("成功创建了一只" + pet.getTypeName() + "宠物");}return pet;       }/*** 购买宠物*/public void buy(Pet pet) {String sql = "select * from petStore where id=?";String paramStore[] = { String.valueOf(pet.getStoreId()) };PetStoreDao storeDao = (PetStoreDao) new PetStoreDaoImpl();PetStore store = storeDao.getPetStore(sql, paramStore); PetOwnerDao ownerDao = new PetOwnerDaoImpl();sql = "select * from petOwner where id = ?";String paramOwner[] = { String.valueOf(pet.getOwnerId()) };PetOwner owner = ownerDao.selectOwner(sql, paramOwner);int updatePet = modifyPet(pet, null, store);// 更新宠物信息if (updatePet > 0) {// 更新宠物主人的信息int updateOwner = modifyOwner(owner, pet, 1);if (updateOwner > 0) {// 更新宠物商店的信息int updateStore = modifyStore(pet, 0);if (updateStore > 0) {// 更新宠物商店台帐信息int insertAccount = modifyAccount(pet, owner);if (insertAccount > 0) {System.out.println("台帐正确插入一条信息");}}}} else {System.out.println("修改宠物信息失败");}}/*** 卖宠物*/public void sell(Pet pet) {PetDaoImpl petDao = new PetDaoImpl();PetStoreDaoImpl storeDao = new PetStoreDaoImpl();PetOwnerDaoImpl ownerDao = new PetOwnerDaoImpl();PetStoreServiceImpl petStore = new PetStoreServiceImpl();String updatesql = "update pet set store_id = null ,owner_id=? where id=?";Object[] param = { pet.getOwnerId(), pet.getId() };int updatePet = petDao.executeSQL(updatesql, param);// 更新宠物信息if (updatePet > 0) {// 更新宠物主人的信息String ownersql = "select * from petowner where id=?";String ownerparam[] = { String.valueOf(pet.getOwnerId()) };PetOwner owner = ownerDao.selectOwner(ownersql, ownerparam);String updateOwnerSql = "update petowner set money=? where id=?";double count = petStore.charge(pet);Object[] ownerParam = { (owner.getMoney() - count), owner.getId() };int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);if (updateOwner > 0) {// 更新宠物商店的信息PetStore store = petStore.getPetStore(pet.getStoreId());String updateStore = "update petstore set balance=? where id=?";Object[] storeParam = { (store.getBalance() + count),store.getId() };int updatestore = storeDao.executeSQL(updateStore, storeParam);if (updatestore > 0) {// 更新宠物商店台帐信息String insertsql = "insert into account(deal_type,pet_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?, ?)";String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());Object[] accountParam = {1, pet.getId(), owner.getId(),pet.getStoreId(),count, date };AccountDao accountDao = new AccountDaoImpl();int insertAccount = accountDao.updateAccount(insertsql,accountParam);if (insertAccount > 0) {System.out.println("台帐正确插入一条信息");}}}}}/*** 根据宠物商店标识符查询宠物商店*/public PetStore getPetStore(long id) {String sql = "select * from petstore where id=" + id;PetStoreDao storeDao = (PetStoreDao) new PetStoreDaoImpl();PetStore petStore = storeDao.getPetStore(sql, null);return petStore;}/*** 根据宠物主人信息修改宠物信息 根据PetOwnerEntity和PetStoreEntity的值判断是宠物主人买宠物或者宠物商店买宠物* PetOwnerEntity=null是宠物商店买宠物,PetStoreEntity=null是宠物主人买宠物*/public int modifyPet(Pet pet, PetOwner owner,PetStore store) {String updatesql = null;long id = 0;if (null == store) {updatesql = "update pet set owner_id=? where id=?";id = owner.getId();} else if (null == owner) {updatesql = "update pet set store_id=?,owner_id=null where id=?";id = store.getId();}Object[] param = { id, pet.getId() };PetDaoImpl petDao = new PetDaoImpl();int updatePet = petDao.executeSQL(updatesql, param);// 更新宠物信息return updatePet;}/*** 修改宠物主人信息 type=0是宠物主人买宠物,type=1是宠物商店买宠物*/public int modifyOwner(PetOwner owner, Pet pet, int type) {PetOwnerDaoImpl ownerDao = new PetOwnerDaoImpl();String updateOwnerSql = "update petowner set money=? where id=?";double price = this.charge(pet);double count = 0;if (0 == type) {count = (owner.getMoney() - price);}if (1 == type) {count = (owner.getMoney() + price);}Object[] ownerParam = { count, owner.getId() };int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);return updateOwner;}/*** 修改宠物商店信息 type=0是宠物主人买宠物,type=1是宠物商店买宠物*/public int modifyStore(Pet pet, int type) {PetStoreServiceImpl store = new PetStoreServiceImpl();PetStore petStore = store.getPetStore(pet.getStoreId());String updateStore = "update petstore set balance=? where id=?";double price = this.charge(pet);double count = 0;if (0 == type) {count = (petStore.getBalance() - price);}if (1 == type) {count = (petStore.getBalance() + price);}Object[] storeParam = { count, petStore.getId() };PetStoreDaoImpl storeDao = new PetStoreDaoImpl();int updatestore = storeDao.executeSQL(updateStore, storeParam);return updatestore;}/*** 修改宠物商店台帐信息*/public int modifyAccount(Pet pet, PetOwner owner) {String insertsql = "insert into account(deal_type,pet_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?,?)";String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());double price = this.charge(pet);Object[] accountParam = { 1, pet.getId(), pet.getStoreId(),owner.getId(), price, date };AccountDao accountDao = new AccountDaoImpl();int insertAccount = accountDao.updateAccount(insertsql, accountParam);return insertAccount;}/*** 宠物商店登录*/public PetStore login() {Scanner input = new Scanner(System.in);PetStore petStore = null;// 1、输入宠物商店名字boolean type = true;while (type) {System.out.println("请先登录,请输入宠物商店名字:");String storeName = input.nextLine().trim();System.out.println("请输入宠物商店的密码:");String storePassword = input.nextLine().trim();PetStoreDao storeDao = (PetStoreDao) new PetStoreDaoImpl();String sql = "select * from petstore where name=? and password=?";String[] param = { storeName, storePassword };petStore = storeDao.getPetStore(sql, param);if (null != petStore) {System.out.println("-------恭喜成功登录-------");System.out.println("-------宠物商店的基本信息:-------");System.out.println("名字:" + petStore.getName());System.out.println("元宝数:" + petStore.getBalance());type = false;} else {System.out.println("登录失败,请确认您的用户名和密码是否正确,重新登录");type = true;}}return petStore;}@Override/*** 查询出所有宠物主人正在出售的宠物*/public List<Pet> getPetSelling() {PetDao petDao = new PetDaoImpl();String sql = "select * from pet where owner_id is not null";String[] petParam = null;List<Pet> petList = petDao.selectPet(sql, petParam);return petList;}}

最后的菜单--test

Main方法菜单:

package cn.bdqn.test;import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;import cn.bdqn.dao.PetDao;
import cn.bdqn.dao.PetOwnerDao;
import cn.bdqn.dao.PetStoreDao;
import cn.bdqn.dao.impl.PetDaoImpl;
import cn.bdqn.dao.impl.PetOwnerDaoImpl;
import cn.bdqn.dao.impl.PetStoreDaoImpl;
import cn.bdqn.entity.Account;
import cn.bdqn.entity.Pet;
import cn.bdqn.entity.PetOwner;
import cn.bdqn.entity.PetStore;
import cn.bdqn.service.PetOwnerService;
import cn.bdqn.service.PetStoreFactory;
import cn.bdqn.service.PetStoreService;
import cn.bdqn.service.impl.PetOwnerServiceImpl;
import cn.bdqn.service.impl.PetStoreFactoryImpl;
import cn.bdqn.service.impl.PetStoreServiceImpl;/*** 入口类* */
public class Main {/*** @param args void 系统入口方法*/public static void main(String[] args) {Main.startPetShop();}/*** 系统启动*/public static void startPetShop() {System.out.println(" 宠物商店启动");System.out.println("Wonderland醒来,所有宠物从MySQL中醒来");System.out.println("****************************************************");PetDao petDao = new PetDaoImpl();List<Pet> petList = petDao.getAllPet();System.out.println("序号\t" + "宠物名称\t");for (int i = 0; i < petList.size(); i++) {Pet pet = petList.get(i);System.out.println((i + 1) + "\t" + pet.getName() + "\t");}System.out.println("****************************************************");System.out.print("\n");System.out.println("所有宠物主人从MySQL中醒来");PetOwnerDao ownerDao = new PetOwnerDaoImpl();List<PetOwner> ownerList = ownerDao.getAllOwner();System.out.println("****************************************************");System.out.println("序号\t" + "主人姓名\t");for (int i = 0; i < ownerList.size(); i++) {PetOwner owner = ownerList.get(i);System.out.println((i + 1) + "\t" + owner.getName() + "\t");}System.out.println("****************************************************");System.out.print("\n");System.out.println("所有宠物商店从MySQL中醒来");System.out.println("****************************************************");PetStoreDao storeDao = new PetStoreDaoImpl();List<PetStore> storeList = storeDao.getAllStore();System.out.println("序号\t" + "宠物商店名称\t");for (int i = 0; i < storeList.size(); i++) {PetStore store = storeList.get(i);System.out.println((i + 1) + "\t" + store.getName() + "\t");}System.out.println("****************************************************");System.out.print("\n");Scanner input = new Scanner(System.in);System.out.println("请选择输入登录模式,输入1为宠物主人登录,输入2为宠物商店登录");boolean type = true;String num;while (type) {num = input.next();if ("1".equals(num)) {Main.ownerLogin();type = false;} else if ("2".equals(num)) {Main.storeLogin();type = false;} else {System.out.println("输入有误,请按照指定规则输入");System.out.println("请选择登录模式,输入1为宠物主人登录,输入2为宠物商店登录");type = true;}}}/*** 宠物主人登录方法*/public static PetOwner ownerLogin() {Scanner input = new Scanner(System.in);PetOwnerServiceImpl petOwner = new PetOwnerServiceImpl();PetOwner Owner = petOwner.login();boolean reg = true;while (reg) {if (null == Owner) {System.out.println("登录失败,请确认您的用户名和密码后重新输入");Owner = petOwner.login();reg = true;} else {reg = false;System.out.println("登录成功,您可以购买和卖出宠物,如果您想购买宠物请输入1,如果想卖出宠物请输入2");System.out.println("1:购买宠物");System.out.println("2:卖出宠物");boolean type = true;while (type) {int num = input.nextInt();if (1 == num) {Main.ownerBuy(Owner);type = false;} else if (2 == num) {Main.ownerSell(Owner);type = false;} else {System.out.println("输入有误,请重新输入");type = true;}}}}return Owner;}/*** 宠物商店登录方法*/public static PetStore storeLogin() {Scanner input = new Scanner(System.in);PetStoreService petStore = new PetStoreServiceImpl();PetStore store = petStore.login();System.out.println("登录成功,可以进行如下操作");System.out.println("1:购买宠物");System.out.println("2:卖出宠物");System.out.println("3:培育宠物");System.out.println("4:查询待售宠物");System.out.println("5:查看商店结余");System.out.println("6:查看商店账目");System.out.println("7:开宠物商店");System.out.println("请根据需要执行的操作,选择序号输入,退出请输入0");boolean type = true;while (type) {int num = input.nextInt();switch (num) {case 0:System.out.println("退出成功");type = false;break;case 1:Main.storeBuy(store);type = false;break;case 2:Main.storeSell(store);type = false;break;case 3:Main.storeBread();type = false;break;case 4:Main.queryPetStock(store.getId());type = false;break;case 5:Main.queryStoreBalance(store);type = false;break;case 6:Main.getAccount(store.getId());type = false;break;case 7:Main.createPetStore();type = false;break;default:System.out.println("输入有误,请重新输入");type = true;break;}}return store;}/*** 查询商店结余*/public static void queryStoreBalance(PetStore petStore) {double balance = petStore.getBalance();System.out.println(petStore.getName() + "宠物商店的结余为:" + balance);}/*** 查询待售宠物*/public static void queryPetStock(long storeId) {PetStoreService petStore = new PetStoreServiceImpl();Pet pet = null;List<Pet> petList = petStore.getPetsInstock(storeId);System.out.println("序号\t" + "宠物名称\t" + " 类型\t");for (int i = 0; i < petList.size(); i++) {pet = petList.get(i);System.out.println((i + 1) + "\t" + pet.getName() + "\t" + pet.getTypeName() + "\t");}}/*** 宠物商店购买宠物*/public static void storeBuy(PetStore store) {Scanner input = new Scanner(System.in);PetStoreService petStore = new PetStoreServiceImpl();Pet pet = null;List<Pet> petList = petStore.getPetSelling();System.out.println("-------以下是宠物主人正在出售的宠物-------");System.out.println("序号\t" + "宠物名称\t" + " 类型\t" + "\t元宝数\t");for (int i = 0; i < petList.size(); i++) {pet = petList.get(i);double price = petStore.charge(pet);// 获得到宠物的价格System.out.println((i + 1) + "\t" + pet.getName() + "\t" + pet.getTypeName() + "\t" + price + "\t");}System.out.println("-------请选择要购买哪个一个宠物,并输入选择项的序号-------");int num = input.nextInt();pet = petList.get(num - 1);petStore.buy(pet);}/*** 宠物商店出售宠物*/public static void storeSell(PetStore store) {Scanner input = new Scanner(System.in);PetStoreService petStore = new PetStoreServiceImpl();Pet pet = null;List<Pet> petList = petStore.getPetsInstock(store.getId());System.out.println("-------以下是宠物商店正在出售的宠物-------");System.out.println("序号\t" + "宠物名称\t" + "类型\t");for (int i = 0; i < petList.size(); i++) {pet = petList.get(i);System.out.println((i + 1) + "\t" + pet.getName() + "\t" + pet.getTypeName() + "\t");}System.out.println("---------请选择要购买的宠物序号--------");boolean type = true;while (type) {int num = input.nextInt();if ((num - 1) < petList.size() && (num - 1) >= 0) {pet = petList.get(num - 1);System.out.println("------要卖出的宠物信息如下------");System.out.println("宠物名字叫:" + pet.getName() + " 宠物类别是:" + pet.getTypeName());System.out.println("请确认是否卖出,y代表卖出,n代表不卖");String code = input.next();if (null != code) {if ("y".equals(code)) {System.out.println("------下面是现有宠物主人买家,请选择您要卖给买家序号------");List<PetOwner> ownerList = new ArrayList<PetOwner>();PetOwnerDao ownerDao = new PetOwnerDaoImpl();ownerList = ownerDao.getAllOwner();PetOwner petOwner = null;System.out.println("序号\t" + "宠物主人姓名\t");for (int i = 0; i < ownerList.size(); i++) {petOwner = ownerList.get(i);System.out.println((i + 1) + "\t" + petOwner.getName() + "\t");}num = input.nextInt();if ((num - 1) < ownerList.size() && (num - 1) >= 0) {petOwner = ownerList.get(num - 1);}pet.setOwnerId(petOwner.getId());petStore.sell(pet);} else if ("n".equals(type)) {System.out.println("--------您选择放弃本次交易,希望您再次光顾----------");} else {System.out.println("--------您的输入有误----------");}}type = true;} else {System.out.println("输入有误,请按照序号重新输入");type = false;}type = false;// 标识符更改为false,退出系统}}/*** 宠物商店培育宠物*/public static void storeBread() {PetStoreService petStore = new PetStoreServiceImpl();Scanner input = new Scanner(System.in);System.out.println("请输入要培育宠物的类型");String petType = input.next();petStore.bread(petType);}/*** 创建宠物商店*/public static void createPetStore() {PetStoreFactory storeFactory = new PetStoreFactoryImpl();storeFactory.createPetStore();}/*** 宠物主人购买宠物*/public static void ownerBuy(PetOwner owner) {Scanner input = new Scanner(System.in);System.out.println("-------请输入选择要购买范围:只输入选择项的序号--------");System.out.println("1:购买库存宠物");System.out.println("2:购买新培育宠物");PetStoreService petStore = new PetStoreServiceImpl();PetOwnerService petOwner = new PetOwnerServiceImpl();Pet pet = null;int num = input.nextInt();List<Pet> petList = null;// num为1时购买库存宠物boolean type = true;while (type) {if (num == 1) {petList = petStore.getPetsInstock(0);System.out.println("-------以下是库存宠物-------");System.out.println("序号\t" + "宠物名称\t" + "类型\t" + "\t元宝数\t");for (int i = 0; i < petList.size(); i++) {pet = petList.get(i);double price = petStore.charge(pet);// 获得到宠物的价格System.out.println((i + 1) + "\t" + pet.getName() + "\t" + pet.getTypeName() + "\t" + price + "\t");}System.out.println("-------请选择要购买哪个一个宠物,并输入选择项的序号-------");num = input.nextInt();pet = petList.get(num - 1);pet.setOwnerId(owner.getId());petOwner.buy(pet);type = false;// num为2时购买新培育宠物} else if (num == 2) {System.out.println("-------以下是库存宠物-------");System.out.println("序号\t" + "宠物名称\t" + "类型\t" + "\t元宝数\t");petList = petStore.getPetsBread();for (int i = 0; i < petList.size(); i++) {pet = petList.get(i);double price = petStore.charge(pet);// 获得到宠物的价格System.out.println((i + 1) + "\t" + pet.getName() + "\t" + pet.getTypeName() + "\t" + price + "\t");}System.out.println("-------请选择要购买哪个一个宠物,并输入选择项的序号-------");String count = input.next();if (count.matches("[0-9]*")) {num = Integer.parseInt(count);pet = petList.get(num - 1);pet.setOwnerId(owner.getId());petOwner.buy(pet);}type = false;} else {System.out.println("您的输入有误,请安提示输入");type = true;}}}/*** 宠物主人向宠物商店出售自己宠物*/public static void ownerSell(PetOwner petOwner) {Scanner input = new Scanner(System.in);PetOwnerService owner = new PetOwnerServiceImpl();System.out.println("---------我的宠物列表--------");List<Pet> petList = owner.getMyPet(petOwner.getId());System.out.println("序号\t" + "宠物名称\t" + "类型\t");for (int i = 0; i < petList.size(); i++) {Pet pet = petList.get(i);System.out.println((i + 1) + "\t" + pet.getName() + "\t" + pet.getTypeName() + "\t");}System.out.println("---------请选择要出售的宠物序号--------");boolean type = true;while (type) {int num = input.nextInt();if ((num - 1) < petList.size() && (num - 1) >= 0) {Pet pet = petList.get(num - 1);System.out.println("------您要卖出的宠物信息如下------");System.out.println("宠物名字叫:" + pet.getName() + " 宠物类别是:" + pet.getTypeName());System.out.println("请确认是否卖出,y代表卖出,n代表不卖");String code = input.next();if (null != code) {if ("y".equals(code)) {System.out.println("------下面是现有宠物商店,请选择您要卖给买家序号------");List<PetStore> storeList = new ArrayList<PetStore>();PetStoreDao storeDao = (PetStoreDao) new PetStoreDaoImpl();storeList = storeDao.getAllStore();PetStore petStore = null;System.out.println("序号\t" + "宠物商店名字\t");for (int i = 0; i < storeList.size(); i++) {petStore = storeList.get(i);System.out.println((i + 1) + "\t" + petStore.getName() + "\t");}num = input.nextInt();if ((num - 1) < storeList.size() && (num - 1) >= 0) {petStore = storeList.get(num - 1);}pet.setStoreId(petStore.getId());owner.sell(pet);} else if ("n".equals(type)) {System.out.println("--------您选择放弃本次交易,希望您再次光顾----------");} else {System.out.println("--------您的输入有误----------");}}type = true;} else {System.out.println("输入有误,请按照序号重新输入");type = false;}type = false;// 标识符更改为false,退出系统}}/*** 查看商店帐目*/public static void getAccount(long storeId) {PetStoreService store = new PetStoreServiceImpl();List<Account> list = store.account(storeId);for (int i = 0; i < list.size(); i++) {Account account = list.get(i);String type = null;if (1 == account.getDealType()) {type = "商店卖给宠物主人";} else if (2 == account.getDealType()) {type = "宠物主人卖给商店";} else {type = "宠物主人之间交易";}System.out.println("第" + (i + 1) + "笔交易,交易类型为:" + type + ",交易金额是:" + account.getPrice() + "个元宝");}}}

宠物商店——三层架构相关推荐

  1. 宠物商店 三层关系小结 显示宠物列表

    宠物商店思路整理 Test类 Game game是显示层,与用户交互 PetServiceImpl 小结三层关系 表示层,借用业务员的某个业务本领 具体的业务需要操作数据 操作数据就使用刀 刀里代码直 ...

  2. 第十二课 从宠物商店案例看DAPP架构和WEB3.JS交互接口

    1. 文章摘要 [本文目标] 了解ETH生态下DAPP去中心化应用程序的框架和交互流程,了解WEB3.JS的作用和接口函数. [前置条件] 完成了<第六课 技术小白如何开发一个DAPP区块链应用 ...

  3. 区块链100讲:从宠物商店案例看DAPP架构和WEB3.JS交互接口

    1 文章摘要 [本文目标] 了解ETH生态下DAPP去中心化应用程序的框架和交互流程,了解WEB3.JS的作用和接口函数. [前置条件] 完成了<技术小白如何开发一个DAPP区块链应用(以宠物商 ...

  4. MVC项目实践,在三层架构下实现SportsStore-06,实现购物车

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

  5. 【宠物商店管理系统】基于SSM的宠物商店系统(ppt+论文+源代码)

    技术架构 SSM +jsp+spring.javascript.servlet. 数据库:mysql 系统功能 ![在这里插入图片描述](https://img-blog.csdnimg.cn/b09 ...

  6. 魅族应用商店云端架构实践

    原文:http://www.infoq.com/cn/articles/flyme-appstore-cloud-architecture?utm_medium=referral 应用商店可以说是移动 ...

  7. 计算机毕业设计ssm宠物商店信息展示与服务订购系统7q5ic系统+程序+源码+lw+远程部署

    计算机毕业设计ssm宠物商店信息展示与服务订购系统7q5ic系统+程序+源码+lw+远程部署 计算机毕业设计ssm宠物商店信息展示与服务订购系统7q5ic系统+程序+源码+lw+远程部署 本源码技术栈 ...

  8. java计算机毕业设计网上宠物商店系统源码+系统+数据库+lw文档+mybatis+运行部署

    java计算机毕业设计网上宠物商店系统源码+系统+数据库+lw文档+mybatis+运行部署 java计算机毕业设计网上宠物商店系统源码+系统+数据库+lw文档+mybatis+运行部署 本源码技术栈 ...

  9. java计算机毕业设计网上宠物商店源程序+mysql+系统+lw文档+远程调试

    java计算机毕业设计网上宠物商店源程序+mysql+系统+lw文档+远程调试 java计算机毕业设计网上宠物商店源程序+mysql+系统+lw文档+远程调试 本源码技术栈: 项目架构:B/S架构 开 ...

  10. [附源码]java毕业设计宠物商店管理系统

    项目运行 环境配置: Jdk1.8 + Tomcat7.0 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(IntelliJ IDEA,Eclispe,MyEclis ...

最新文章

  1. CentOS安装配置Samba
  2. 加密和解密算法 Asp.net
  3. mysql router安装教程_MySQL Router 安装与配置
  4. Java 8系列之重构和定制收集器
  5. 如何为Apache Kylin快速开发新数据源?
  6. linux获取主板温度电压_自学修电脑:常见主板报警声解析!
  7. C语言输出规定长度的整数,不够位数前面补零
  8. Failed to find provider null for user 0; expected to find a valid ContentProvider for this authority
  9. 【NLP之情感分析】华为云NLP算法专家:全面解读文本情感分析任务
  10. 黑马程序员——C语言基础 字符串
  11. BPM波导matlab,用Matlab画平板波导色散图详解.doc
  12. 一文学会如何使用Java的交互式编程环境 JShell
  13. ad转3d视图快捷键_AD详细快捷键按键
  14. css实现简单几何图形
  15. 这是什么意思admin.php,administratior是什么意思
  16. 阿拉伯数字转换成中文数字 C++
  17. Linux下mplayer播放器安装及故障排除
  18. 推荐一款风格融合软件——Ostagram
  19. WORD文档如何在页眉页尾显示 page X of Y
  20. 基于长短期记忆网络的电力负荷预测(Python代码实现)

热门文章

  1. 我的python中级班学习之路(全程笔记第一模块) (第二章)(第3部分:元祖、哈希(hash)、字典、集合...
  2. IBM智慧商务 - IBM和SugarCRM携手提供全方位渠道客户体验
  3. .Net面试经验总结
  4. 数分下(第1讲):一阶微分方程的三类模型求解
  5. 常微分方程简要复习_笔记_第2章:一阶微分方程的初等解法
  6. catboost原理
  7. 商业智能应用的五大步骤
  8. B树、B+树、B*树
  9. java根据IP查询所在地址(百度地图)
  10. 论数据库运维的全流程管控技术