当前位置:   article > 正文

鲜花商店 完整代码_鲜花售卖网站代码

鲜花售卖网站代码

 鲜花商店 1


第一关
文件:src/step1/entity/Flower.java

  1. package step1.entity;
  2. /**
  3. * 鲜花顾客实体类
  4. */
  5. public class FlowerOwner {
  6. private int id; //顾客标识符
  7. private String name; //顾客名称
  8. private String password; //顾客密码
  9. private double money; //顾客资金
  10. //TODO-2 补全getter/setter方法
  11. public FlowerOwner(){}
  12. public FlowerOwner(int id, String name, String password, double money) {
  13. this.id = id;
  14. this.name = name;
  15. this.password = password;
  16. this.money = money;
  17. }
  18. public int getId() {
  19. return id;
  20. }
  21. public void setId(int id) {
  22. this.id = id;
  23. }
  24. public String getName() {
  25. return name;
  26. }
  27. public void setName(String name) {
  28. this.name = name;
  29. }
  30. public String getPassword() {
  31. return password;
  32. }
  33. public void setPassword(String password) {
  34. this.password = password;
  35. }
  36. public double getMoney() {
  37. return money;
  38. }
  39. public void setMoney(double money) {
  40. this.money = money;
  41. }
  42. }

 文件:src/step1/entity/FlowerOwner.java

  1. package step1.entity;
  2. /**
  3. * 鲜花实体类
  4. */
  5. public class Flower {
  6. private long id; //鲜花标识符
  7. private String name; //鲜花名称
  8. private String typeName; //鲜花类别
  9. private int ownerId; //鲜花所属顾客标识符
  10. private long storeId; //鲜花所属鲜花商店标识符
  11. private double price; //鲜花价格
  12. //TODO-1 补齐getter/setter方法
  13. public Flower(){}
  14. public Flower(long id, String name, String typeName, int ownerId, long storeId, double price) {
  15. this.id = id;
  16. this.name = name;
  17. this.typeName = typeName;
  18. this.ownerId = ownerId;
  19. this.storeId = storeId;
  20. this.price = price;
  21. }
  22. public long getId() {
  23. return id;
  24. }
  25. public void setId(long id) {
  26. this.id = id;
  27. }
  28. public String getName() {
  29. return name;
  30. }
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34. public String getTypeName() {
  35. return typeName;
  36. }
  37. public void setTypeName(String typeName) {
  38. this.typeName = typeName;
  39. }
  40. public int getOwnerId() {
  41. return ownerId;
  42. }
  43. public void setOwnerId(int ownerId) {
  44. this.ownerId = ownerId;
  45. }
  46. public long getStoreId() {
  47. return storeId;
  48. }
  49. public void setStoreId(long storeId) {
  50. this.storeId = storeId;
  51. }
  52. public double getPrice() {
  53. return price;
  54. }
  55. public void setPrice(double price) {
  56. this.price = price;
  57. }
  58. }

文件:src/step1/entity/FlowerStore.java 

  1. package step1.entity;
  2. /**
  3. * 鲜花商店实体类
  4. */
  5. public class FlowerStore {
  6. private long id; //鲜花商店id
  7. private String name; //鲜花商店名称
  8. private String password; //鲜花商店密码
  9. private double balance; //鲜花商店资金
  10. public FlowerStore(long id, String name, String password, double balance) {
  11. this.id = id;
  12. this.name = name;
  13. this.password = password;
  14. this.balance = balance;
  15. }
  16. public FlowerStore(){};
  17. public long getId() {
  18. return id;
  19. }
  20. public void setId(long id) {
  21. this.id = id;
  22. }
  23. public String getName() {
  24. return name;
  25. }
  26. public void setName(String name) {
  27. this.name = name;
  28. }
  29. //TODO-3 补全name的getter/setter方法
  30. public String getPassword() {
  31. return password;
  32. }
  33. public void setPassword(String password) {
  34. this.password = password;
  35. }
  36. public double getBalance() {
  37. return balance;
  38. }
  39. public void setBalance(double balance) {
  40. this.balance = balance;
  41. }
  42. }

第二关

文件:src/step2/dao/impl/FlowerDaoImpl.java

  1. package step2.dao.impl;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import step2.dao.BaseDao;
  9. import step2.dao.FlowerDao;
  10. import step2.entity.Flower;
  11. /**
  12. * 鲜花数据库操作类
  13. */
  14. public class FlowerDaoImpl extends BaseDao implements FlowerDao {
  15. private Connection conn = null; // 保存数据库连接
  16. private PreparedStatement pstmt = null; // 用于执行SQL语句
  17. private ResultSet rs = null; // 用户保存查询结果集
  18. /**
  19. * 查询所有鲜花
  20. */
  21. @Override
  22. public List<Flower> getAllFlower() {
  23. List<Flower> flowerList = new ArrayList<Flower>();
  24. try {
  25. conn = getConn();
  26. pstmt = conn.prepareStatement("SELECT * FROM flower");
  27. rs = pstmt.executeQuery();
  28. while(rs.next()){
  29. Flower f = new Flower();
  30. f.setId(rs.getLong(1));
  31. f.setName(rs.getString(2));
  32. f.setTypeName(rs.getString(3));
  33. f.setOwnerId(rs.getInt(4));
  34. f.setStoreId(rs.getLong(5));
  35. f.setPrice(rs.getLong(6));
  36. flowerList.add(f);
  37. }
  38. //TODO-1 获取所有鲜花
  39. } catch (SQLException e) {
  40. e.printStackTrace();
  41. } catch (ClassNotFoundException e) {
  42. e.printStackTrace();
  43. } finally {
  44. super.closeAll(conn, pstmt, rs);
  45. }
  46. return flowerList;
  47. }
  48. /**
  49. * 根据参数查询相应符合条件鲜花
  50. */
  51. @Override
  52. public List<Flower> selectFlower(String sql, String[] param) {
  53. List<Flower> flowerList = new ArrayList<Flower>();
  54. try {
  55. conn = getConn(); // 得到数据库连接
  56. pstmt = conn.prepareStatement(sql); // 得到PreparedStatement对象
  57. if (param != null) {
  58. for (int i = 0; i < param.length; i++) {
  59. pstmt.setString(i + 1, param[i]); // 为预编译sql设置参数
  60. }
  61. }
  62. rs = pstmt.executeQuery(); // 执行SQL语句
  63. while (rs.next()) {
  64. Flower flower = new Flower();
  65. flower.setId(rs.getInt(1));
  66. flower.setName(rs.getString(2));
  67. flower.setTypeName(rs.getString(3));
  68. flower.setOwnerId(rs.getInt(4));
  69. flower.setStoreId(rs.getInt(5));
  70. flower.setPrice(rs.getDouble(6));
  71. flowerList.add(flower);
  72. }
  73. } catch (SQLException e) {
  74. e.printStackTrace();
  75. } catch (ClassNotFoundException e) {
  76. e.printStackTrace();
  77. } finally {
  78. super.closeAll(conn, pstmt, rs);
  79. }
  80. return flowerList;
  81. }
  82. /**
  83. * 更新鲜花信息
  84. */
  85. @Override
  86. public int updateFlower(String sql, Object[] param) {
  87. int count = super.executeSQL(sql, param);
  88. return count;
  89. }
  90. }

 文件:src/step2/dao/impl/FlowerStoreDaoImpl.java

  1. package step2.dao.impl;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import step2.dao.BaseDao;
  9. import step2.dao.FlowerStoreDao;
  10. import step2.entity.FlowerStore;
  11. public class FlowerStoreDaoImpl extends BaseDao implements FlowerStoreDao {
  12. private Connection conn = null; // 保存数据库连接
  13. private PreparedStatement pstmt = null; // 用于执行SQL语句
  14. private ResultSet rs = null; // 用户保存查询到的鲜花商店结果集
  15. @Override
  16. public List<FlowerStore> getAllStore() {
  17. List<FlowerStore> storeList = new ArrayList<FlowerStore>();
  18. try {
  19. //TODO-2 获取全部鲜花商店
  20. conn = getConn();
  21. pstmt = conn.prepareStatement("SELECT * FROM flowerstore");
  22. rs = pstmt.executeQuery();
  23. while(rs.next()){
  24. FlowerStore f = new FlowerStore();
  25. f.setId(rs.getLong(1));
  26. f.setName(rs.getString(2));
  27. f.setPassword(rs.getString(3));
  28. f.setBalance(rs.getDouble(4));
  29. storeList.add(f);
  30. }
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. } catch (ClassNotFoundException e) {
  34. e.printStackTrace();
  35. } finally {
  36. super.closeAll(conn, pstmt, rs);
  37. }
  38. return storeList;
  39. }
  40. @Override
  41. public FlowerStore getFlowerStore(String sql, String[] param) {
  42. FlowerStore flowerStore = null;
  43. try {
  44. //TODO-3 根据sql语句获取相应的鲜花商店
  45. conn = getConn();
  46. pstmt = conn.prepareStatement(sql);
  47. for(int i=0;i<param.length;i++){
  48. pstmt.setString(i+1,param[i]);
  49. }
  50. rs = pstmt.executeQuery();
  51. flowerStore = new FlowerStore();
  52. } catch (SQLException e) {
  53. e.printStackTrace();
  54. } catch (ClassNotFoundException e) {
  55. e.printStackTrace();
  56. } finally {
  57. super.closeAll(conn, pstmt, rs);
  58. }
  59. return flowerStore;
  60. }
  61. @Override
  62. public int updateStore(String sql, Object[] param) {
  63. int i = 0;
  64. //TODO-4 修改商店信息
  65. try {
  66. conn = getConn();
  67. pstmt = conn.prepareStatement(sql);
  68. for(int j=0;i<param.length;i++){
  69. pstmt.setObject(i+1,param[i]);
  70. }
  71. i = pstmt.executeUpdate();
  72. } catch (SQLException e) {
  73. e.printStackTrace();
  74. } catch (ClassNotFoundException e) {
  75. e.printStackTrace();
  76. } finally {
  77. super.closeAll(conn, pstmt, rs);
  78. }
  79. return i;
  80. }
  81. }

第三关

 文件:src/step3/service/impl/FlowerOwnerServiceImpl.java

  1. package step3.service.impl;
  2. import java.text.SimpleDateFormat;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import step3.dao.AccountDao;
  8. import step3.dao.FlowerDao;
  9. import step3.dao.FlowerOwnerDao;
  10. import step3.dao.FlowerStoreDao;
  11. import step3.dao.impl.AccountDaoImpl;
  12. import step3.dao.impl.FlowerDaoImpl;
  13. import step3.dao.impl.FlowerOwnerDaoImpl;
  14. import step3.dao.impl.FlowerStoreDaoImpl;
  15. import step3.entity.Flower;
  16. import step3.entity.FlowerOwner;
  17. import step3.entity.FlowerStore;
  18. import step3.service.FlowerOwnerService;
  19. import step3.service.FlowerStoreService;
  20. /**
  21. *
  22. * @author 鲜花顾客实现类
  23. *
  24. */
  25. public class FlowerOwnerServiceImpl implements FlowerOwnerService{
  26. /**
  27. * 顾客购买库存鲜花,根据用户控制台输入获得到的序号,来实际调用购买库存鲜花或者购买新培育的鲜花
  28. */
  29. @Override
  30. public void sell(Flower flower) {
  31. }
  32. @Override
  33. public void FlowerOwnerbuy(Flower flower) {
  34. }
  35. /**
  36. *顾客登录
  37. */
  38. @Override
  39. public FlowerOwner login() {
  40. FlowerOwner owner = new FlowerOwner();
  41. if (null != owner) {
  42. System.out.println("-------恭喜您成功登录-------");
  43. System.out.println("-------您的基本信息:-------");
  44. System.out.println("名字:" + owner.getName());
  45. System.out.println("资金:" + owner.getMoney());
  46. }
  47. return owner;
  48. }
  49. /**
  50. *
  51. * 根据顾客标识符(id)获得到该顾客所有鲜花信息
  52. */
  53. @Override
  54. public List<Flower> getMyFlower(int ownerId) {
  55. //TODO-1 根据顾客id获得到该顾客所有鲜花信息
  56. FlowerDaoImpl flowerDao = new FlowerDaoImpl();
  57. String[] param = {String.valueOf(ownerId)};
  58. String sql = "select * from flower where owner_id = ?";
  59. List<Flower> flowerList = flowerDao.selectFlower(sql, param);
  60. return flowerList;
  61. }
  62. }

