首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >java宠物商店(控制台版本)、学会开发思想,走遍天下

java宠物商店(控制台版本)、学会开发思想,走遍天下

作者头像
软件小生活
发布2021-09-10 16:36:10
发布2021-09-10 16:36:10
8480
举报
文章被收录于专栏:软件小生活软件小生活

不说废话,直接奔主题

代码语言:javascript
复制
dao层实现类daoImpl

/**
 * 
 */
package daoImpl;

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 dao.AccountDao;
import dao.BaseDao;
import entity.Account;

/**
 * 宠物商店台账信息数据库操作类
 */
public class AccountDaoImpl extends BaseDao implements AccountDao {
  private Connection conn = null; // 保存数据库连接

  private PreparedStatement pstmt = null; // 用于执行SQL语句

  private ResultSet rs = null; // 用户保存查询结果集

  /* (non-Javadoc)
   * @see cn.jbit.epetShop.dao.impl.AccountDao#updateAccount(java.lang.String, java.lang.Object[])
   */
  public int updateAccount(String sql, Object[] param) {
    int count = super.executeSQL(sql, param);
    return count;
  }

  /* (non-Javadoc)
   * @see cn.jbit.epetShop.dao.impl.AccountDao#getPetStoreAccount(java.lang.String, java.lang.String[])
   */
  public List<Account> getPetStoreAccount(String sql, String[] param) {
    List<Account> accountList = new ArrayList<Account>();
    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语句
      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 (ClassNotFoundException e) {
      e.printStackTrace();
    } finally {
      super.closeAll(conn, pstmt, rs);
    }
    return accountList;
  }
}




package daoImpl;

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 dao.BaseDao;
import dao.PetDao;
import entity.Pet;

/**
 * 宠物数据库操作类
 */
public class PetDaoImpl extends BaseDao implements PetDao {
  private Connection conn = null; // 保存数据库连接

  private PreparedStatement pstmt = null; // 用于执行SQL语句

  private ResultSet rs = null; // 用户保存查询结果集

  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 (ClassNotFoundException e) {
      e.printStackTrace();
    } 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 (ClassNotFoundException 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;
  }

}





package daoImpl;

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 dao.BaseDao;
import dao.PetOwnerDao;
import 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();
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      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)
   * @
   */
  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;
  }

}


package daoImpl;

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 dao.BaseDao;
import dao.PetStoreDao;
import entity.PetStore;

/**
 *宠物商店数据库操作类
 */
public class PetStoreDaoImpl extends BaseDao implements 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();
    } catch (ClassNotFoundException 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();
    } catch (ClassNotFoundException 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;
  }

}
代码语言:javascript
复制
业务层service

/**
 * 
 */
package service;

import java.util.List;

import entity.Account;
import entity.Pet;
import entity.PetOwner;

/**
 * 宠物商店台账接口
 */
public interface Accountable {
  /**
   * 查询宠物商店台帐信息
   */
  public List<Account> account(long storeId);

  /**
   * 修改宠物商店台帐信息
   */
  public int modifyAccount(Pet pet, PetOwner owner);

}



package service;

import entity.Pet;

/**
 *宠物培育接口
 * 
 */
public interface Breadable {
  /**
   * 宠物繁殖
   */
  public Pet bread(String pet);
}



package service;

import entity.Pet;
import entity.PetStore;

/**
 * 买宠物接口
 */
public interface Buyable {
  /**
   * 宠物主人购买库存宠物
   */
  public void buy(Pet pet,PetStore store);
}



/**
 * 
 */
package service;

import entity.Pet;

/**
 *  宠物工厂接口
 */
public interface PetFactory {
  /**
   * 培育新品种宠物
   */
  public Pet breadNewPet(String[] petParam);
}



/**
 * 
 */
package service;

import java.util.List;

import entity.Pet;
import entity.PetOwner;

/**
 * 宠物主人接口
 * 
 */
public interface PetOwnerService extends Sellable, Buyable {

  /**
   * 宠物主人登录
   */
  public PetOwner login();

  /**
   * 根据宠物主人标识符(id)获得到该宠物主人所有宠物信息
   */
  public List<Pet> getMyPet(int ownerId);
}



/**
 * 
 */
package service;

/**
 *  宠物商店工厂接口
 */
public interface PetStoreFactory {
  /**
   * 创建宠物商店方法
   */
  public void createPetStore();
}



/**
 * 
 */
package service;

import java.util.List;

import entity.Pet;
import entity.PetOwner;
import 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,PetStore petStore);

  /**
   * 宠物商店登录
   */
  public PetStore login();

  /**
   * 查询出所有宠物主人正在出售的宠物
   */
  public List<Pet> getPetSelling();
}



/**
 * 
 */
package service;

import entity.Pet;

/**
 * 宠物卖接口
 */
public interface Sellable {
  /**
   * 卖宠物
   */
  public void sell(Pet pet);
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-08-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 软件小生活 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档