文件:src/step3/service/impl/FlowerStoreServiceImpl.java 

  1. package step3.service.impl;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import step3.dao.AccountDao;
  7. import step3.dao.FlowerDao;
  8. import step3.dao.FlowerOwnerDao;
  9. import step3.dao.FlowerStoreDao;
  10. import step3.dao.impl.AccountDaoImpl;
  11. import step3.dao.impl.FlowerDaoImpl;
  12. import step3.dao.impl.FlowerOwnerDaoImpl;
  13. import step3.dao.impl.FlowerStoreDaoImpl;
  14. import step3.entity.Account;
  15. import step3.entity.Flower;
  16. import step3.entity.FlowerOwner;
  17. import step3.entity.FlowerStore;
  18. import step3.service.FlowerStoreService;
  19. /**
  20. * 鲜花商店实现类
  21. */
  22. public class FlowerStoreServiceImpl implements FlowerStoreService {
  23. /**
  24. * 查询鲜花商店账目 其中的1代表鲜花商店卖给顾客,2代表顾客卖给商店,代表鲜花商店的各种交易
  25. */
  26. @Override
  27. public List<Account> account(long storeId) {
  28. String sql = "select * from account where deal_type=? and seller_id=? union select * from account where deal_type=? and buyer_id=?";
  29. String[] param = {"1", String.valueOf(storeId), "2", String.valueOf(storeId)};
  30. AccountDao accountDao = new AccountDaoImpl();
  31. List<Account> list = accountDao.getFlowerStoreAccount(sql, param);
  32. return list;
  33. }
  34. /**
  35. * 修改鲜花商店资金信息
  36. */
  37. @Override
  38. public int modifyAccount(Flower flower, FlowerOwner owner) {
  39. String insertsql = "insert into account(deal_type,flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?,?)";
  40. String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  41. Object[] accountParam = {1, flower.getId(), flower.getStoreId(), owner.getId(), flower.getPrice(), date};
  42. AccountDao accountDao = new AccountDaoImpl();
  43. int insertAccount = accountDao.updateAccount(insertsql, accountParam);
  44. return insertAccount;
  45. }
  46. /**
  47. * 培育鲜花
  48. */
  49. @Override
  50. public Flower bread(String flowerType, FlowerStore store) {
  51. Scanner input = new Scanner(System.in);
  52. System.out.println("请输入您期望培育的鲜花名字:");
  53. String flowerName = input.nextLine();
  54. System.out.println("请输入您期望的鲜花价格:");
  55. String flowerprice = input.nextLine();
  56. String storeId = String.valueOf(store.getId());
  57. String[] flowerParam = {flowerName, flowerType, storeId, flowerprice};
  58. FlowerFactoryImpl flowerFactory = new FlowerFactoryImpl();
  59. Flower flower = flowerFactory.breadNewFlower(flowerParam);
  60. String sql = "insert into flower(`name`,typeName,store_id,price) values(?,?,?,?)";
  61. Object[] param = {flower.getName(), flower.getTypeName(),
  62. flower.getStoreId(), flower.getPrice()};
  63. FlowerDao flowerDao = new FlowerDaoImpl();
  64. int count = flowerDao.updateFlower(sql, param);
  65. if (count > 0) {
  66. System.out.println(store.getName() + "成功培育了一种" + flower.getTypeName() + "鲜花");
  67. }
  68. return flower;
  69. }
  70. /**
  71. * 鲜花商店购买鲜花
  72. */
  73. @Override
  74. public void buy(Flower flower, FlowerStore oldstore) {
  75. String sql = "select * from flowerStore where id=?";
  76. String paramStore[] = {String.valueOf(oldstore.getId())};
  77. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  78. FlowerStore store = storeDao.getFlowerStore(sql, paramStore);
  79. FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
  80. sql = "select * from flowerOwner where id = ?";
  81. String paramOwner[] = {String.valueOf(flower.getOwnerId())};
  82. FlowerOwner owner = ownerDao.selectOwner(sql, paramOwner);
  83. int updateFlower = modifyFlower(flower, null, store);// 更新鲜花信息
  84. if (updateFlower > 0) {// 更新顾客的信息
  85. int updateOwner = modifyOwner(owner, flower, 1);
  86. if (updateOwner > 0) {// 更新鲜花商店的信息
  87. int updateStore = modifyStore(flower, 1, store);
  88. if (updateStore > 0) {// 更新鲜花商店信息
  89. int insertAccount = modifyAccount(flower, owner);
  90. if (insertAccount > 0) {
  91. System.out.println(store.getName() + "已成功购买鲜花:" + flower.getName() + "价格为" + flower.getPrice());
  92. }
  93. }
  94. }
  95. } else {
  96. System.out.println("修改鲜花信息失败");
  97. }
  98. }
  99. /**
  100. * 商店卖鲜花
  101. */
  102. @Override
  103. public void sell(Flower flower) {
  104. FlowerDaoImpl FlowerDao = new FlowerDaoImpl();
  105. FlowerStoreDaoImpl storeDao = new FlowerStoreDaoImpl();
  106. FlowerOwnerDaoImpl ownerDao = new FlowerOwnerDaoImpl();
  107. FlowerStoreService FlowerStore = new FlowerStoreServiceImpl();
  108. String updatesql = "update Flower set store_id = null ,owner_id=? where id=?";
  109. Object[] param = {flower.getOwnerId(), flower.getId()};
  110. int updateFlower = FlowerDao.executeSQL(updatesql, param);// 更新鲜花信息
  111. if (updateFlower > 0) {// 更新顾客的信息
  112. String ownersql = "select * from Flowerowner where id=?";
  113. String ownerparam[] = {String.valueOf(flower.getOwnerId())};
  114. FlowerOwner owner = ownerDao.selectOwner(ownersql, ownerparam);
  115. String updateOwnerSql = "update Flowerowner set money=? where id=?";
  116. double count = flower.getPrice();
  117. Object[] ownerParam = {(owner.getMoney() - count), owner.getId()};
  118. int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);
  119. if (updateOwner > 0) {// 更新鲜花商店的信息
  120. FlowerStore store = FlowerStore.getFlowerStore(flower.getStoreId());
  121. String updateStore = "update Flowerstore set balance=? where id=?";
  122. Object[] storeParam = {(store.getBalance() + count),
  123. store.getId()};
  124. int updatestore = storeDao.executeSQL(updateStore, storeParam);
  125. if (updatestore > 0) {// 更新鲜花商店账户的信息
  126. String insertsql = "insert into account(deal_type,Flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?, ?)";
  127. String date = new SimpleDateFormat("yyyy-MM-dd")
  128. .format(new Date());
  129. Object[] accountParam = {1, flower.getId(), owner.getId(), flower.getStoreId(), count, date};
  130. AccountDao accountDao = new AccountDaoImpl();
  131. int insertAccount = accountDao.updateAccount(insertsql, accountParam);
  132. if (insertAccount > 0) {
  133. System.out.println(store.getName() + "已成功卖出价格为" + flower.getPrice() + "的鲜花" + flower.getName());
  134. }
  135. }
  136. }
  137. }
  138. }
  139. /**
  140. * 查询出所有库存鲜花
  141. */
  142. @Override
  143. public List<Flower> getFlowersInstock(long storeId) {
  144. FlowerDao flowerDao = new FlowerDaoImpl();
  145. String[] param = {String.valueOf(storeId)};
  146. String sql = "";
  147. // 当storeId不为0时,要执行查询指定商店库存鲜花
  148. if (storeId != 0) {
  149. sql = "select * from flower where owner_id is null and store_id=?";
  150. }
  151. // 当storeId为0时,要执行查询所有商店的库存鲜花
  152. if (0 == storeId) {
  153. sql = "select * from flower where owner_id is null";
  154. param = null;
  155. }
  156. List<Flower> flowerList = flowerDao.selectFlower(sql, param);
  157. return flowerList;
  158. }
  159. /**
  160. * 查询出所有新培育的鲜花,
  161. */
  162. @Override
  163. public List<Flower> getFlowersBread() {
  164. FlowerDao FlowerDao = new FlowerDaoImpl();
  165. String sql = "SELECT * FROM flower WHERE owner_id IS NULL AND typeName NOT IN (?,?,?) AND store_id IS Not NULL";
  166. String[] flowerParam = {"香槟玫瑰", "白玫瑰", "粉玫瑰"};
  167. List<Flower> flowerList = FlowerDao.selectFlower(sql, flowerParam);
  168. return flowerList;
  169. }
  170. /**
  171. * 根据顾客信息修改鲜花信息 根据FlowerOwnerEntity和FlowerStoreEntity的值判断是顾客买鲜花或者鲜花商店买鲜花
  172. * FlowerOwnerEntity=null是鲜花商店买鲜花,FlowerStoreEntity=null是顾客买鲜花
  173. */
  174. @Override
  175. public int modifyFlower(Flower flower, FlowerOwner flowerOwner, FlowerStore store) {
  176. String updatesql = null;
  177. long id = 0;
  178. if (null == store) {
  179. updatesql = "update flower set owner_id=? where id=?";
  180. id = flowerOwner.getId();
  181. } else if (null == flowerOwner) {
  182. updatesql = "update flower set store_id=?,owner_id=null where id=?";
  183. id = store.getId();
  184. }
  185. Object[] param = {id, flower.getId()};
  186. FlowerDaoImpl flowerDao = new FlowerDaoImpl();
  187. int updateFlower = flowerDao.executeSQL(updatesql, param);// 更新鲜花信息
  188. return updateFlower;
  189. }
  190. /**
  191. * 修改顾客信息 type=0是顾客买鲜花,type=1是鲜花商店买鲜花,价钱的变动
  192. */
  193. @Override
  194. public int modifyOwner(FlowerOwner owner, Flower flower, int type) {
  195. FlowerOwnerDaoImpl ownerDao = new FlowerOwnerDaoImpl();
  196. String updateOwnerSql = "update flowerowner set money=? where id=?";
  197. double count = 0;
  198. if (0 == type) {
  199. count = (owner.getMoney() - flower.getPrice());
  200. }
  201. if (1 == type) {
  202. count = (owner.getMoney() + flower.getPrice());
  203. }
  204. Object[] ownerParam = {count, owner.getId()};
  205. int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);
  206. return updateOwner;
  207. }
  208. /**
  209. * 修改鲜花商店信息 type=0是顾客买鲜花,type=1是鲜花商店买鲜花
  210. */
  211. @Override
  212. public int modifyStore(Flower flower, int type, FlowerStore oldstore) {
  213. FlowerStoreService store = new FlowerStoreServiceImpl();
  214. FlowerStore flowerStore = store.getFlowerStore(oldstore.getId());
  215. String updateStore = "update flowerstore set balance=? where id=?";
  216. double count = 0;
  217. if (0 == type) {
  218. count = (flowerStore.getBalance() + flower.getPrice());
  219. }
  220. if (1 == type) {
  221. count = (flowerStore.getBalance() - flower.getPrice());
  222. }
  223. Object[] storeParam = {count, oldstore.getId()};
  224. FlowerStoreDaoImpl storeDao = new FlowerStoreDaoImpl();
  225. int updatestore = storeDao.executeSQL(updateStore, storeParam);
  226. return updatestore;
  227. }
  228. /**
  229. * 鲜花商店登录
  230. */
  231. @Override
  232. public FlowerStore login() {
  233. Scanner input = new Scanner(System.in);
  234. FlowerStore flowerStore = null;
  235. // 1、输入鲜花商店名字
  236. boolean type = true;
  237. while (type) {
  238. System.out.println("请先登录,请输入鲜花商店名字:");
  239. String storeName = input.nextLine().trim();
  240. System.out.println("请输入鲜花商店的密码:");
  241. String storePassword = input.nextLine().trim();
  242. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  243. String sql = "select * from flowerstore where name=? and password=?";
  244. String[] param = {storeName, storePassword};
  245. flowerStore = storeDao.getFlowerStore(sql, param);
  246. if (null != flowerStore) {
  247. System.out.println("-------恭喜成功登录-------");
  248. System.out.println("-------鲜花商店的基本信息:-------");
  249. System.out.println("名字:" + flowerStore.getName());
  250. System.out.println("资金:" + flowerStore.getBalance());
  251. type = false;
  252. } else {
  253. System.out.println("登录失败,请确认您的用户名和密码是否正确,重新登录");
  254. type = true;
  255. }
  256. }
  257. return flowerStore;
  258. }
  259. /**
  260. * 查询出所有鲜花商店正在出售的鲜花
  261. */
  262. @Override
  263. public List<Flower> getFlowerSelling() {
  264. FlowerDao flowerDao = new FlowerDaoImpl();
  265. String sql = "select * from flower where owner_id is not null";
  266. String[] flowerParam = null;
  267. List<Flower> flowerList = flowerDao.selectFlower(sql, flowerParam);
  268. return flowerList;
  269. }
  270. /**
  271. * 根据鲜花商店标识符查询鲜花商店信息
  272. */
  273. @Override
  274. public FlowerStore getFlowerStore(long id) {
  275. //TODO-2 根据鲜花商店标识符查询鲜花商店信息方法的实现
  276. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  277. String sql = "select * from flowerStore where id=?";
  278. String paramStore[] = {String.valueOf(id)};
  279. FlowerStore store = storeDao.getFlowerStore(sql, paramStore);
  280. return store;
  281. }
  282. }

第四关

文件:src/step4/service/impl/FlowerStoreServiceImpl.java

  1. package step4.service.impl;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import step4.dao.*;
  7. import step4.dao.impl.*;
  8. import step4.entity.*;
  9. import step4.service.FlowerStoreService;
  10. /**
  11. * 鲜花商店实现类
  12. */
  13. public class FlowerStoreServiceImpl implements FlowerStoreService {
  14. /**
  15. * 查询鲜花商店账目 其中的1代表鲜花商店卖给顾客,2代表顾客卖给商店,代表鲜花商店的各种交易
  16. */
  17. @Override
  18. public List<Account> account(long storeId) {
  19. String sql = "select * from account where deal_type=? and seller_id=? union select * from account where deal_type=? and buyer_id=?";
  20. String[] param = {"1", String.valueOf(storeId), "2", String.valueOf(storeId)};
  21. AccountDao accountDao = new AccountDaoImpl();
  22. List<Account> list = accountDao.getFlowerStoreAccount(sql, param);
  23. return list;
  24. }
  25. /**
  26. * 修改鲜花商店资金信息
  27. */
  28. @Override
  29. public int modifyAccount(Flower flower, FlowerOwner owner) {
  30. String insertsql = "insert into account(deal_type,flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?,?)";
  31. String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  32. Object[] accountParam = {1, flower.getId(), flower.getStoreId(), owner.getId(), flower.getPrice(), date};
  33. AccountDao accountDao = new AccountDaoImpl();
  34. int insertAccount = accountDao.updateAccount(insertsql, accountParam);
  35. return insertAccount;
  36. }
  37. /**
  38. * 培育鲜花
  39. */
  40. @Override
  41. public Flower bread(String flowerType, FlowerStore store) {
  42. Scanner input = new Scanner(System.in);
  43. System.out.println("请输入您期望培育的鲜花名字:");
  44. String flowerName = input.nextLine();
  45. System.out.println("请输入您期望的鲜花价格:");
  46. String flowerprice = input.nextLine();
  47. String storeId = String.valueOf(store.getId());
  48. String[] flowerParam = {flowerName, flowerType, storeId, flowerprice};
  49. FlowerFactoryImpl flowerFactory = new FlowerFactoryImpl();
  50. Flower flower = flowerFactory.breadNewFlower(flowerParam);
  51. String sql = "insert into flower(`name`,typeName,store_id,price) values(?,?,?,?)";
  52. Object[] param = {flower.getName(), flower.getTypeName(), flower.getStoreId(), flower.getPrice()};
  53. FlowerDao flowerDao = new FlowerDaoImpl();
  54. int count = flowerDao.updateFlower(sql, param);
  55. if (count > 0) {
  56. System.out.println(store.getName() + "成功培育了一种" + flower.getTypeName() + "鲜花");
  57. }
  58. return flower;
  59. }
  60. /**
  61. * 鲜花商店购买鲜花
  62. */
  63. @Override
  64. public void buy(Flower flower, FlowerStore oldstore) {
  65. String sql = "select * from flowerStore where id=?";
  66. String paramStore[] = {String.valueOf(oldstore.getId())};
  67. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  68. FlowerStore store = storeDao.getFlowerStore(sql, paramStore);
  69. FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
  70. sql = "select * from flowerOwner where id = ?";
  71. String paramOwner[] = {String.valueOf(flower.getOwnerId())};
  72. FlowerOwner owner = ownerDao.selectOwner(sql, paramOwner);
  73. int updateFlower = modifyFlower(flower, null, store);// 更新鲜花信息
  74. if (updateFlower > 0) {// 更新顾客的信息
  75. int updateOwner = modifyOwner(owner, flower, 1);
  76. if (updateOwner > 0) {// 更新鲜花商店的信息
  77. int updateStore = modifyStore(flower, 1, store);
  78. if (updateStore > 0) {// 更新鲜花商店信息
  79. int insertAccount = modifyAccount(flower, owner);
  80. if (insertAccount > 0) {
  81. System.out.println(store.getName() + "已成功购买鲜花:" + flower.getName() + "价格为" + flower.getPrice());
  82. }
  83. }
  84. }
  85. } else {
  86. System.out.println("修改鲜花信息失败");
  87. }
  88. }
  89. /**
  90. * 商店卖鲜花
  91. */
  92. @Override
  93. public void sell(Flower flower) {
  94. FlowerDaoImpl FlowerDao = new FlowerDaoImpl();
  95. FlowerStoreDaoImpl storeDao = new FlowerStoreDaoImpl();
  96. FlowerOwnerDaoImpl ownerDao = new FlowerOwnerDaoImpl();
  97. FlowerStoreService FlowerStore = new FlowerStoreServiceImpl();
  98. String updatesql = "update Flower set store_id = null ,owner_id=? where id=?";
  99. Object[] param = {flower.getOwnerId(), flower.getId()};
  100. int updateFlower = FlowerDao.executeSQL(updatesql, param);// 更新鲜花信息
  101. if (updateFlower > 0) {// 更新顾客的信息
  102. String ownersql = "select * from Flowerowner where id=?";
  103. String ownerparam[] = {String.valueOf(flower.getOwnerId())};
  104. FlowerOwner owner = ownerDao.selectOwner(ownersql, ownerparam);
  105. String updateOwnerSql = "update Flowerowner set money=? where id=?";
  106. double count = flower.getPrice();
  107. Object[] ownerParam = {(owner.getMoney() - count), owner.getId()};
  108. int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);
  109. if (updateOwner > 0) {// 更新鲜花商店的信息
  110. FlowerStore store = FlowerStore.getFlowerStore(flower.getStoreId());
  111. String updateStore = "update Flowerstore set balance=? where id=?";
  112. Object[] storeParam = {(store.getBalance() + count),
  113. store.getId()};
  114. int updatestore = storeDao.executeSQL(updateStore, storeParam);
  115. if (updatestore > 0) {// 更新鲜花商店账户的信息
  116. String insertsql = "insert into account(deal_type,Flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?, ?)";
  117. String date = new SimpleDateFormat("yyyy-MM-dd")
  118. .format(new Date());
  119. Object[] accountParam = {1, flower.getId(), owner.getId(), flower.getStoreId(), count, date};
  120. AccountDao accountDao = new AccountDaoImpl();
  121. int insertAccount = accountDao.updateAccount(insertsql, accountParam);
  122. if (insertAccount > 0) {
  123. System.out.println(store.getName() + "已成功卖出价格为" + flower.getPrice() + "的鲜花" + flower.getName());
  124. }
  125. }
  126. }
  127. }
  128. }
  129. /**
  130. * 查询出所有库存鲜花
  131. */
  132. @Override
  133. public List<Flower> getFlowersInstock(long storeId) {
  134. FlowerDao flowerDao = new FlowerDaoImpl();
  135. String[] param = {String.valueOf(storeId)};
  136. String sql = "";
  137. // 当storeId不为0时,要执行查询指定商店库存鲜花
  138. if (storeId != 0) {
  139. sql = "select * from flower where owner_id is null and store_id=?";
  140. }
  141. // 当storeId为0时,要执行查询所有商店的库存鲜花
  142. if (0 == storeId) {
  143. sql = "select * from flower where owner_id is null";
  144. param = null;
  145. }
  146. List<Flower> flowerList = flowerDao.selectFlower(sql, param);
  147. return flowerList;
  148. }
  149. /**
  150. * 查询出所有新培育的鲜花,
  151. */
  152. @Override
  153. public List<Flower> getFlowersBread() {
  154. FlowerDao FlowerDao = new FlowerDaoImpl();
  155. String sql = "SELECT * FROM flower WHERE owner_id IS NULL AND typeName NOT IN (?,?,?) AND store_id IS Not NULL";
  156. String[] flowerParam = {"香槟玫瑰", "白玫瑰", "粉玫瑰"};
  157. List<Flower> flowerList = FlowerDao.selectFlower(sql, flowerParam);
  158. return flowerList;
  159. }
  160. /**
  161. * 根据顾客信息修改鲜花信息 根据FlowerOwnerEntity和FlowerStoreEntity的值判断是顾客买鲜花或者鲜花商店买鲜花
  162. * FlowerOwnerEntity=null是鲜花商店买鲜花,FlowerStoreEntity=null是顾客买鲜花
  163. */
  164. @Override
  165. public int modifyFlower(Flower flower, FlowerOwner flowerOwner, FlowerStore store) {
  166. String updatesql = null;
  167. long id = 0;
  168. if (null == store) {
  169. updatesql = "update flower set owner_id=? where id=?";
  170. id = flowerOwner.getId();
  171. } else if (null == flowerOwner) {
  172. updatesql = "update flower set store_id=?,owner_id=null where id=?";
  173. id = store.getId();
  174. }
  175. Object[] param = {id, flower.getId()};
  176. FlowerDaoImpl flowerDao = new FlowerDaoImpl();
  177. int updateFlower = flowerDao.executeSQL(updatesql, param);// 更新鲜花信息
  178. return updateFlower;
  179. }
  180. /**
  181. * 修改顾客信息 type=0是顾客买鲜花,type=1是鲜花商店买鲜花,价钱的变动
  182. */
  183. @Override
  184. public int modifyOwner(FlowerOwner owner, Flower flower, int type) {
  185. FlowerOwnerDaoImpl ownerDao = new FlowerOwnerDaoImpl();
  186. String updateOwnerSql = "update flowerowner set money=? where id=?";
  187. double count = 0;
  188. if (0 == type) {
  189. count = (owner.getMoney() - flower.getPrice());
  190. }
  191. if (1 == type) {
  192. count = (owner.getMoney() + flower.getPrice());
  193. }
  194. Object[] ownerParam = {count, owner.getId()};
  195. int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);
  196. return updateOwner;
  197. }
  198. /**
  199. * 修改鲜花商店信息 type=0是顾客买鲜花,type=1是鲜花商店买鲜花
  200. */
  201. @Override
  202. public int modifyStore(Flower flower, int type, FlowerStore oldstore) {
  203. FlowerStoreService store = new FlowerStoreServiceImpl();
  204. FlowerStore flowerStore = store.getFlowerStore(oldstore.getId());
  205. String updateStore = "update flowerstore set balance=? where id=?";
  206. double count = 0;
  207. if (0 == type) {
  208. count = (flowerStore.getBalance() + flower.getPrice());
  209. }
  210. if (1 == type) {
  211. count = (flowerStore.getBalance() - flower.getPrice());
  212. }
  213. Object[] storeParam = {count, oldstore.getId()};
  214. FlowerStoreDaoImpl storeDao = new FlowerStoreDaoImpl();
  215. int updatestore = storeDao.executeSQL(updateStore, storeParam);
  216. return updatestore;
  217. }
  218. /**
  219. * 鲜花商店登录
  220. *
  221. * @param storeName
  222. * @param storePassword
  223. * @return
  224. */
  225. @Override
  226. public FlowerStore login(String storeName, String storePassword) {
  227. FlowerStore flowerStore;
  228. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  229. //TODO-2 鲜花商店登录(访问数据库,查询鲜花商店名称和密码是否正确)
  230. String sql = "select * from flowerStore where name=? and password=?";
  231. String paramStore[] = {String.valueOf(storeName),String.valueOf(storePassword)};
  232. flowerStore = storeDao.getFlowerStore(sql, paramStore);
  233. return flowerStore;
  234. }
  235. /**
  236. * 查询出所有鲜花商店正在出售的鲜花
  237. */
  238. @Override
  239. public List<Flower> getFlowerSelling() {
  240. FlowerDao flowerDao = new FlowerDaoImpl();
  241. String sql = "select * from flower where owner_id is not null";
  242. String[] flowerParam = null;
  243. List<Flower> flowerList = flowerDao.selectFlower(sql, flowerParam);
  244. return flowerList;
  245. }
  246. /**
  247. * 根据鲜花商店标识符查询鲜花信息
  248. */
  249. @Override
  250. public FlowerStore getFlowerStore(long id) {
  251. String sql = "select * from flowerstore where id=" + id;
  252. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  253. FlowerStore flowerStore = storeDao.getFlowerStore(sql, null);
  254. return flowerStore;
  255. }
  256. }

 文件:src/step4/test/FlowerStoreApp.java

  1. package step4.test;
  2. import dbtest.InitDB;
  3. import step4.entity.*;
  4. import step4.dao.*;
  5. import step4.dao.impl.*;
  6. import step4.service.*;
  7. import step4.service.impl.*;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Scanner;
  11. public class FlowerStoreApp {
  12. private static Scanner input = new Scanner(System.in);
  13. public static void main(String[] args) {
  14. //1-初始化 NySQL 数据库
  15. InitDB db = new InitDB();
  16. //2-启动鲜花商店系统
  17. startFlowerShop();
  18. }
  19. private static void startFlowerShop() {
  20. System.out.println("----------------------鲜花商店启动-----------------");
  21. System.out.println("鲜花信息");
  22. System.out.println("****************************************************");
  23. FlowerDao flowerDao = new FlowerDaoImpl();
  24. List<Flower> flowerList = flowerDao.getAllFlower();
  25. System.out.println("序号\t" + "鲜花名称\t" + "鲜花品种\t" + "鲜花售价\t" + "");
  26. for (int i = 0; i < flowerList.size(); i++) {
  27. Flower flower = flowerList.get(i);
  28. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  29. }
  30. System.out.println("****************************************************");
  31. System.out.print("\n");
  32. System.out.println("顾客信息");
  33. FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
  34. List<FlowerOwner> ownerList = ownerDao.getAllOwner();
  35. System.out.println("****************************************************");
  36. System.out.println("序号\t" + "顾客姓名\t");
  37. for (int i = 0; i < ownerList.size(); i++) {
  38. FlowerOwner owner = ownerList.get(i);
  39. System.out.println((i + 1) + "\t" + owner.getName() + "\t");
  40. }
  41. System.out.println("****************************************************");
  42. System.out.print("\n");
  43. System.out.println("鲜花商店信息");
  44. System.out.println("****************************************************");
  45. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  46. List<FlowerStore> storeList = storeDao.getAllStore();
  47. System.out.println("序号\t" + "鲜花商店名称\t");
  48. for (int i = 0; i < storeList.size(); i++) {
  49. FlowerStore store = storeList.get(i);
  50. System.out.println((i + 1) + "\t" + store.getName() + "\t");
  51. }
  52. System.out.println("****************************************************");
  53. System.out.print("\n");
  54. //系统登录
  55. FlowerStore flowerStore = storeLogin();
  56. }
  57. //鲜花商店登录
  58. private static FlowerStore storeLogin() {
  59. // Scanner input = new Scanner(System.in);
  60. FlowerStoreService fsService = new FlowerStoreServiceImpl();
  61. FlowerStore flowerStore = null;
  62. //TODO-1 鲜花商店登录代码实现(允许多次输入商店名称和密码)
  63. while(true){
  64. System.out.println("请先登录,请输入鲜花商店名字:");
  65. String n = input.next();
  66. System.out.println("请输入鲜花商店的密码:");
  67. String p = input.next();
  68. flowerStore = fsService.login(n,p);
  69. if(flowerStore==null){
  70. System.out.println("登录失败,请确认您的用户名和密码是否正确,重新登录");
  71. continue;
  72. }
  73. break;
  74. }
  75. System.out.println("-------恭喜成功登录-------");
  76. System.out.println("-------鲜花商店的基本信息:-------");
  77. System.out.printf("名字:%s\n资金:%.1f\n",flowerStore.getName(),flowerStore.getBalance());
  78. System.out.println("您已登录成功,可以进行如下操作");
  79. StoreChoose(flowerStore);
  80. return flowerStore;
  81. }
  82. //创建鲜花商店
  83. private static FlowerStore createFlowerStore(FlowerStore store) {
  84. FlowerStoreFactory storeFactory = new FlowerStoreFactoryImpl();
  85. storeFactory.createFlowerStore();
  86. IsStoreLogOut(store);
  87. return store;//返回的是鲜花商店创建者
  88. }
  89. private static void IsStoreLogOut(FlowerStore store) {
  90. System.out.println("您是否继续其它操作若是请输入y,退出请按任意键");
  91. //Scanner input = new Scanner(System.in);
  92. String code = input.next();
  93. if (code.equals("y")) {
  94. StoreChoose(store);
  95. } else {
  96. System.out.println("您已成功退出系统");
  97. }
  98. }
  99. private static void StoreChoose(FlowerStore store) {
  100. System.out.println("1:购买鲜花");
  101. System.out.println("2:卖出鲜花");
  102. System.out.println("3:培育鲜花");
  103. System.out.println("4:查询待售鲜花");
  104. System.out.println("5:查看商店结余");
  105. System.out.println("6:查看商店账目");
  106. System.out.println("7:开鲜花商店");
  107. System.out.println("请根据需要执行的操作,选择序号输入,退出请输入0");
  108. // Scanner input = new Scanner(System.in);
  109. boolean type = true;
  110. while (type) {
  111. int num = input.nextInt();
  112. switch (num) {
  113. case 0:
  114. System.out.println("退出成功");
  115. type = false;
  116. break;
  117. case 1:
  118. storeBuy(store);
  119. type = false;
  120. break;
  121. case 2:
  122. storeSell(store);
  123. type = false;
  124. break;
  125. case 3:
  126. storeBread(store);//传入登录的商店
  127. type = false;
  128. break;
  129. case 4:
  130. queryFlowerStock(store.getId());
  131. type = false;
  132. break;
  133. case 5:
  134. queryStoreBalance(store);
  135. type = false;
  136. break;
  137. case 6:
  138. getAccount(store.getId());
  139. type = false;
  140. break;
  141. case 7:
  142. createFlowerStore(store);
  143. type = false;
  144. break;
  145. default:
  146. System.out.println("输入有误,请重新输入");
  147. type = true;
  148. break;
  149. }
  150. }
  151. }
  152. //获得鲜花商店的资金1代表商店顾客,2顾客卖给商店,3代表顾客之间的交易
  153. private static FlowerStore getAccount(long id) {
  154. FlowerStoreService storeService = new FlowerStoreServiceImpl();
  155. FlowerStore store = storeService.getFlowerStore(id);
  156. List<Account> list = storeService.account(id);
  157. for (int i = 0; i < list.size(); i++) {
  158. Account account = list.get(i);
  159. String type = null;
  160. if (1 == account.getDealType()) {
  161. type = "商店卖给顾客";
  162. } else if (2 == account.getDealType()) {
  163. type = "顾客卖给商店";
  164. } else {
  165. type = "顾客之间交易";
  166. }
  167. System.out.println("第" + (i + 1) + "笔交易,交易类型为:" + type + ",交易金额是:"
  168. + account.getPrice());
  169. }
  170. IsStoreLogOut(store);
  171. return store;
  172. }
  173. //查询商店余额
  174. private static FlowerStore queryStoreBalance(FlowerStore store) {
  175. double balance = store.getBalance();
  176. System.out.println(store.getName() + "鲜花商店的结余为:" + balance);
  177. IsStoreLogOut(store);
  178. return store;
  179. }
  180. //查询待售鲜花
  181. private static FlowerStore queryFlowerStock(long storeId) {
  182. FlowerStoreService flowerStoreService = new FlowerStoreServiceImpl();
  183. FlowerStore store = flowerStoreService.getFlowerStore(storeId);
  184. Flower flower = null;
  185. List<Flower> flowerList = flowerStoreService.getFlowersInstock(storeId);
  186. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  187. for (int i = 0; i < flowerList.size(); i++) {
  188. flower = flowerList.get(i);
  189. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  190. }
  191. IsStoreLogOut(store);
  192. return store;
  193. }
  194. //鲜花商店培育鲜花
  195. private static FlowerStore storeBread(FlowerStore store) {
  196. FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
  197. //Scanner input = new Scanner(System.in);
  198. System.out.println("请输入要培育鲜花的品种(例如:玫瑰))");
  199. String flowerType = input.next();
  200. flowerStore.bread(flowerType, store);
  201. IsStoreLogOut(store);
  202. return store;
  203. }
  204. //以商店的身份登录,商店向顾客出售鲜花
  205. private static void storeSell(FlowerStore store) {
  206. //Scanner input = new Scanner(System.in);
  207. FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
  208. Flower flower = null;
  209. List<Flower> flowerList = flowerStore.getFlowersInstock(store.getId());
  210. System.out.println("-------以下是鲜花商店正在出售的鲜花-------");
  211. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  212. for (int i = 0; i < flowerList.size(); i++) {
  213. flower = flowerList.get(i);
  214. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  215. }
  216. System.out.println("---------请选择要购买的鲜花序号--------");
  217. boolean type = true;
  218. while (type) {
  219. int num = input.nextInt();
  220. if ((num - 1) < flowerList.size() && (num - 1) >= 0) {
  221. flower = flowerList.get(num - 1);
  222. System.out.println("------要卖出的鲜花信息如下------");
  223. System.out.println("鲜花名称为:" + flower.getName() + " 鲜花类别是:" + flower.getTypeName() + "鲜花价格是:" + flower.getPrice());
  224. System.out.println("请确认是否卖出,y代表卖出,n代表不卖");
  225. String code = input.next();
  226. if (null != code) {
  227. if ("y".equals(code)) {
  228. System.out.println("------下面是现有顾客买家,请选择您要卖给买家序号------");
  229. List<FlowerOwner> ownerList = new ArrayList<FlowerOwner>();
  230. FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
  231. ownerList = ownerDao.getAllOwner();
  232. FlowerOwner flowerOwner = null;
  233. System.out.println("序号\t" + "顾客姓名\t");
  234. for (int i = 0; i < ownerList.size(); i++) {
  235. flowerOwner = ownerList.get(i);
  236. System.out.println((i + 1) + "\t" + flowerOwner.getName() + "\t");
  237. }
  238. num = input.nextInt();
  239. if ((num - 1) < ownerList.size() && (num - 1) >= 0) {
  240. flowerOwner = ownerList.get(num - 1);
  241. }
  242. flower.setOwnerId(flowerOwner.getId());
  243. flowerStore.sell(flower);
  244. } else if ("n".equals(code)) {
  245. System.out
  246. .println("--------您选择放弃本次交易,希望您再次光顾----------");
  247. } else {
  248. System.out.println("--------您的输入有误----------");
  249. }
  250. }
  251. type = true;
  252. } else {
  253. System.out.println("输入有误,请按照序号重新输入");
  254. type = false;
  255. }
  256. type = false;// 标识符更改为false,退出系统
  257. }
  258. IsStoreLogOut(store);
  259. }
  260. //以商店的身份登录,商店找顾客购买鲜花
  261. private static void storeBuy(FlowerStore store) {
  262. //Scanner input = new Scanner(System.in);
  263. FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
  264. Flower flower = null;
  265. List<Flower> flowerList = flowerStore.getFlowerSelling();
  266. System.out.println("-------以下是顾客正在出售的鲜花-------");
  267. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  268. for (int i = 0; i < flowerList.size(); i++) {
  269. flower = flowerList.get(i);
  270. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  271. }
  272. System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
  273. int num = input.nextInt();
  274. flower = flowerList.get(num - 1);
  275. //System.out.println("选中的鲜花信息"+flower.getName()+"id"+flower.getId());
  276. flowerStore.buy(flower, store);
  277. IsStoreLogOut(store);
  278. }
  279. //顾客登录
  280. private static FlowerOwner ownerLogin() {
  281. //Scanner input = new Scanner(System.in);
  282. System.out.println("请先登录,请您输入姓名:");
  283. String ownerName = input.nextLine().trim();
  284. System.out.println("请您输入密码:");
  285. String ownerPassword = input.nextLine().trim();
  286. FlowerOwnerService flowerOwner = new FlowerOwnerServiceImpl();
  287. FlowerOwner Owner = flowerOwner.login(ownerName, ownerPassword);
  288. boolean reg = true;
  289. while (reg) {
  290. if (null == Owner) {
  291. System.out.println("登录失败,请确认您的用户名和密码后重新输入");
  292. System.out.println("请先登录,请您输入姓名:");
  293. ownerName = input.nextLine().trim();
  294. System.out.println("请您输入密码:");
  295. ownerPassword = input.nextLine().trim();
  296. Owner = flowerOwner.login(ownerName, ownerPassword);
  297. reg = true;
  298. } else {
  299. reg = false;
  300. System.out.println("登录成功,您可以购买和卖出鲜花,如果您想购买鲜花请输入1,如果想卖出鲜花请输入2");
  301. System.out.println("1:购买鲜花");
  302. System.out.println("2:卖出鲜花");
  303. boolean type = true;
  304. //----------------------
  305. FlowerCirculate(type, Owner);
  306. }
  307. }
  308. return Owner;
  309. }
  310. private static boolean FlowerCirculate(boolean type, FlowerOwner owner) {
  311. while (type) {
  312. int num = input.nextInt();
  313. if (1 == num) {
  314. ownerBuy(owner);
  315. type = false;
  316. } else if (2 == num) {
  317. ownerSell(owner);
  318. type = false;
  319. } else {
  320. System.out.println("输入有误,请重新输入");
  321. type = true;
  322. }
  323. }
  324. return type;
  325. }
  326. //顾客向商店卖鲜花
  327. private static void ownerSell(FlowerOwner flowerowner) {
  328. }
  329. //顾客找鲜花商店买鲜花
  330. private static void ownerBuy(FlowerOwner flowerowner) {
  331. }
  332. }

鲜花商店 2

第一关:

 文件:src/step5/service/impl/FlowerStoreServiceImpl.java

  1. package step5.service.impl;
  2. import step5.dao.AccountDao;
  3. import step5.dao.FlowerDao;
  4. import step5.dao.FlowerOwnerDao;
  5. import step5.dao.FlowerStoreDao;
  6. import step5.dao.impl.AccountDaoImpl;
  7. import step5.dao.impl.FlowerDaoImpl;
  8. import step5.dao.impl.FlowerOwnerDaoImpl;
  9. import step5.dao.impl.FlowerStoreDaoImpl;
  10. import step5.entity.Account;
  11. import step5.entity.Flower;
  12. import step5.entity.FlowerOwner;
  13. import step5.entity.FlowerStore;
  14. import step5.service.FlowerStoreService;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Date;
  17. import java.util.List;
  18. import java.util.Scanner;
  19. /**
  20. * 鲜花商店实现类
  21. */
  22. public class FlowerStoreServiceImpl implements FlowerStoreService {
  23. /**
  24. * 查询鲜花商店账目 其中的1代表鲜花商店卖给顾客,2代表顾客卖给商店,代表鲜花商店的各种交易
  25. */
  26. @Override
  27. public List<Account> account(long storeId) {
  28. String sql = "select * from account where deal_type=? and seller_id=? union select * from account where deal_type=? and buyer_id=?";
  29. String[] param = {"1", String.valueOf(storeId), "2", String.valueOf(storeId)};
  30. AccountDao accountDao = new AccountDaoImpl();
  31. List<Account> list = accountDao.getFlowerStoreAccount(sql, param);
  32. return list;
  33. }
  34. /**
  35. * 修改鲜花商店资金信息
  36. */
  37. @Override
  38. public int modifyAccount(Flower flower, FlowerOwner owner) {
  39. String insertsql = "insert into account(deal_type,flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?,?)";
  40. String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  41. Object[] accountParam = {1, flower.getId(), flower.getStoreId(), owner.getId(), flower.getPrice(), date};
  42. AccountDao accountDao = new AccountDaoImpl();
  43. int insertAccount = accountDao.updateAccount(insertsql, accountParam);
  44. return insertAccount;
  45. }
  46. /**
  47. * 培育鲜花
  48. */
  49. @Override
  50. public Flower bread(String flowerType, FlowerStore store) {
  51. Scanner input = new Scanner(System.in);
  52. System.out.println("请输入您期望培育的鲜花名字:");
  53. String flowerName = input.nextLine();
  54. System.out.println("请输入您期望的鲜花价格:");
  55. String flowerprice = input.nextLine();
  56. String storeId = String.valueOf(store.getId());
  57. String[] flowerParam = {flowerName, flowerType, storeId, flowerprice};
  58. FlowerFactoryImpl flowerFactory = new FlowerFactoryImpl();
  59. Flower flower = flowerFactory.breadNewFlower(flowerParam);
  60. String sql = "insert into flower(`name`,typeName,store_id,price) values(?,?,?,?)";
  61. Object[] param = {flower.getName(), flower.getTypeName(), flower.getStoreId(), flower.getPrice()};
  62. FlowerDao flowerDao = new FlowerDaoImpl();
  63. int count = flowerDao.updateFlower(sql, param);
  64. if (count > 0) {
  65. System.out.println(store.getName() + "成功培育了一种" + flower.getTypeName() + "鲜花");
  66. }
  67. return flower;
  68. }
  69. /**
  70. * 鲜花商店购买鲜花
  71. */
  72. @Override
  73. public void buy(Flower flower, FlowerStore oldstore) {
  74. String sql = "select * from flowerStore where id=?";
  75. String paramStore[] = {String.valueOf(oldstore.getId())};
  76. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  77. FlowerStore store = storeDao.getFlowerStore(sql, paramStore);
  78. FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
  79. sql = "select * from flowerOwner where id = ?";
  80. String paramOwner[] = {String.valueOf(flower.getOwnerId())};
  81. FlowerOwner owner = ownerDao.selectOwner(sql, paramOwner);
  82. int updateFlower = modifyFlower(flower, null, store);// 更新鲜花信息
  83. if (updateFlower > 0) {// 更新顾客的信息
  84. int updateOwner = modifyOwner(owner, flower, 1);
  85. if (updateOwner > 0) {// 更新鲜花商店的信息
  86. int updateStore = modifyStore(flower, 1, store);
  87. if (updateStore > 0) {// 更新鲜花商店信息
  88. int insertAccount = modifyAccount(flower, owner);
  89. if (insertAccount > 0) {
  90. System.out.println(store.getName() + "已成功购买鲜花:" + flower.getName() + "价格为" + flower.getPrice());
  91. }
  92. }
  93. }
  94. } else {
  95. System.out.println("修改鲜花信息失败");
  96. }
  97. }
  98. /**
  99. * 商店卖鲜花
  100. */
  101. @Override
  102. public void sell(Flower flower) {
  103. FlowerDaoImpl FlowerDao = new FlowerDaoImpl();
  104. FlowerStoreDaoImpl storeDao = new FlowerStoreDaoImpl();
  105. FlowerOwnerDaoImpl ownerDao = new FlowerOwnerDaoImpl();
  106. FlowerStoreService FlowerStore = new FlowerStoreServiceImpl();
  107. String updatesql = "update Flower set store_id = null ,owner_id=? where id=?";
  108. Object[] param = {flower.getOwnerId(), flower.getId()};
  109. int updateFlower = FlowerDao.executeSQL(updatesql, param);// 更新鲜花信息
  110. if (updateFlower > 0) {// 更新顾客的信息
  111. String ownersql = "select * from Flowerowner where id=?";
  112. String ownerparam[] = {String.valueOf(flower.getOwnerId())};
  113. FlowerOwner owner = ownerDao.selectOwner(ownersql, ownerparam);
  114. String updateOwnerSql = "update Flowerowner set money=? where id=?";
  115. double count = flower.getPrice();
  116. Object[] ownerParam = {(owner.getMoney() - count), owner.getId()};
  117. int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);
  118. if (updateOwner > 0) {// 更新鲜花商店的信息
  119. FlowerStore store = FlowerStore.getFlowerStore(flower.getStoreId());
  120. String updateStore = "update Flowerstore set balance=? where id=?";
  121. Object[] storeParam = {(store.getBalance() + count),
  122. store.getId()};
  123. int updatestore = storeDao.executeSQL(updateStore, storeParam);
  124. if (updatestore > 0) {// 更新鲜花商店账户的信息
  125. String insertsql = "insert into account(deal_type,Flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?, ?)";
  126. String date = new SimpleDateFormat("yyyy-MM-dd")
  127. .format(new Date());
  128. Object[] accountParam = {1, flower.getId(), owner.getId(), flower.getStoreId(), count, date};
  129. AccountDao accountDao = new AccountDaoImpl();
  130. int insertAccount = accountDao.updateAccount(insertsql, accountParam);
  131. if (insertAccount > 0) {
  132. System.out.println(store.getName() + "已成功卖出价格为" + flower.getPrice() + "的鲜花" + flower.getName());
  133. }
  134. }
  135. }
  136. }
  137. }
  138. /**
  139. * 查询出所有库存鲜花
  140. */
  141. @Override
  142. public List<Flower> getFlowersInstock(long storeId) {
  143. FlowerDao flowerDao = new FlowerDaoImpl();
  144. String[] param = {String.valueOf(storeId)};
  145. String sql = "";
  146. // 当storeId不为 0 时,要执行查询指定商店库存鲜花
  147. if (storeId != 0) {
  148. // if (storeId != 1) {
  149. //TODO-2 填写查询指定商店库存鲜花SQL语句( sql="xxx"; )
  150. sql = "select * from Flower where store_id != ? and owner_id is not null";
  151. }
  152. // 当storeId为0时,要执行查询所有商店的库存鲜花
  153. if (0 == storeId) {
  154. //TODO-3 填写查询所有商店库存鲜花SQL语句( sql="xxx"; )
  155. // sql = "select * from Flower";
  156. sql = "select * from Flower where store_id <> 0 and owner_id is null";
  157. param = null;
  158. }
  159. List<Flower> flowerList = flowerDao.selectFlower(sql, param);
  160. return flowerList;
  161. }
  162. /**
  163. * 查询出所有新培育的鲜花,
  164. */
  165. @Override
  166. public List<Flower> getFlowersBread() {
  167. FlowerDao FlowerDao = new FlowerDaoImpl();
  168. String sql = "SELECT * FROM flower WHERE owner_id IS NULL AND typeName NOT IN (?,?,?) AND store_id IS Not NULL";
  169. String[] flowerParam = {"香槟玫瑰", "白玫瑰", "粉玫瑰"};
  170. List<Flower> flowerList = FlowerDao.selectFlower(sql, flowerParam);
  171. return flowerList;
  172. }
  173. /**
  174. * 根据顾客信息修改鲜花信息 根据FlowerOwnerEntity和FlowerStoreEntity的值判断是顾客买鲜花或者鲜花商店买鲜花
  175. * FlowerOwnerEntity=null是鲜花商店买鲜花,FlowerStoreEntity=null是顾客买鲜花
  176. */
  177. @Override
  178. public int modifyFlower(Flower flower, FlowerOwner flowerOwner, FlowerStore store) {
  179. String updatesql = null;
  180. long id = 0;
  181. if (null == store) {
  182. updatesql = "update flower set owner_id=? where id=?";
  183. id = flowerOwner.getId();
  184. } else if (null == flowerOwner) {
  185. updatesql = "update flower set store_id=?,owner_id=null where id=?";
  186. id = store.getId();
  187. }
  188. Object[] param = {id, flower.getId()};
  189. FlowerDaoImpl flowerDao = new FlowerDaoImpl();
  190. int updateFlower = flowerDao.executeSQL(updatesql, param);// 更新鲜花信息
  191. return updateFlower;
  192. }
  193. /**
  194. * 修改顾客信息 type=0是顾客买鲜花,type=1是鲜花商店买鲜花,价钱的变动
  195. */
  196. @Override
  197. public int modifyOwner(FlowerOwner owner, Flower flower, int type) {
  198. FlowerOwnerDaoImpl ownerDao = new FlowerOwnerDaoImpl();
  199. String updateOwnerSql = "update flowerowner set money=? where id=?";
  200. double count = 0;
  201. if (0 == type) {
  202. count = (owner.getMoney() - flower.getPrice());
  203. }
  204. if (1 == type) {
  205. count = (owner.getMoney() + flower.getPrice());
  206. }
  207. Object[] ownerParam = {count, owner.getId()};
  208. int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);
  209. return updateOwner;
  210. }
  211. /**
  212. * 修改鲜花商店信息 type=0是顾客买鲜花,type=1是鲜花商店买鲜花
  213. */
  214. @Override
  215. public int modifyStore(Flower flower, int type, FlowerStore oldstore) {
  216. FlowerStoreService store = new FlowerStoreServiceImpl();
  217. FlowerStore flowerStore = store.getFlowerStore(oldstore.getId());
  218. String updateStore = "update flowerstore set balance=? where id=?";
  219. double count = 0;
  220. if (0 == type) {
  221. count = (flowerStore.getBalance() + flower.getPrice());
  222. }
  223. if (1 == type) {
  224. count = (flowerStore.getBalance() - flower.getPrice());
  225. }
  226. Object[] storeParam = {count, oldstore.getId()};
  227. FlowerStoreDaoImpl storeDao = new FlowerStoreDaoImpl();
  228. int updatestore = storeDao.executeSQL(updateStore, storeParam);
  229. return updatestore;
  230. }
  231. /**
  232. * 鲜花商店登录
  233. *
  234. * @param storeName
  235. * @param storePassword
  236. * @return
  237. */
  238. @Override
  239. public FlowerStore login(String storeName, String storePassword) {
  240. FlowerStore flowerStore;
  241. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  242. String sql = "select * from flowerstore where name=? and password=?";
  243. String[] param = {storeName, storePassword};
  244. flowerStore = storeDao.getFlowerStore(sql, param);
  245. return flowerStore;
  246. }
  247. /**
  248. * 查询出所有鲜花商店正在出售的鲜花
  249. */
  250. @Override
  251. public List<Flower> getFlowerSelling() {
  252. FlowerDao flowerDao = new FlowerDaoImpl();
  253. String sql = "select * from flower where owner_id is not null";
  254. String[] flowerParam = null;
  255. List<Flower> flowerList = flowerDao.selectFlower(sql, flowerParam);
  256. return flowerList;
  257. }
  258. /**
  259. * 根据鲜花商店标识符查询鲜花信息
  260. */
  261. @Override
  262. public FlowerStore getFlowerStore(long id) {
  263. String sql = "select * from flowerstore where id=" + id;
  264. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  265. FlowerStore flowerStore = storeDao.getFlowerStore(sql, null);
  266. return flowerStore;
  267. }
  268. }

文件:src/step5/test/FlowerStoreApp.java

  1. package step5.test;
  2. import dbtest.InitDB;
  3. import step5.dao.*;
  4. import step5.dao.impl.*;
  5. import step5.entity.*;
  6. import step5.service.*;
  7. import step5.service.impl.*;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Scanner;
  11. public class FlowerStoreApp {
  12. private static Scanner input = new Scanner(System.in);
  13. public static void main(String[] args) {
  14. //1-初始化 NySQL 数据库
  15. InitDB db = new InitDB();
  16. //2-启动鲜花商店系统
  17. startFlowerShop();
  18. }
  19. private static void startFlowerShop() {
  20. System.out.println("----------------------鲜花商店启动-----------------");
  21. System.out.println("鲜花信息");
  22. System.out.println("****************************************************");
  23. FlowerDao flowerDao = new FlowerDaoImpl();
  24. List<Flower> flowerList = flowerDao.getAllFlower();
  25. System.out.println("序号\t" + "鲜花名称\t" + "鲜花品种\t" + "鲜花售价\t" + "");
  26. for (int i = 0; i < flowerList.size(); i++) {
  27. Flower flower = flowerList.get(i);
  28. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  29. }
  30. System.out.println("****************************************************");
  31. System.out.print("\n");
  32. System.out.println("顾客信息");
  33. FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
  34. List<FlowerOwner> ownerList = ownerDao.getAllOwner();
  35. System.out.println("****************************************************");
  36. System.out.println("序号\t" + "顾客姓名\t");
  37. for (int i = 0; i < ownerList.size(); i++) {
  38. FlowerOwner owner = ownerList.get(i);
  39. System.out.println((i + 1) + "\t" + owner.getName() + "\t");
  40. }
  41. System.out.println("****************************************************");
  42. System.out.print("\n");
  43. System.out.println("鲜花商店信息");
  44. System.out.println("****************************************************");
  45. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  46. List<FlowerStore> storeList = storeDao.getAllStore();
  47. System.out.println("序号\t" + "鲜花商店名称\t");
  48. for (int i = 0; i < storeList.size(); i++) {
  49. FlowerStore store = storeList.get(i);
  50. System.out.println((i + 1) + "\t" + store.getName() + "\t");
  51. }
  52. System.out.println("****************************************************");
  53. System.out.print("\n");
  54. //系统登录
  55. System.out.println("请选择输入登录模式,输入1为顾客登录,输入2为鲜花商店登录");
  56. boolean type = true;
  57. String num;
  58. while (type) {
  59. num = input.nextLine();
  60. if ("1".equals(num)) {
  61. ownerLogin();
  62. type = false;
  63. } else if ("2".equals(num)) {
  64. storeLogin();
  65. type = false;
  66. } else {
  67. System.out.println("输入有误,请按照指定规则输入");
  68. System.out.println("请选择登录模式,输入1为顾客登录,输入2为鲜花商店登录");
  69. type = true;
  70. }
  71. }
  72. }
  73. //鲜花商店登录
  74. private static FlowerStore storeLogin() {
  75. FlowerStoreService fsService = new FlowerStoreServiceImpl();
  76. FlowerStore flowerStore = null;
  77. //TODO-1 鲜花商店登录代码实现
  78. boolean type = true;
  79. while (type) {
  80. System.out.println("请先登录,请输入鲜花商店名字:");
  81. String storeName = input.nextLine().trim();
  82. System.out.println("请输入鲜花商店的密码:");
  83. String storePassword = input.nextLine().trim();
  84. flowerStore = fsService.login(storeName, storePassword);
  85. if (null != flowerStore) {
  86. System.out.println("-------恭喜成功登录-------");
  87. System.out.println("-------鲜花商店的基本信息:-------");
  88. System.out.println("名字:" + flowerStore.getName());
  89. System.out.println("资金:" + flowerStore.getBalance());
  90. type = false;
  91. } else {
  92. System.out.println("登录失败,请确认您的用户名和密码是否正确,重新登录");
  93. type = true;
  94. }
  95. }
  96. System.out.println("您已登录成功,可以进行如下操作");
  97. StoreChoose(flowerStore);
  98. return flowerStore;
  99. }
  100. //创建鲜花商店
  101. private static FlowerStore createFlowerStore(FlowerStore store) {
  102. FlowerStoreFactory storeFactory = new FlowerStoreFactoryImpl();
  103. storeFactory.createFlowerStore();
  104. IsStoreLogOut(store);
  105. return store;//返回的是鲜花商店创建者
  106. }
  107. private static void IsStoreLogOut(FlowerStore store) {
  108. System.out.println("您是否继续其它操作若是请输入y,退出请按任意键");
  109. String code = input.next();
  110. if (code.equals("y")) {
  111. StoreChoose(store);
  112. } else {
  113. System.out.println("您已成功退出系统");
  114. }
  115. }
  116. private static void StoreChoose(FlowerStore store) {
  117. System.out.println("1:购买鲜花");
  118. System.out.println("2:卖出鲜花");
  119. System.out.println("3:培育鲜花");
  120. System.out.println("4:查询待售鲜花");
  121. System.out.println("5:查看商店结余");
  122. System.out.println("6:查看商店账目");
  123. System.out.println("7:开鲜花商店");
  124. System.out.println("请根据需要执行的操作,选择序号输入,退出请输入0");
  125. boolean type = true;
  126. while (type) {
  127. int num = input.nextInt();
  128. switch (num) {
  129. case 0:
  130. System.out.println("退出成功");
  131. type = false;
  132. break;
  133. case 1:
  134. storeBuy(store);
  135. type = false;
  136. break;
  137. case 2:
  138. storeSell(store);
  139. type = false;
  140. break;
  141. case 3:
  142. storeBread(store);//传入登录的商店
  143. type = false;
  144. break;
  145. case 4:
  146. queryFlowerStock(store.getId());
  147. type = false;
  148. break;
  149. case 5:
  150. queryStoreBalance(store);
  151. type = false;
  152. break;
  153. case 6:
  154. getAccount(store.getId());
  155. type = false;
  156. break;
  157. case 7:
  158. createFlowerStore(store);
  159. type = false;
  160. break;
  161. default:
  162. System.out.println("输入有误,请重新输入");
  163. type = true;
  164. break;
  165. }
  166. }
  167. }
  168. //获得鲜花商店的资金1代表商店顾客,2顾客卖给商店,3代表顾客之间的交易
  169. private static FlowerStore getAccount(long id) {
  170. FlowerStoreService storeService = new FlowerStoreServiceImpl();
  171. FlowerStore store = storeService.getFlowerStore(id);
  172. List<Account> list = storeService.account(id);
  173. for (int i = 0; i < list.size(); i++) {
  174. Account account = list.get(i);
  175. String type = null;
  176. if (1 == account.getDealType()) {
  177. type = "商店卖给顾客";
  178. } else if (2 == account.getDealType()) {
  179. type = "顾客卖给商店";
  180. } else {
  181. type = "顾客之间交易";
  182. }
  183. System.out.println("第" + (i + 1) + "笔交易,交易类型为:" + type + ",交易金额是:"
  184. + account.getPrice());
  185. }
  186. IsStoreLogOut(store);
  187. return store;
  188. }
  189. //查询商店余额
  190. private static FlowerStore queryStoreBalance(FlowerStore store) {
  191. double balance = store.getBalance();
  192. System.out.println(store.getName() + "鲜花商店的结余为:" + balance);
  193. IsStoreLogOut(store);
  194. return store;
  195. }
  196. //查询待售鲜花
  197. private static FlowerStore queryFlowerStock(long storeId) {
  198. FlowerStoreService flowerStoreService = new FlowerStoreServiceImpl();
  199. FlowerStore store = flowerStoreService.getFlowerStore(storeId);
  200. Flower flower = null;
  201. List<Flower> flowerList = flowerStoreService.getFlowersInstock(storeId);
  202. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  203. for (int i = 0; i < flowerList.size(); i++) {
  204. flower = flowerList.get(i);
  205. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  206. }
  207. IsStoreLogOut(store);
  208. return store;
  209. }
  210. //鲜花商店培育鲜花
  211. private static FlowerStore storeBread(FlowerStore store) {
  212. FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
  213. System.out.println("请输入要培育鲜花的品种(例如:玫瑰))");
  214. String flowerType = input.next();
  215. flowerStore.bread(flowerType, store);
  216. IsStoreLogOut(store);
  217. return store;
  218. }
  219. //以商店的身份登录,商店向顾客出售鲜花
  220. private static void storeSell(FlowerStore store) {
  221. FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
  222. Flower flower = null;
  223. List<Flower> flowerList = flowerStore.getFlowersInstock(store.getId());
  224. System.out.println("-------以下是鲜花商店正在出售的鲜花-------");
  225. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  226. for (int i = 0; i < flowerList.size(); i++) {
  227. flower = flowerList.get(i);
  228. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  229. }
  230. System.out.println("---------请选择要购买的鲜花序号--------");
  231. boolean type = true;
  232. while (type) {
  233. int num = input.nextInt();
  234. if ((num - 1) < flowerList.size() && (num - 1) >= 0) {
  235. flower = flowerList.get(num - 1);
  236. System.out.println("------要卖出的鲜花信息如下------");
  237. System.out.println("鲜花名称为:" + flower.getName() + " 鲜花类别是:" + flower.getTypeName() + "鲜花价格是:" + flower.getPrice());
  238. System.out.println("请确认是否卖出,y代表卖出,n代表不卖");
  239. String code = input.next();
  240. if (null != code) {
  241. if ("y".equals(code)) {
  242. System.out.println("------下面是现有顾客买家,请选择您要卖给买家序号------");
  243. List<FlowerOwner> ownerList = new ArrayList<FlowerOwner>();
  244. FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
  245. ownerList = ownerDao.getAllOwner();
  246. FlowerOwner flowerOwner = null;
  247. System.out.println("序号\t" + "顾客姓名\t");
  248. for (int i = 0; i < ownerList.size(); i++) {
  249. flowerOwner = ownerList.get(i);
  250. System.out.println((i + 1) + "\t" + flowerOwner.getName() + "\t");
  251. }
  252. num = input.nextInt();
  253. if ((num - 1) < ownerList.size() && (num - 1) >= 0) {
  254. flowerOwner = ownerList.get(num - 1);
  255. }
  256. flower.setOwnerId(flowerOwner.getId());
  257. flowerStore.sell(flower);
  258. } else if ("n".equals(code)) {
  259. System.out
  260. .println("--------您选择放弃本次交易,希望您再次光顾----------");
  261. } else {
  262. System.out.println("--------您的输入有误----------");
  263. }
  264. }
  265. type = true;
  266. } else {
  267. System.out.println("输入有误,请按照序号重新输入");
  268. type = false;
  269. }
  270. type = false;// 标识符更改为false,退出系统
  271. }
  272. IsStoreLogOut(store);
  273. }
  274. //以商店的身份登录,商店找顾客购买鲜花
  275. private static void storeBuy(FlowerStore store) {
  276. FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
  277. Flower flower = null;
  278. List<Flower> flowerList = flowerStore.getFlowerSelling();
  279. System.out.println("-------以下是顾客正在出售的鲜花-------");
  280. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  281. for (int i = 0; i < flowerList.size(); i++) {
  282. flower = flowerList.get(i);
  283. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  284. }
  285. System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
  286. int num = input.nextInt();
  287. flower = flowerList.get(num - 1);
  288. //System.out.println("选中的鲜花信息"+flower.getName()+"id"+flower.getId());
  289. flowerStore.buy(flower, store);
  290. IsStoreLogOut(store);
  291. }
  292. //顾客登录
  293. private static FlowerOwner ownerLogin() {
  294. System.out.println("请先登录,请您输入姓名:");
  295. String ownerName = input.nextLine().trim();
  296. System.out.println("请您输入密码:");
  297. String ownerPassword = input.nextLine().trim();
  298. FlowerOwnerService flowerOwner = new FlowerOwnerServiceImpl();
  299. FlowerOwner Owner = flowerOwner.login(ownerName, ownerPassword);
  300. boolean reg = true;
  301. while (reg) {
  302. if (null == Owner) {
  303. System.out.println("登录失败,请确认您的用户名和密码后重新输入");
  304. System.out.println("请先登录,请您输入姓名:");
  305. ownerName = input.nextLine().trim();
  306. System.out.println("请您输入密码:");
  307. ownerPassword = input.nextLine().trim();
  308. Owner = flowerOwner.login(ownerName, ownerPassword);
  309. reg = true;
  310. } else {
  311. reg = false;
  312. System.out.println("登录成功,您可以购买和卖出鲜花,如果您想购买鲜花请输入1,如果想卖出鲜花请输入2");
  313. System.out.println("1:购买鲜花");
  314. System.out.println("2:卖出鲜花");
  315. boolean type = true;
  316. //----------------------
  317. FlowerCirculate(type, Owner);
  318. }
  319. }
  320. return Owner;
  321. }
  322. private static boolean FlowerCirculate(boolean type, FlowerOwner owner) {
  323. while (type) {
  324. int num = input.nextInt();
  325. if (1 == num) {
  326. ownerBuy(owner);
  327. type = false;
  328. } else if (2 == num) {
  329. ownerSell(owner);
  330. type = false;
  331. } else {
  332. System.out.println("输入有误,请重新输入");
  333. type = true;
  334. }
  335. }
  336. return type;
  337. }
  338. //顾客向商店卖鲜花
  339. private static void ownerSell(FlowerOwner flowerowner) {
  340. }
  341. //顾客找鲜花商店买鲜花
  342. private static void ownerBuy(FlowerOwner flowerowner) {
  343. System.out.println("-------请输入选择要购买范围:只需要按照下文要求输入选择项的序号即可--------");
  344. System.out.println("1:购买库存鲜花");
  345. System.out.println("2:购买新培育鲜花");
  346. FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
  347. FlowerOwnerService flowerOwner = new FlowerOwnerServiceImpl();
  348. Flower flower = null;
  349. int num = input.nextInt();
  350. List<Flower> flowerList = null;
  351. // num为1时购买库存鲜花
  352. boolean type = true;
  353. while (type) {
  354. if (num == 1) {
  355. System.out.println(num + "库存鲜花: -------以下是库存鲜花-------");
  356. //TODO-1 通过服务层接口查询库存鲜花
  357. flowerList = flowerStore.getFlowersInstock(0);
  358. //显示所有库存鲜花
  359. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  360. for (int i = 0; i < flowerList.size(); i++) {
  361. flower = flowerList.get(i);
  362. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  363. }
  364. System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
  365. num = input.nextInt();
  366. flower = flowerList.get(num - 1);
  367. flower.setOwnerId(flowerowner.getId());
  368. flowerOwner.FlowerOwnerbuy(flower);
  369. type = false;
  370. } else if (num == 2) { // num为2时购买新培育鲜花
  371. System.out.println(num + "培育鲜花: -------以下是库存鲜花-------");
  372. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  373. //通过服务层接口查询所有培育鲜花
  374. flowerList = flowerStore.getFlowersBread();
  375. //显示所有培育鲜花
  376. for (int i = 0; i < flowerList.size(); i++) {
  377. flower = flowerList.get(i);
  378. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  379. }
  380. System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
  381. String count = input.next();
  382. if (count.matches("[0-9]*")) {
  383. num = Integer.parseInt(count);
  384. flower = flowerList.get(num - 1);
  385. flower.setOwnerId(flowerowner.getId());
  386. flowerOwner.FlowerOwnerbuy(flower);
  387. }
  388. type = false;
  389. } else {
  390. System.out.println("您的输入有误,请按照上诉提示输入");
  391. type = true;
  392. }
  393. }
  394. boolean isLogOut = true;
  395. System.out.println("您是否要继续进行其它操作,若是请输入Y,否则输入任意字母退出系统");
  396. String flag = input.next();
  397. if (flag.equals("Y")) {
  398. System.out.println("您可以购买和卖出鲜花,如果您想购买鲜花请输入1,如果想卖出鲜花请输入2");
  399. System.out.println("1:购买鲜花");
  400. System.out.println("2:卖出鲜花");
  401. //循环
  402. FlowerCirculate(isLogOut, flowerowner);
  403. } else {
  404. System.out.println("您已成功退出系统");
  405. }
  406. }
  407. }

 第二关:

文件:src/step6/service/impl/FlowerStoreServiceImpl.java

  1. package step6.service.impl;
  2. import step6.dao.AccountDao;
  3. import step6.dao.FlowerDao;
  4. import step6.dao.FlowerOwnerDao;
  5. import step6.dao.FlowerStoreDao;
  6. import step6.dao.impl.AccountDaoImpl;
  7. import step6.dao.impl.FlowerDaoImpl;
  8. import step6.dao.impl.FlowerOwnerDaoImpl;
  9. import step6.dao.impl.FlowerStoreDaoImpl;
  10. import step6.entity.Account;
  11. import step6.entity.Flower;
  12. import step6.entity.FlowerOwner;
  13. import step6.entity.FlowerStore;
  14. import step6.service.FlowerStoreService;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Date;
  17. import java.util.List;
  18. import java.util.Scanner;
  19. /**
  20. * 鲜花商店实现类
  21. */
  22. public class FlowerStoreServiceImpl implements FlowerStoreService {
  23. /**
  24. * 查询鲜花商店账目 其中的1代表鲜花商店卖给顾客,2代表顾客卖给商店,代表鲜花商店的各种交易
  25. */
  26. @Override
  27. public List<Account> account(long storeId) {
  28. String sql = "select * from account where deal_type=? and seller_id=? union select * from account where deal_type=? and buyer_id=?";
  29. String[] param = {"1", String.valueOf(storeId), "2", String.valueOf(storeId)};
  30. AccountDao accountDao = new AccountDaoImpl();
  31. List<Account> list = accountDao.getFlowerStoreAccount(sql, param);
  32. return list;
  33. }
  34. /**
  35. * 修改鲜花商店资金信息
  36. */
  37. @Override
  38. public int modifyAccount(Flower flower, FlowerOwner owner) {
  39. String insertsql = "insert into account(deal_type,flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?,?)";
  40. String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  41. Object[] accountParam = {1, flower.getId(), flower.getStoreId(), owner.getId(), flower.getPrice(), date};
  42. AccountDao accountDao = new AccountDaoImpl();
  43. int insertAccount = accountDao.updateAccount(insertsql, accountParam);
  44. return insertAccount;
  45. }
  46. /**
  47. * 培育鲜花
  48. */
  49. @Override
  50. public Flower bread(String flowerType, FlowerStore store) {
  51. Scanner input = new Scanner(System.in);
  52. System.out.println("请输入您期望培育的鲜花名字:");
  53. String flowerName = input.nextLine();
  54. System.out.println("请输入您期望的鲜花价格:");
  55. String flowerprice = input.nextLine();
  56. String storeId = String.valueOf(store.getId());
  57. String[] flowerParam = {flowerName, flowerType, storeId, flowerprice};
  58. FlowerFactoryImpl flowerFactory = new FlowerFactoryImpl();
  59. Flower flower = flowerFactory.breadNewFlower(flowerParam);
  60. String sql = "insert into flower(`name`,typeName,store_id,price) values(?,?,?,?)";
  61. Object[] param = {flower.getName(), flower.getTypeName(), flower.getStoreId(), flower.getPrice()};
  62. FlowerDao flowerDao = new FlowerDaoImpl();
  63. int count = flowerDao.updateFlower(sql, param);
  64. if (count > 0) {
  65. System.out.println(store.getName() + "成功培育了一种" + flower.getTypeName() + "鲜花");
  66. }
  67. return flower;
  68. }
  69. /**
  70. * 鲜花商店购买鲜花
  71. */
  72. @Override
  73. public void buy(Flower flower, FlowerStore oldstore) {
  74. String sql = "select * from flowerStore where id=?";
  75. String paramStore[] = {String.valueOf(oldstore.getId())};
  76. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  77. FlowerStore store = storeDao.getFlowerStore(sql, paramStore);
  78. FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
  79. sql = "select * from flowerOwner where id = ?";
  80. String paramOwner[] = {String.valueOf(flower.getOwnerId())};
  81. FlowerOwner owner = ownerDao.selectOwner(sql, paramOwner);
  82. int updateFlower = modifyFlower(flower, null, store);// 更新鲜花信息
  83. if (updateFlower > 0) {// 更新顾客的信息
  84. int updateOwner = modifyOwner(owner, flower, 1);
  85. if (updateOwner > 0) {// 更新鲜花商店的信息
  86. int updateStore = modifyStore(flower, 1, store);
  87. if (updateStore > 0) {// 更新鲜花商店信息
  88. int insertAccount = modifyAccount(flower, owner);
  89. if (insertAccount > 0) {
  90. System.out.println(store.getName() + "已成功购买鲜花:" + flower.getName() + "价格为" + flower.getPrice());
  91. }
  92. }
  93. }
  94. } else {
  95. System.out.println("修改鲜花信息失败");
  96. }
  97. }
  98. /**
  99. * 商店卖鲜花
  100. */
  101. @Override
  102. public void sell(Flower flower) {
  103. FlowerDaoImpl FlowerDao = new FlowerDaoImpl();
  104. FlowerStoreDaoImpl storeDao = new FlowerStoreDaoImpl();
  105. FlowerOwnerDaoImpl ownerDao = new FlowerOwnerDaoImpl();
  106. FlowerStoreService FlowerStore = new FlowerStoreServiceImpl();
  107. String updatesql = "update Flower set store_id = null ,owner_id=? where id=?";
  108. Object[] param = {flower.getOwnerId(), flower.getId()};
  109. int updateFlower = FlowerDao.executeSQL(updatesql, param);// 更新鲜花信息
  110. if (updateFlower > 0) {// 更新顾客的信息
  111. String ownersql = "select * from Flowerowner where id=?";
  112. String ownerparam[] = {String.valueOf(flower.getOwnerId())};
  113. FlowerOwner owner = ownerDao.selectOwner(ownersql, ownerparam);
  114. String updateOwnerSql = "update Flowerowner set money=? where id=?";
  115. double count = flower.getPrice();
  116. Object[] ownerParam = {(owner.getMoney() - count), owner.getId()};
  117. int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);
  118. if (updateOwner > 0) {// 更新鲜花商店的信息
  119. FlowerStore store = FlowerStore.getFlowerStore(flower.getStoreId());
  120. String updateStore = "update Flowerstore set balance=? where id=?";
  121. Object[] storeParam = {(store.getBalance() + count),
  122. store.getId()};
  123. int updatestore = storeDao.executeSQL(updateStore, storeParam);
  124. if (updatestore > 0) {// 更新鲜花商店账户的信息
  125. String insertsql = "insert into account(deal_type,Flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?, ?)";
  126. String date = new SimpleDateFormat("yyyy-MM-dd")
  127. .format(new Date());
  128. Object[] accountParam = {1, flower.getId(), owner.getId(), flower.getStoreId(), count, date};
  129. AccountDao accountDao = new AccountDaoImpl();
  130. int insertAccount = accountDao.updateAccount(insertsql, accountParam);
  131. if (insertAccount > 0) {
  132. System.out.println(store.getName() + "已成功卖出价格为" + flower.getPrice() + "的鲜花" + flower.getName());
  133. }
  134. }
  135. }
  136. }
  137. }
  138. /**
  139. * 查询出所有库存鲜花
  140. */
  141. @Override
  142. public List<Flower> getFlowersInstock(long storeId) {
  143. FlowerDao flowerDao = new FlowerDaoImpl();
  144. String[] param = {String.valueOf(storeId)};
  145. String sql = "";
  146. // 当storeId不为 0 时,要执行查询指定商店库存鲜花
  147. if (storeId != 0) {
  148. //填写查询指定商店库存鲜花SQL语句
  149. sql = "select * from flower where owner_id is null and store_id=?";
  150. }
  151. // 当storeId为0时,要执行查询所有商店的库存鲜花
  152. if (0 == storeId) {
  153. //填写查询所有商店库存鲜花SQL语句
  154. sql = "select * from flower where owner_id is null";
  155. param = null;
  156. }
  157. List<Flower> flowerList = flowerDao.selectFlower(sql, param);
  158. return flowerList;
  159. }
  160. /**
  161. * 查询出所有新培育的鲜花,
  162. */
  163. @Override
  164. public List<Flower> getFlowersBread() {
  165. FlowerDao flowerDao = new FlowerDaoImpl();
  166. String sql = "SELECT * FROM flower WHERE owner_id IS NULL AND typeName NOT IN (?,?,?) AND store_id IS Not NULL";
  167. String[] flowerParam = {"香槟玫瑰", "白玫瑰", "粉玫瑰"};
  168. //TODO-2 通过Dao层访问新培育的鲜花,并将结果返回
  169. return flowerDao.selectFlower(sql, flowerParam);
  170. }
  171. /**
  172. * 根据顾客信息修改鲜花信息 根据FlowerOwnerEntity和FlowerStoreEntity的值判断是顾客买鲜花或者鲜花商店买鲜花
  173. * FlowerOwnerEntity=null是鲜花商店买鲜花,FlowerStoreEntity=null是顾客买鲜花
  174. */
  175. @Override
  176. public int modifyFlower(Flower flower, FlowerOwner flowerOwner, FlowerStore store) {
  177. String updatesql = null;
  178. long id = 0;
  179. if (null == store) {
  180. updatesql = "update flower set owner_id=? where id=?";
  181. id = flowerOwner.getId();
  182. } else if (null == flowerOwner) {
  183. updatesql = "update flower set store_id=?,owner_id=null where id=?";
  184. id = store.getId();
  185. }
  186. Object[] param = {id, flower.getId()};
  187. FlowerDaoImpl flowerDao = new FlowerDaoImpl();
  188. int updateFlower = flowerDao.executeSQL(updatesql, param);// 更新鲜花信息
  189. return updateFlower;
  190. }
  191. /**
  192. * 修改顾客信息 type=0是顾客买鲜花,type=1是鲜花商店买鲜花,价钱的变动
  193. */
  194. @Override
  195. public int modifyOwner(FlowerOwner owner, Flower flower, int type) {
  196. FlowerOwnerDaoImpl ownerDao = new FlowerOwnerDaoImpl();
  197. String updateOwnerSql = "update flowerowner set money=? where id=?";
  198. double count = 0;
  199. if (0 == type) {
  200. count = (owner.getMoney() - flower.getPrice());
  201. }
  202. if (1 == type) {
  203. count = (owner.getMoney() + flower.getPrice());
  204. }
  205. Object[] ownerParam = {count, owner.getId()};
  206. int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);
  207. return updateOwner;
  208. }
  209. /**
  210. * 修改鲜花商店信息 type=0是顾客买鲜花,type=1是鲜花商店买鲜花
  211. */
  212. @Override
  213. public int modifyStore(Flower flower, int type, FlowerStore oldstore) {
  214. FlowerStoreService store = new FlowerStoreServiceImpl();
  215. FlowerStore flowerStore = store.getFlowerStore(oldstore.getId());
  216. String updateStore = "update flowerstore set balance=? where id=?";
  217. double count = 0;
  218. if (0 == type) {
  219. count = (flowerStore.getBalance() + flower.getPrice());
  220. }
  221. if (1 == type) {
  222. count = (flowerStore.getBalance() - flower.getPrice());
  223. }
  224. Object[] storeParam = {count, oldstore.getId()};
  225. FlowerStoreDaoImpl storeDao = new FlowerStoreDaoImpl();
  226. int updatestore = storeDao.executeSQL(updateStore, storeParam);
  227. return updatestore;
  228. }
  229. /**
  230. * 鲜花商店登录
  231. *
  232. * @param storeName
  233. * @param storePassword
  234. * @return
  235. */
  236. @Override
  237. public FlowerStore login(String storeName, String storePassword) {
  238. FlowerStore flowerStore;
  239. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  240. String sql = "select * from flowerstore where name=? and password=?";
  241. String[] param = {storeName, storePassword};
  242. flowerStore = storeDao.getFlowerStore(sql, param);
  243. return flowerStore;
  244. }
  245. /**
  246. * 查询出所有鲜花商店正在出售的鲜花
  247. */
  248. @Override
  249. public List<Flower> getFlowerSelling() {
  250. FlowerDao flowerDao = new FlowerDaoImpl();
  251. String sql = "select * from flower where owner_id is not null";
  252. String[] flowerParam = null;
  253. List<Flower> flowerList = flowerDao.selectFlower(sql, flowerParam);
  254. return flowerList;
  255. }
  256. /**
  257. * 根据鲜花商店标识符查询鲜花信息
  258. */
  259. @Override
  260. public FlowerStore getFlowerStore(long id) {
  261. String sql = "select * from flowerstore where id=" + id;
  262. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  263. FlowerStore flowerStore = storeDao.getFlowerStore(sql, null);
  264. return flowerStore;
  265. }
  266. }

文件:src/step6/test/FlowerStoreApp.java

  1. package step6.test;
  2. import dbtest.InitDB;
  3. import step6.dao.*;
  4. import step6.dao.impl.*;
  5. import step6.entity.*;
  6. import step6.service.*;
  7. import step6.service.impl.*;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Scanner;
  11. public class FlowerStoreApp {
  12. private static Scanner input = new Scanner(System.in);
  13. public static void main(String[] args) {
  14. //1-初始化 NySQL 数据库 ownerBuy TODO
  15. InitDB db = new InitDB();
  16. //2-启动鲜花商店系统
  17. startFlowerShop();
  18. }
  19. private static void startFlowerShop() {
  20. System.out.println("----------------------鲜花商店启动-----------------");
  21. System.out.println("鲜花信息");
  22. System.out.println("****************************************************");
  23. FlowerDao flowerDao = new FlowerDaoImpl();
  24. List<Flower> flowerList = flowerDao.getAllFlower();
  25. System.out.println("序号\t" + "鲜花名称\t" + "鲜花品种\t" + "鲜花售价\t" + "");
  26. for (int i = 0; i < flowerList.size(); i++) {
  27. Flower flower = flowerList.get(i);
  28. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  29. }
  30. System.out.println("****************************************************");
  31. System.out.print("\n");
  32. System.out.println("顾客信息");
  33. FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
  34. List<FlowerOwner> ownerList = ownerDao.getAllOwner();
  35. System.out.println("****************************************************");
  36. System.out.println("序号\t" + "顾客姓名\t");
  37. for (int i = 0; i < ownerList.size(); i++) {
  38. FlowerOwner owner = ownerList.get(i);
  39. System.out.println((i + 1) + "\t" + owner.getName() + "\t");
  40. }
  41. System.out.println("****************************************************");
  42. System.out.print("\n");
  43. System.out.println("鲜花商店信息");
  44. System.out.println("****************************************************");
  45. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  46. List<FlowerStore> storeList = storeDao.getAllStore();
  47. System.out.println("序号\t" + "鲜花商店名称\t");
  48. for (int i = 0; i < storeList.size(); i++) {
  49. FlowerStore store = storeList.get(i);
  50. System.out.println((i + 1) + "\t" + store.getName() + "\t");
  51. }
  52. System.out.println("****************************************************");
  53. System.out.print("\n");
  54. //系统登录
  55. System.out.println("请选择输入登录模式,输入1为顾客登录,输入2为鲜花商店登录");
  56. boolean type = true;
  57. String num;
  58. while (type) {
  59. num = input.nextLine();
  60. if ("1".equals(num)) {
  61. ownerLogin();
  62. type = false;
  63. } else if ("2".equals(num)) {
  64. storeLogin();
  65. type = false;
  66. } else {
  67. System.out.println("输入有误,请按照指定规则输入");
  68. System.out.println("请选择登录模式,输入1为顾客登录,输入2为鲜花商店登录");
  69. type = true;
  70. }
  71. }
  72. }
  73. //鲜花商店登录
  74. private static FlowerStore storeLogin() {
  75. FlowerStoreService fsService = new FlowerStoreServiceImpl();
  76. FlowerStore flowerStore = null;
  77. //TODO-1 鲜花商店登录代码实现
  78. boolean type = true;
  79. while (type) {
  80. System.out.println("请先登录,请输入鲜花商店名字:");
  81. String storeName = input.nextLine().trim();
  82. System.out.println("请输入鲜花商店的密码:");
  83. String storePassword = input.nextLine().trim();
  84. flowerStore = fsService.login(storeName, storePassword);
  85. if (null != flowerStore) {
  86. System.out.println("-------恭喜成功登录-------");
  87. System.out.println("-------鲜花商店的基本信息:-------");
  88. System.out.println("名字:" + flowerStore.getName());
  89. System.out.println("资金:" + flowerStore.getBalance());
  90. type = false;
  91. } else {
  92. System.out.println("登录失败,请确认您的用户名和密码是否正确,重新登录");
  93. type = true;
  94. }
  95. }
  96. System.out.println("您已登录成功,可以进行如下操作");
  97. StoreChoose(flowerStore);
  98. return flowerStore;
  99. }
  100. //创建鲜花商店
  101. private static FlowerStore createFlowerStore(FlowerStore store) {
  102. FlowerStoreFactory storeFactory = new FlowerStoreFactoryImpl();
  103. storeFactory.createFlowerStore();
  104. IsStoreLogOut(store);
  105. return store;//返回的是鲜花商店创建者
  106. }
  107. private static void IsStoreLogOut(FlowerStore store) {
  108. System.out.println("您是否继续其它操作若是请输入y,退出请按任意键");
  109. String code = input.next();
  110. if (code.equals("y")) {
  111. StoreChoose(store);
  112. } else {
  113. System.out.println("您已成功退出系统");
  114. }
  115. }
  116. private static void StoreChoose(FlowerStore store) {
  117. System.out.println("1:购买鲜花");
  118. System.out.println("2:卖出鲜花");
  119. System.out.println("3:培育鲜花");
  120. System.out.println("4:查询待售鲜花");
  121. System.out.println("5:查看商店结余");
  122. System.out.println("6:查看商店账目");
  123. System.out.println("7:开鲜花商店");
  124. System.out.println("请根据需要执行的操作,选择序号输入,退出请输入0");
  125. boolean type = true;
  126. while (type) {
  127. int num = input.nextInt();
  128. switch (num) {
  129. case 0:
  130. System.out.println("退出成功");
  131. type = false;
  132. break;
  133. case 1:
  134. storeBuy(store);
  135. type = false;
  136. break;
  137. case 2:
  138. storeSell(store);
  139. type = false;
  140. break;
  141. case 3:
  142. storeBread(store);//传入登录的商店
  143. type = false;
  144. break;
  145. case 4:
  146. queryFlowerStock(store.getId());
  147. type = false;
  148. break;
  149. case 5:
  150. queryStoreBalance(store);
  151. type = false;
  152. break;
  153. case 6:
  154. getAccount(store.getId());
  155. type = false;
  156. break;
  157. case 7:
  158. createFlowerStore(store);
  159. type = false;
  160. break;
  161. default:
  162. System.out.println("输入有误,请重新输入");
  163. type = true;
  164. break;
  165. }
  166. }
  167. }
  168. //获得鲜花商店的资金1代表商店顾客,2顾客卖给商店,3代表顾客之间的交易
  169. private static FlowerStore getAccount(long id) {
  170. FlowerStoreService storeService = new FlowerStoreServiceImpl();
  171. FlowerStore store = storeService.getFlowerStore(id);
  172. List<Account> list = storeService.account(id);
  173. for (int i = 0; i < list.size(); i++) {
  174. Account account = list.get(i);
  175. String type = null;
  176. if (1 == account.getDealType()) {
  177. type = "商店卖给顾客";
  178. } else if (2 == account.getDealType()) {
  179. type = "顾客卖给商店";
  180. } else {
  181. type = "顾客之间交易";
  182. }
  183. System.out.println("第" + (i + 1) + "笔交易,交易类型为:" + type + ",交易金额是:"
  184. + account.getPrice());
  185. }
  186. IsStoreLogOut(store);
  187. return store;
  188. }
  189. //查询商店余额
  190. private static FlowerStore queryStoreBalance(FlowerStore store) {
  191. double balance = store.getBalance();
  192. System.out.println(store.getName() + "鲜花商店的结余为:" + balance);
  193. IsStoreLogOut(store);
  194. return store;
  195. }
  196. //查询待售鲜花
  197. private static FlowerStore queryFlowerStock(long storeId) {
  198. FlowerStoreService flowerStoreService = new FlowerStoreServiceImpl();
  199. FlowerStore store = flowerStoreService.getFlowerStore(storeId);
  200. Flower flower = null;
  201. List<Flower> flowerList = flowerStoreService.getFlowersInstock(storeId);
  202. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  203. for (int i = 0; i < flowerList.size(); i++) {
  204. flower = flowerList.get(i);
  205. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  206. }
  207. IsStoreLogOut(store);
  208. return store;
  209. }
  210. //鲜花商店培育鲜花
  211. private static FlowerStore storeBread(FlowerStore store) {
  212. FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
  213. System.out.println("请输入要培育鲜花的品种(例如:玫瑰))");
  214. String flowerType = input.next();
  215. flowerStore.bread(flowerType, store);
  216. IsStoreLogOut(store);
  217. return store;
  218. }
  219. //以商店的身份登录,商店向顾客出售鲜花
  220. private static void storeSell(FlowerStore store) {
  221. FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
  222. Flower flower = null;
  223. List<Flower> flowerList = flowerStore.getFlowersInstock(store.getId());
  224. System.out.println("-------以下是鲜花商店正在出售的鲜花-------");
  225. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  226. for (int i = 0; i < flowerList.size(); i++) {
  227. flower = flowerList.get(i);
  228. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  229. }
  230. System.out.println("---------请选择要购买的鲜花序号--------");
  231. boolean type = true;
  232. while (type) {
  233. int num = input.nextInt();
  234. if ((num - 1) < flowerList.size() && (num - 1) >= 0) {
  235. flower = flowerList.get(num - 1);
  236. System.out.println("------要卖出的鲜花信息如下------");
  237. System.out.println("鲜花名称为:" + flower.getName() + " 鲜花类别是:" + flower.getTypeName() + "鲜花价格是:" + flower.getPrice());
  238. System.out.println("请确认是否卖出,y代表卖出,n代表不卖");
  239. String code = input.next();
  240. if (null != code) {
  241. if ("y".equals(code)) {
  242. System.out.println("------下面是现有顾客买家,请选择您要卖给买家序号------");
  243. List<FlowerOwner> ownerList = new ArrayList<FlowerOwner>();
  244. FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
  245. ownerList = ownerDao.getAllOwner();
  246. FlowerOwner flowerOwner = null;
  247. System.out.println("序号\t" + "顾客姓名\t");
  248. for (int i = 0; i < ownerList.size(); i++) {
  249. flowerOwner = ownerList.get(i);
  250. System.out.println((i + 1) + "\t" + flowerOwner.getName() + "\t");
  251. }
  252. num = input.nextInt();
  253. if ((num - 1) < ownerList.size() && (num - 1) >= 0) {
  254. flowerOwner = ownerList.get(num - 1);
  255. }
  256. flower.setOwnerId(flowerOwner.getId());
  257. flowerStore.sell(flower);
  258. } else if ("n".equals(code)) {
  259. System.out
  260. .println("--------您选择放弃本次交易,希望您再次光顾----------");
  261. } else {
  262. System.out.println("--------您的输入有误----------");
  263. }
  264. }
  265. type = true;
  266. } else {
  267. System.out.println("输入有误,请按照序号重新输入");
  268. type = false;
  269. }
  270. type = false;// 标识符更改为false,退出系统
  271. }
  272. IsStoreLogOut(store);
  273. }
  274. //以商店的身份登录,商店找顾客购买鲜花
  275. private static void storeBuy(FlowerStore store) {
  276. FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
  277. Flower flower = null;
  278. List<Flower> flowerList = flowerStore.getFlowerSelling();
  279. System.out.println("-------以下是顾客正在出售的鲜花-------");
  280. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  281. for (int i = 0; i < flowerList.size(); i++) {
  282. flower = flowerList.get(i);
  283. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  284. }
  285. System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
  286. int num = input.nextInt();
  287. flower = flowerList.get(num - 1);
  288. //System.out.println("选中的鲜花信息"+flower.getName()+"id"+flower.getId());
  289. flowerStore.buy(flower, store);
  290. IsStoreLogOut(store);
  291. }
  292. //顾客登录
  293. private static FlowerOwner ownerLogin() {
  294. System.out.println("请先登录,请您输入姓名:");
  295. String ownerName = input.nextLine().trim();
  296. System.out.println("请您输入密码:");
  297. String ownerPassword = input.nextLine().trim();
  298. FlowerOwnerService flowerOwner = new FlowerOwnerServiceImpl();
  299. FlowerOwner Owner = flowerOwner.login(ownerName, ownerPassword);
  300. boolean reg = true;
  301. while (reg) {
  302. if (null == Owner) {
  303. System.out.println("登录失败,请确认您的用户名和密码后重新输入");
  304. System.out.println("请先登录,请您输入姓名:");
  305. ownerName = input.nextLine().trim();
  306. System.out.println("请您输入密码:");
  307. ownerPassword = input.nextLine().trim();
  308. Owner = flowerOwner.login(ownerName, ownerPassword);
  309. reg = true;
  310. } else {
  311. reg = false;
  312. System.out.println("登录成功,您可以购买和卖出鲜花,如果您想购买鲜花请输入1,如果想卖出鲜花请输入2");
  313. System.out.println("1:购买鲜花");
  314. System.out.println("2:卖出鲜花");
  315. boolean type = true;
  316. //----------------------
  317. FlowerCirculate(type, Owner);
  318. }
  319. }
  320. return Owner;
  321. }
  322. private static boolean FlowerCirculate(boolean type, FlowerOwner owner) {
  323. while (type) {
  324. int num = input.nextInt();
  325. if (1 == num) {
  326. ownerBuy(owner);
  327. type = false;
  328. } else if (2 == num) {
  329. ownerSell(owner);
  330. type = false;
  331. } else {
  332. System.out.println("输入有误,请重新输入");
  333. type = true;
  334. }
  335. }
  336. return type;
  337. }
  338. //顾客向商店卖鲜花
  339. private static void ownerSell(FlowerOwner flowerowner) {
  340. }
  341. //顾客找鲜花商店买鲜花
  342. private static void ownerBuy(FlowerOwner flowerowner) {
  343. System.out.println("-------请输入选择要购买范围:只需要按照下文要求输入选择项的序号即可--------");
  344. System.out.println("1:购买库存鲜花");
  345. System.out.println("2:购买新培育鲜花");
  346. FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
  347. FlowerOwnerService flowerOwner = new FlowerOwnerServiceImpl();
  348. Flower flower = null;
  349. int num = input.nextInt();
  350. List<Flower> flowerList = null;
  351. // num为1时购买库存鲜花
  352. boolean type = true;
  353. while (type) {
  354. if (num == 1) {
  355. System.out.println(num + "库存鲜花: -------以下是库存鲜花-------");
  356. //通过服务层接口查询库存鲜花
  357. flowerList = flowerStore.getFlowersInstock(0);
  358. //显示所有库存鲜花
  359. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  360. for (int i = 0; i < flowerList.size(); i++) {
  361. flower = flowerList.get(i);
  362. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  363. }
  364. System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
  365. num = input.nextInt();
  366. flower = flowerList.get(num - 1);
  367. flower.setOwnerId(flowerowner.getId());
  368. flowerOwner.FlowerOwnerbuy(flower);
  369. type = false;
  370. } else if (num == 2) { // num为2时购买新培育鲜花
  371. System.out.println(num + "培育鲜花: -------以下是库存鲜花-------");
  372. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  373. //TODO-1 通过服务层接口查询所有培育鲜花 FlowerStoreServiceImpl public List<Flower> getFlowersBread()
  374. flowerList = null;
  375. flowerList = flowerStore.getFlowersBread();
  376. //显示所有培育鲜花
  377. for (int i = 0; i < flowerList.size(); i++) {
  378. flower = flowerList.get(i);
  379. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  380. }
  381. System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
  382. String count = input.next();
  383. if (count.matches("[0-9]*")) {
  384. num = Integer.parseInt(count);
  385. flower = flowerList.get(num - 1);
  386. flower.setOwnerId(flowerowner.getId());
  387. flowerOwner.FlowerOwnerbuy(flower);
  388. }
  389. type = false;
  390. } else {
  391. System.out.println("您的输入有误,请按照上诉提示输入");
  392. type = true;
  393. }
  394. }
  395. boolean isLogOut = true;
  396. System.out.println("您是否要继续进行其它操作,若是请输入Y,否则输入任意字母退出系统");
  397. String flag = input.next();
  398. if (flag.equals("Y")) {
  399. System.out.println("您可以购买和卖出鲜花,如果您想购买鲜花请输入1,如果想卖出鲜花请输入2");
  400. System.out.println("1:购买鲜花");
  401. System.out.println("2:卖出鲜花");
  402. //循环
  403. FlowerCirculate(isLogOut, flowerowner);
  404. } else {
  405. System.out.println("您已成功退出系统");
  406. }
  407. }
  408. }

  第三关

文件:src/step7/service/impl/FlowerOwnerServiceImpl.java

  1. package step7.service.impl;
  2. import step7.dao.AccountDao;
  3. import step7.dao.FlowerDao;
  4. import step7.dao.FlowerOwnerDao;
  5. import step7.dao.FlowerStoreDao;
  6. import step7.dao.impl.AccountDaoImpl;
  7. import step7.dao.impl.FlowerDaoImpl;
  8. import step7.dao.impl.FlowerOwnerDaoImpl;
  9. import step7.dao.impl.FlowerStoreDaoImpl;
  10. import step7.entity.Flower;
  11. import step7.entity.FlowerOwner;
  12. import step7.entity.FlowerStore;
  13. import step7.service.FlowerOwnerService;
  14. import step7.service.FlowerStoreService;
  15. import java.text.SimpleDateFormat;
  16. import java.util.ArrayList;
  17. import java.util.Date;
  18. import java.util.List;
  19. /**
  20. * @author 鲜花顾客实现类
  21. */
  22. public class FlowerOwnerServiceImpl implements FlowerOwnerService {
  23. /**
  24. * 顾客购买库存鲜花,根据用户控制台输入获得到的序号,来实际调用购买库存鲜花或者购买新培育的鲜花 sell
  25. */
  26. @Override
  27. public void sell(Flower flower) {
  28. FlowerDao flowerDao = new FlowerDaoImpl();
  29. FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
  30. String updatesql = "update flower set store_id=?,owner_id=NUll where id=?";
  31. Object[] param = {flower.getStoreId(), flower.getId()};
  32. int updateFlower = flowerDao.updateFlower(updatesql, param);// 更新鲜花信息
  33. if (updateFlower > 0) {// 更新顾客的信息
  34. String ownersql = "select * from flowerowner where id=?";
  35. String ownerparam[] = {String.valueOf(flower.getOwnerId())};
  36. FlowerOwner owner = ownerDao.selectOwner(ownersql, ownerparam);
  37. String updateOwnerSql = "update flowerowner set money=? where id=?";
  38. String[] ownerParam = {String.valueOf((owner.getMoney() + flower.getPrice())), String.valueOf(owner.getId())};
  39. int updateOwner = ownerDao.updateOwner(updateOwnerSql, ownerParam);
  40. if (updateOwner > 0) {// 更新鲜花商店的信息
  41. FlowerStoreService store = new FlowerStoreServiceImpl();
  42. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  43. FlowerStore flowerStore = store.getFlowerStore(flower.getStoreId());
  44. //TODO-4 在 Dao层更新鲜花商店的信息
  45. String sql = "update flowerstore set balance=? where id=?";
  46. Object[] ownerParam2 ={(flowerStore.getBalance()-flower.getPrice()),flowerStore.getId()};
  47. int updatestore = storeDao.updateStore(sql, ownerParam2);
  48. if (updatestore > 0) {// 更新鲜花商店台帐信息
  49. String insertsql = "insert into account(deal_type,flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?, ?)";
  50. String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  51. Object[] accountParam = {2, flower.getId(), owner.getId(), flower.getStoreId(), flower.getPrice(), date};
  52. //TODO-5 在 Dao层在 account表中插入数据
  53. AccountDaoImpl ac = new AccountDaoImpl();
  54. int insertAccount = ac.updateAccount(insertsql,accountParam);
  55. if (insertAccount > 0) {
  56. System.out.println("您已成功卖出鲜花:" + flower.getName() + "获得收入" + flower.getPrice());
  57. }
  58. }
  59. }
  60. }
  61. }
  62. @Override
  63. public void FlowerOwnerbuy(Flower flower) {
  64. String sql = "select * from flowerowner where id=?";
  65. String param[] = {String.valueOf(flower.getOwnerId())};
  66. FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
  67. FlowerOwner owner = ownerDao.selectOwner(sql, param);
  68. String sql1 = "select * from flowerStore where id=?";
  69. String param1[] = {String.valueOf(flower.getStoreId())};
  70. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  71. FlowerStore store = storeDao.getFlowerStore(sql, param1);
  72. FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
  73. int updateFlower = flowerStore.modifyFlower(flower, owner, null);// 更新鲜花信息
  74. if (updateFlower > 0) {// 更新顾客的信息
  75. int updateOwner = flowerStore.modifyOwner(owner, flower, 0);
  76. if (updateOwner > 0) {// 更新鲜花商店的信息
  77. int updateStore = flowerStore.modifyStore(flower, 0, store);
  78. if (updateStore > 0) {// 更新鲜花商店帐户信息
  79. int insertAccount = flowerStore.modifyAccount(flower, owner);
  80. if (insertAccount > 0) {
  81. System.out.println("您已成功购买价格为" + flower.getPrice() + "的" + flower.getName());
  82. }
  83. }
  84. }
  85. }
  86. }
  87. /**
  88. * 顾客登录
  89. */
  90. @Override
  91. public FlowerOwner login(String ownerName,String ownerPassword) {
  92. FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
  93. String sql = "select * from flowerowner where name=? and password=?";
  94. String[] param = {ownerName, ownerPassword};
  95. FlowerOwner owner = ownerDao.selectOwner(sql, param);
  96. return owner;
  97. }
  98. /**
  99. * 根据顾客标识符(id)获得到该顾客所有鲜花信息
  100. */
  101. @Override
  102. public List<Flower> getMyFlower(int ownerId) {
  103. List<Flower> flowerList = new ArrayList<Flower>();
  104. String sql = "select * from flower where owner_id=?";
  105. String[] param = {String.valueOf(ownerId)};
  106. FlowerDao petDao = new FlowerDaoImpl();
  107. flowerList = petDao.selectFlower(sql, param);
  108. return flowerList;
  109. }
  110. }

文件:src/step7/test/FlowerStoreApp.java

  1. package step7.test;
  2. import dbtest.InitDB;
  3. import step7.dao.*;
  4. import step7.dao.impl.*;
  5. import step7.entity.*;
  6. import step7.service.*;
  7. import step7.service.impl.*;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Scanner;
  11. public class FlowerStoreApp {
  12. private static Scanner input = new Scanner(System.in);
  13. public static void main(String[] args) {
  14. //1-初始化 NySQL 数据库
  15. InitDB db = new InitDB();
  16. //2-启动鲜花商店系统
  17. startFlowerShop();
  18. }
  19. private static void startFlowerShop() {
  20. System.out.println("----------------------鲜花商店启动-----------------");
  21. System.out.println("鲜花信息");
  22. System.out.println("****************************************************");
  23. FlowerDao flowerDao = new FlowerDaoImpl();
  24. List<Flower> flowerList = flowerDao.getAllFlower();
  25. System.out.println("序号\t" + "鲜花名称\t" + "鲜花品种\t" + "鲜花售价\t" + "");
  26. for (int i = 0; i < flowerList.size(); i++) {
  27. Flower flower = flowerList.get(i);
  28. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  29. }
  30. System.out.println("****************************************************");
  31. System.out.print("\n");
  32. System.out.println("顾客信息");
  33. FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
  34. List<FlowerOwner> ownerList = ownerDao.getAllOwner();
  35. System.out.println("****************************************************");
  36. System.out.println("序号\t" + "顾客姓名\t");
  37. for (int i = 0; i < ownerList.size(); i++) {
  38. FlowerOwner owner = ownerList.get(i);
  39. System.out.println((i + 1) + "\t" + owner.getName() + "\t");
  40. }
  41. System.out.println("****************************************************");
  42. System.out.print("\n");
  43. System.out.println("鲜花商店信息");
  44. System.out.println("****************************************************");
  45. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  46. List<FlowerStore> storeList = storeDao.getAllStore();
  47. System.out.println("序号\t" + "鲜花商店名称\t");
  48. for (int i = 0; i < storeList.size(); i++) {
  49. FlowerStore store = storeList.get(i);
  50. System.out.println((i + 1) + "\t" + store.getName() + "\t");
  51. }
  52. System.out.println("****************************************************");
  53. System.out.print("\n");
  54. //系统登录
  55. System.out.println("请选择输入登录模式,输入1为顾客登录,输入2为鲜花商店登录");
  56. boolean type = true;
  57. String num;
  58. while (type) {
  59. num = input.nextLine();
  60. if ("1".equals(num)) {
  61. ownerLogin();
  62. type = false;
  63. } else if ("2".equals(num)) {
  64. storeLogin();
  65. type = false;
  66. } else {
  67. System.out.println("输入有误,请按照指定规则输入");
  68. System.out.println("请选择登录模式,输入1为顾客登录,输入2为鲜花商店登录");
  69. type = true;
  70. }
  71. }
  72. }
  73. //鲜花商店登录
  74. private static FlowerStore storeLogin() {
  75. FlowerStoreService fsService = new FlowerStoreServiceImpl();
  76. FlowerStore flowerStore = null;
  77. //TODO-1 鲜花商店登录代码实现
  78. boolean type = true;
  79. while (type) {
  80. System.out.println("请先登录,请输入鲜花商店名字:");
  81. String storeName = input.nextLine().trim();
  82. System.out.println("请输入鲜花商店的密码:");
  83. String storePassword = input.nextLine().trim();
  84. flowerStore = fsService.login(storeName, storePassword);
  85. if (null != flowerStore) {
  86. System.out.println("-------恭喜成功登录-------");
  87. System.out.println("-------鲜花商店的基本信息:-------");
  88. System.out.println("名字:" + flowerStore.getName());
  89. System.out.println("资金:" + flowerStore.getBalance());
  90. type = false;
  91. } else {
  92. System.out.println("登录失败,请确认您的用户名和密码是否正确,重新登录");
  93. type = true;
  94. }
  95. }
  96. System.out.println("您已登录成功,可以进行如下操作");
  97. StoreChoose(flowerStore);
  98. return flowerStore;
  99. }
  100. //创建鲜花商店
  101. private static FlowerStore createFlowerStore(FlowerStore store) {
  102. FlowerStoreFactory storeFactory = new FlowerStoreFactoryImpl();
  103. storeFactory.createFlowerStore();
  104. IsStoreLogOut(store);
  105. return store;//返回的是鲜花商店创建者
  106. }
  107. private static void IsStoreLogOut(FlowerStore store) {
  108. System.out.println("您是否继续其它操作若是请输入y,退出请按任意键");
  109. String code = input.next();
  110. if (code.equals("y")) {
  111. StoreChoose(store);
  112. } else {
  113. System.out.println("您已成功退出系统");
  114. }
  115. }
  116. private static void StoreChoose(FlowerStore store) {
  117. System.out.println("1:购买鲜花");
  118. System.out.println("2:卖出鲜花");
  119. System.out.println("3:培育鲜花");
  120. System.out.println("4:查询待售鲜花");
  121. System.out.println("5:查看商店结余");
  122. System.out.println("6:查看商店账目");
  123. System.out.println("7:开鲜花商店");
  124. System.out.println("请根据需要执行的操作,选择序号输入,退出请输入0");
  125. boolean type = true;
  126. while (type) {
  127. int num = input.nextInt();
  128. switch (num) {
  129. case 0:
  130. System.out.println("退出成功");
  131. type = false;
  132. break;
  133. case 1:
  134. storeBuy(store);
  135. type = false;
  136. break;
  137. case 2:
  138. storeSell(store);
  139. type = false;
  140. break;
  141. case 3:
  142. storeBread(store);//传入登录的商店
  143. type = false;
  144. break;
  145. case 4:
  146. queryFlowerStock(store.getId());
  147. type = false;
  148. break;
  149. case 5:
  150. queryStoreBalance(store);
  151. type = false;
  152. break;
  153. case 6:
  154. getAccount(store.getId());
  155. type = false;
  156. break;
  157. case 7:
  158. createFlowerStore(store);
  159. type = false;
  160. break;
  161. default:
  162. System.out.println("输入有误,请重新输入");
  163. type = true;
  164. break;
  165. }
  166. }
  167. }
  168. //获得鲜花商店的资金1代表商店顾客,2顾客卖给商店,3代表顾客之间的交易
  169. private static FlowerStore getAccount(long id) {
  170. FlowerStoreService storeService = new FlowerStoreServiceImpl();
  171. FlowerStore store = storeService.getFlowerStore(id);
  172. List<Account> list = storeService.account(id);
  173. for (int i = 0; i < list.size(); i++) {
  174. Account account = list.get(i);
  175. String type = null;
  176. if (1 == account.getDealType()) {
  177. type = "商店卖给顾客";
  178. } else if (2 == account.getDealType()) {
  179. type = "顾客卖给商店";
  180. } else {
  181. type = "顾客之间交易";
  182. }
  183. System.out.println("第" + (i + 1) + "笔交易,交易类型为:" + type + ",交易金额是:"
  184. + account.getPrice());
  185. }
  186. IsStoreLogOut(store);
  187. return store;
  188. }
  189. //查询商店余额
  190. private static FlowerStore queryStoreBalance(FlowerStore store) {
  191. double balance = store.getBalance();
  192. System.out.println(store.getName() + "鲜花商店的结余为:" + balance);
  193. IsStoreLogOut(store);
  194. return store;
  195. }
  196. //查询待售鲜花
  197. private static FlowerStore queryFlowerStock(long storeId) {
  198. FlowerStoreService flowerStoreService = new FlowerStoreServiceImpl();
  199. FlowerStore store = flowerStoreService.getFlowerStore(storeId);
  200. Flower flower = null;
  201. List<Flower> flowerList = flowerStoreService.getFlowersInstock(storeId);
  202. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  203. for (int i = 0; i < flowerList.size(); i++) {
  204. flower = flowerList.get(i);
  205. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  206. }
  207. IsStoreLogOut(store);
  208. return store;
  209. }
  210. //鲜花商店培育鲜花
  211. private static FlowerStore storeBread(FlowerStore store) {
  212. FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
  213. System.out.println("请输入要培育鲜花的品种(例如:玫瑰))");
  214. String flowerType = input.next();
  215. flowerStore.bread(flowerType, store);
  216. IsStoreLogOut(store);
  217. return store;
  218. }
  219. //以商店的身份登录,商店向顾客出售鲜花
  220. private static void storeSell(FlowerStore store) {
  221. FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
  222. Flower flower = null;
  223. List<Flower> flowerList = flowerStore.getFlowersInstock(store.getId());
  224. System.out.println("-------以下是鲜花商店正在出售的鲜花-------");
  225. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  226. for (int i = 0; i < flowerList.size(); i++) {
  227. flower = flowerList.get(i);
  228. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  229. }
  230. System.out.println("---------请选择要购买的鲜花序号--------");
  231. boolean type = true;
  232. while (type) {
  233. int num = input.nextInt();
  234. if ((num - 1) < flowerList.size() && (num - 1) >= 0) {
  235. flower = flowerList.get(num - 1);
  236. System.out.println("------要卖出的鲜花信息如下------");
  237. System.out.println("鲜花名称为:" + flower.getName() + " 鲜花类别是:" + flower.getTypeName() + "鲜花价格是:" + flower.getPrice());
  238. System.out.println("请确认是否卖出,y代表卖出,n代表不卖");
  239. String code = input.next();
  240. if (null != code) {
  241. if ("y".equals(code)) {
  242. System.out.println("------下面是现有顾客买家,请选择您要卖给买家序号------");
  243. List<FlowerOwner> ownerList = new ArrayList<FlowerOwner>();
  244. FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
  245. ownerList = ownerDao.getAllOwner();
  246. FlowerOwner flowerOwner = null;
  247. System.out.println("序号\t" + "顾客姓名\t");
  248. for (int i = 0; i < ownerList.size(); i++) {
  249. flowerOwner = ownerList.get(i);
  250. System.out.println((i + 1) + "\t" + flowerOwner.getName() + "\t");
  251. }
  252. num = input.nextInt();
  253. if ((num - 1) < ownerList.size() && (num - 1) >= 0) {
  254. flowerOwner = ownerList.get(num - 1);
  255. }
  256. flower.setOwnerId(flowerOwner.getId());
  257. flowerStore.sell(flower);
  258. } else if ("n".equals(code)) {
  259. System.out
  260. .println("--------您选择放弃本次交易,希望您再次光顾----------");
  261. } else {
  262. System.out.println("--------您的输入有误----------");
  263. }
  264. }
  265. type = true;
  266. } else {
  267. System.out.println("输入有误,请按照序号重新输入");
  268. type = false;
  269. }
  270. type = false;// 标识符更改为false,退出系统
  271. }
  272. IsStoreLogOut(store);
  273. }
  274. //以商店的身份登录,商店找顾客购买鲜花
  275. private static void storeBuy(FlowerStore store) {
  276. FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
  277. Flower flower = null;
  278. List<Flower> flowerList = flowerStore.getFlowerSelling();
  279. System.out.println("-------以下是顾客正在出售的鲜花-------");
  280. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  281. for (int i = 0; i < flowerList.size(); i++) {
  282. flower = flowerList.get(i);
  283. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  284. }
  285. System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
  286. int num = input.nextInt();
  287. flower = flowerList.get(num - 1);
  288. //System.out.println("选中的鲜花信息"+flower.getName()+"id"+flower.getId());
  289. flowerStore.buy(flower, store);
  290. IsStoreLogOut(store);
  291. }
  292. //顾客登录
  293. private static FlowerOwner ownerLogin() {
  294. System.out.println("请先登录,请您输入姓名:");
  295. String ownerName = input.nextLine().trim();
  296. System.out.println("请您输入密码:");
  297. String ownerPassword = input.nextLine().trim();
  298. FlowerOwnerService flowerOwner = new FlowerOwnerServiceImpl();
  299. FlowerOwner Owner = flowerOwner.login(ownerName, ownerPassword);
  300. boolean reg = true;
  301. while (reg) {
  302. if (null == Owner) {
  303. System.out.println("登录失败,请确认您的用户名和密码后重新输入");
  304. System.out.println("请先登录,请您输入姓名:");
  305. ownerName = input.nextLine().trim();
  306. System.out.println("请您输入密码:");
  307. ownerPassword = input.nextLine().trim();
  308. Owner = flowerOwner.login(ownerName, ownerPassword);
  309. reg = true;
  310. } else {
  311. reg = false;
  312. System.out.println("登录成功,您可以购买和卖出鲜花,如果您想购买鲜花请输入1,如果想卖出鲜花请输入2");
  313. System.out.println("1:购买鲜花");
  314. System.out.println("2:卖出鲜花");
  315. boolean type = true;
  316. //----------------------
  317. FlowerCirculate(type, Owner);
  318. }
  319. }
  320. return Owner;
  321. }
  322. private static boolean FlowerCirculate(boolean type, FlowerOwner owner) {
  323. while (type) {
  324. int num = input.nextInt();
  325. if (1 == num) {
  326. ownerBuy(owner);
  327. type = false;
  328. } else if (2 == num) {
  329. ownerSell(owner);
  330. type = false;
  331. } else {
  332. System.out.println("输入有误,请重新输入");
  333. type = true;
  334. }
  335. }
  336. return type;
  337. }
  338. //顾客向商店卖鲜花
  339. private static void ownerSell(FlowerOwner flowerowner) {
  340. FlowerOwnerService owner = new FlowerOwnerServiceImpl();
  341. System.out.println("---------我的鲜花列表--------");
  342. List<Flower> flowerList = owner.getMyFlower(flowerowner.getId());
  343. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格");
  344. for (int i = 0; i < flowerList.size(); i++) {
  345. Flower flower = flowerList.get(i);
  346. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  347. }
  348. System.out.println("---------请选择要出售的鲜花序号--------");
  349. boolean type = true;
  350. while (type) {
  351. int num = input.nextInt();
  352. if ((num - 1) < flowerList.size() && (num - 1) >= 0) {
  353. Flower flower = flowerList.get(num - 1);
  354. System.out.println("------您要卖出的鲜花信息如下------");
  355. System.out.println("鲜花名字为:" + flower.getName() + "鲜花类别是:"
  356. + flower.getTypeName() + "鲜花价格为" + flower.getPrice());
  357. boolean again = true;
  358. while (again)//y,n的循环
  359. {
  360. System.out.println("请确认是否卖出,Y代表卖出,N代表不卖");
  361. String code = input.next();
  362. if (null != code) {
  363. if ("Y".equals(code)) {
  364. System.out.println("------下面是现有鲜花商店,请选择您要卖的商店序号------");
  365. List<FlowerStore> storeList = new ArrayList<FlowerStore>();
  366. //TODO-1 通过 Dao层查询所有的鲜花商店 您已成功卖出鲜花
  367. FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
  368. storeList = storeDao.getAllStore();
  369. FlowerStore flowerStore = null;
  370. System.out.println("序号\t" + "鲜花商店名字\t");
  371. for (int i = 0; i < storeList.size(); i++) {
  372. flowerStore = storeList.get(i);
  373. System.out.println((i + 1) + "\t" + flowerStore.getName() + "\t");
  374. }
  375. num = input.nextInt();
  376. if ((num - 1) < storeList.size() && (num - 1) >= 0) {
  377. flowerStore = storeList.get(num - 1);
  378. }
  379. //TODO-2 要销售的鲜花设置商店 id
  380. flower.setStoreId(num);
  381. //TODO-3 在 service 层顾客卖出该鲜花 Flower
  382. owner.sell(flower);
  383. again = false;//退出 y,n的循环
  384. } else if ("N".equals(code)) {
  385. System.out.println("--------您选择放弃本次交易,希望您再次光顾----------");
  386. again = false;
  387. } else {
  388. System.out.println("--------您的输入有误,请按照上诉要求输入----------");
  389. again = true;
  390. }
  391. }
  392. }//y,n循环结束
  393. } else {
  394. System.out.println("输入有误,请按照序号重新输入");
  395. type = true;//重新循环一次
  396. }
  397. type = false;
  398. }
  399. boolean isLogOut = true;
  400. System.out.println("您是否要继续进行其它操作,若是请输入Y,否则退出系统");
  401. String flag = input.next();
  402. if (flag.equals("Y")) {
  403. System.out.println("您可以购买和卖出鲜花,如果您想购买鲜花请输入1,如果想卖出鲜花请输入2");
  404. System.out.println("1:购买鲜花");
  405. System.out.println("2:卖出鲜花");
  406. //循环
  407. FlowerCirculate(isLogOut, flowerowner);
  408. } else {
  409. System.out.println("您已成功退出系统");
  410. }
  411. }
  412. //顾客找鲜花商店买鲜花
  413. private static void ownerBuy(FlowerOwner flowerowner) {
  414. System.out.println("-------请输入选择要购买范围:只需要按照下文要求输入选择项的序号即可--------");
  415. System.out.println("1:购买库存鲜花");
  416. System.out.println("2:购买新培育鲜花");
  417. FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
  418. FlowerOwnerService flowerOwner = new FlowerOwnerServiceImpl();
  419. Flower flower = null;
  420. int num = input.nextInt();
  421. List<Flower> flowerList = null;
  422. // num为1时购买库存鲜花
  423. boolean type = true;
  424. while (type) {
  425. if (num == 1) {
  426. System.out.println(num + "库存鲜花: -------以下是库存鲜花-------");
  427. //通过服务层接口查询库存鲜花
  428. flowerList = flowerStore.getFlowersInstock(0);
  429. //显示所有库存鲜花
  430. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  431. for (int i = 0; i < flowerList.size(); i++) {
  432. flower = flowerList.get(i);
  433. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  434. }
  435. System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
  436. num = input.nextInt();
  437. flower = flowerList.get(num - 1);
  438. flower.setOwnerId(flowerowner.getId());
  439. flowerOwner.FlowerOwnerbuy(flower);
  440. type = false;
  441. } else if (num == 2) { // num为2时购买新培育鲜花
  442. System.out.println(num + "培育鲜花: -------以下是库存鲜花-------");
  443. System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
  444. //TODO-1 通过服务层接口查询所有培育鲜花
  445. flowerList = flowerStore.getFlowersBread();
  446. //显示所有培育鲜花
  447. for (int i = 0; i < flowerList.size(); i++) {
  448. flower = flowerList.get(i);
  449. System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
  450. }
  451. System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
  452. String count = input.next();
  453. if (count.matches("[0-9]*")) {
  454. num = Integer.parseInt(count);
  455. flower = flowerList.get(num - 1);
  456. flower.setOwnerId(flowerowner.getId());
  457. flowerOwner.FlowerOwnerbuy(flower);
  458. }
  459. type = false;
  460. } else {
  461. System.out.println("您的输入有误,请按照上诉提示输入");
  462. type = true;
  463. }
  464. }
  465. boolean isLogOut = true;
  466. System.out.println("您是否要继续进行其它操作,若是请输入Y,否则输入任意字母退出系统");
  467. String flag = input.next();
  468. if (flag.equals("Y")) {
  469. System.out.println("您可以购买和卖出鲜花,如果您想购买鲜花请输入1,如果想卖出鲜花请输入2");
  470. System.out.println("1:购买鲜花");
  471. System.out.println("2:卖出鲜花");
  472. //循环
  473. FlowerCirculate(isLogOut, flowerowner);
  474. } else {
  475. System.out.println("您已成功退出系统");
  476. }
  477. }
  478. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/419789
推荐阅读
相关标签
  

闽ICP备14008679号