赞
踩
- package step1.entity;
-
- /**
- * 鲜花顾客实体类
- */
- public class FlowerOwner {
- private int id; //顾客标识符
- private String name; //顾客名称
- private String password; //顾客密码
- private double money; //顾客资金
-
- //TODO-2 补全getter/setter方法
-
- public FlowerOwner(){}
- public FlowerOwner(int id, String name, String password, double money) {
- this.id = id;
- this.name = name;
- this.password = password;
- this.money = money;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public double getMoney() {
- return money;
- }
-
- public void setMoney(double money) {
- this.money = money;
- }
- }
- package step1.entity;
-
- /**
- * 鲜花实体类
- */
- public class Flower {
- private long id; //鲜花标识符
- private String name; //鲜花名称
- private String typeName; //鲜花类别
- private int ownerId; //鲜花所属顾客标识符
- private long storeId; //鲜花所属鲜花商店标识符
- private double price; //鲜花价格
-
- //TODO-1 补齐getter/setter方法
-
- public Flower(){}
- public Flower(long id, String name, String typeName, int ownerId, long storeId, double price) {
- this.id = id;
- this.name = name;
- this.typeName = typeName;
- this.ownerId = ownerId;
- this.storeId = storeId;
- this.price = price;
- }
-
- public long getId() {
- return id;
- }
-
- public void setId(long id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getTypeName() {
- return typeName;
- }
-
- public void setTypeName(String typeName) {
- this.typeName = typeName;
- }
-
- public int getOwnerId() {
- return ownerId;
- }
-
- public void setOwnerId(int ownerId) {
- this.ownerId = ownerId;
- }
-
- public long getStoreId() {
- return storeId;
- }
-
- public void setStoreId(long storeId) {
- this.storeId = storeId;
- }
-
- public double getPrice() {
- return price;
- }
-
- public void setPrice(double price) {
- this.price = price;
- }
- }
- package step1.entity;
-
- /**
- * 鲜花商店实体类
- */
- public class FlowerStore {
- private long id; //鲜花商店id
- private String name; //鲜花商店名称
- private String password; //鲜花商店密码
- private double balance; //鲜花商店资金
-
- public FlowerStore(long id, String name, String password, double balance) {
- this.id = id;
- this.name = name;
- this.password = password;
- this.balance = balance;
- }
- public FlowerStore(){};
-
- public long getId() {
- return id;
- }
-
- public void setId(long id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- //TODO-3 补全name的getter/setter方法
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public double getBalance() {
- return balance;
- }
-
- public void setBalance(double balance) {
- this.balance = balance;
- }
- }
- package step2.dao.impl;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
-
- import step2.dao.BaseDao;
- import step2.dao.FlowerDao;
- import step2.entity.Flower;
-
- /**
- * 鲜花数据库操作类
- */
- public class FlowerDaoImpl extends BaseDao implements FlowerDao {
- private Connection conn = null; // 保存数据库连接
- private PreparedStatement pstmt = null; // 用于执行SQL语句
- private ResultSet rs = null; // 用户保存查询结果集
-
- /**
- * 查询所有鲜花
- */
- @Override
- public List<Flower> getAllFlower() {
- List<Flower> flowerList = new ArrayList<Flower>();
- try {
- conn = getConn();
- pstmt = conn.prepareStatement("SELECT * FROM flower");
- rs = pstmt.executeQuery();
- while(rs.next()){
- Flower f = new Flower();
- f.setId(rs.getLong(1));
- f.setName(rs.getString(2));
- f.setTypeName(rs.getString(3));
- f.setOwnerId(rs.getInt(4));
- f.setStoreId(rs.getLong(5));
- f.setPrice(rs.getLong(6));
- flowerList.add(f);
- }
- //TODO-1 获取所有鲜花
-
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } finally {
- super.closeAll(conn, pstmt, rs);
- }
- return flowerList;
- }
-
- /**
- * 根据参数查询相应符合条件鲜花
- */
- @Override
- public List<Flower> selectFlower(String sql, String[] param) {
- List<Flower> flowerList = new ArrayList<Flower>();
- try {
- conn = getConn(); // 得到数据库连接
- pstmt = conn.prepareStatement(sql); // 得到PreparedStatement对象
- if (param != null) {
- for (int i = 0; i < param.length; i++) {
- pstmt.setString(i + 1, param[i]); // 为预编译sql设置参数
- }
- }
- rs = pstmt.executeQuery(); // 执行SQL语句
- while (rs.next()) {
- Flower flower = new Flower();
- flower.setId(rs.getInt(1));
- flower.setName(rs.getString(2));
- flower.setTypeName(rs.getString(3));
- flower.setOwnerId(rs.getInt(4));
- flower.setStoreId(rs.getInt(5));
- flower.setPrice(rs.getDouble(6));
- flowerList.add(flower);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } finally {
- super.closeAll(conn, pstmt, rs);
- }
- return flowerList;
- }
-
- /**
- * 更新鲜花信息
- */
- @Override
- public int updateFlower(String sql, Object[] param) {
- int count = super.executeSQL(sql, param);
- return count;
- }
- }
- package step2.dao.impl;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
-
- import step2.dao.BaseDao;
- import step2.dao.FlowerStoreDao;
- import step2.entity.FlowerStore;
-
-
- public class FlowerStoreDaoImpl extends BaseDao implements FlowerStoreDao {
- private Connection conn = null; // 保存数据库连接
- private PreparedStatement pstmt = null; // 用于执行SQL语句
- private ResultSet rs = null; // 用户保存查询到的鲜花商店结果集
-
- @Override
- public List<FlowerStore> getAllStore() {
- List<FlowerStore> storeList = new ArrayList<FlowerStore>();
- try {
-
- //TODO-2 获取全部鲜花商店
- conn = getConn();
- pstmt = conn.prepareStatement("SELECT * FROM flowerstore");
- rs = pstmt.executeQuery();
- while(rs.next()){
- FlowerStore f = new FlowerStore();
- f.setId(rs.getLong(1));
- f.setName(rs.getString(2));
- f.setPassword(rs.getString(3));
- f.setBalance(rs.getDouble(4));
-
- storeList.add(f);
- }
-
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } finally {
- super.closeAll(conn, pstmt, rs);
- }
- return storeList;
- }
-
- @Override
- public FlowerStore getFlowerStore(String sql, String[] param) {
- FlowerStore flowerStore = null;
- try {
- //TODO-3 根据sql语句获取相应的鲜花商店
- conn = getConn();
- pstmt = conn.prepareStatement(sql);
- for(int i=0;i<param.length;i++){
- pstmt.setString(i+1,param[i]);
- }
- rs = pstmt.executeQuery();
- flowerStore = new FlowerStore();
-
-
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } finally {
- super.closeAll(conn, pstmt, rs);
- }
- return flowerStore;
- }
-
- @Override
- public int updateStore(String sql, Object[] param) {
- int i = 0;
- //TODO-4 修改商店信息
- try {
- conn = getConn();
- pstmt = conn.prepareStatement(sql);
- for(int j=0;i<param.length;i++){
- pstmt.setObject(i+1,param[i]);
- }
- i = pstmt.executeUpdate();
-
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } finally {
- super.closeAll(conn, pstmt, rs);
- }
- return i;
-
- }
- }
- package step3.service.impl;
-
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Scanner;
-
- import step3.dao.AccountDao;
- import step3.dao.FlowerDao;
- import step3.dao.FlowerOwnerDao;
- import step3.dao.FlowerStoreDao;
- import step3.dao.impl.AccountDaoImpl;
- import step3.dao.impl.FlowerDaoImpl;
- import step3.dao.impl.FlowerOwnerDaoImpl;
- import step3.dao.impl.FlowerStoreDaoImpl;
- import step3.entity.Flower;
- import step3.entity.FlowerOwner;
- import step3.entity.FlowerStore;
- import step3.service.FlowerOwnerService;
- import step3.service.FlowerStoreService;
-
- /**
- *
- * @author 鲜花顾客实现类
- *
- */
- public class FlowerOwnerServiceImpl implements FlowerOwnerService{
- /**
- * 顾客购买库存鲜花,根据用户控制台输入获得到的序号,来实际调用购买库存鲜花或者购买新培育的鲜花
- */
- @Override
- public void sell(Flower flower) {
-
- }
-
- @Override
- public void FlowerOwnerbuy(Flower flower) {
- }
-
- /**
- *顾客登录
- */
- @Override
- public FlowerOwner login() {
- FlowerOwner owner = new FlowerOwner();
- if (null != owner) {
- System.out.println("-------恭喜您成功登录-------");
- System.out.println("-------您的基本信息:-------");
- System.out.println("名字:" + owner.getName());
- System.out.println("资金:" + owner.getMoney());
- }
- return owner;
- }
- /**
- *
- * 根据顾客标识符(id)获得到该顾客所有鲜花信息
- */
- @Override
- public List<Flower> getMyFlower(int ownerId) {
- //TODO-1 根据顾客id获得到该顾客所有鲜花信息
- FlowerDaoImpl flowerDao = new FlowerDaoImpl();
- String[] param = {String.valueOf(ownerId)};
- String sql = "select * from flower where owner_id = ?";
- List<Flower> flowerList = flowerDao.selectFlower(sql, param);
- return flowerList;
-
- }
- }
- package step3.service.impl;
-
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.List;
- import java.util.Scanner;
-
- import step3.dao.AccountDao;
- import step3.dao.FlowerDao;
- import step3.dao.FlowerOwnerDao;
- import step3.dao.FlowerStoreDao;
- import step3.dao.impl.AccountDaoImpl;
- import step3.dao.impl.FlowerDaoImpl;
- import step3.dao.impl.FlowerOwnerDaoImpl;
- import step3.dao.impl.FlowerStoreDaoImpl;
- import step3.entity.Account;
- import step3.entity.Flower;
- import step3.entity.FlowerOwner;
- import step3.entity.FlowerStore;
- import step3.service.FlowerStoreService;
-
- /**
- * 鲜花商店实现类
- */
- public class FlowerStoreServiceImpl implements FlowerStoreService {
- /**
- * 查询鲜花商店账目 其中的1代表鲜花商店卖给顾客,2代表顾客卖给商店,代表鲜花商店的各种交易
- */
- @Override
- public List<Account> account(long storeId) {
- String sql = "select * from account where deal_type=? and seller_id=? union select * from account where deal_type=? and buyer_id=?";
- String[] param = {"1", String.valueOf(storeId), "2", String.valueOf(storeId)};
- AccountDao accountDao = new AccountDaoImpl();
- List<Account> list = accountDao.getFlowerStoreAccount(sql, param);
- return list;
- }
-
- /**
- * 修改鲜花商店资金信息
- */
- @Override
- public int modifyAccount(Flower flower, FlowerOwner owner) {
- String insertsql = "insert into account(deal_type,flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?,?)";
- String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
- Object[] accountParam = {1, flower.getId(), flower.getStoreId(), owner.getId(), flower.getPrice(), date};
- AccountDao accountDao = new AccountDaoImpl();
- int insertAccount = accountDao.updateAccount(insertsql, accountParam);
- return insertAccount;
- }
-
- /**
- * 培育鲜花
- */
- @Override
- public Flower bread(String flowerType, FlowerStore store) {
- Scanner input = new Scanner(System.in);
- System.out.println("请输入您期望培育的鲜花名字:");
- String flowerName = input.nextLine();
- System.out.println("请输入您期望的鲜花价格:");
- String flowerprice = input.nextLine();
- String storeId = String.valueOf(store.getId());
- String[] flowerParam = {flowerName, flowerType, storeId, flowerprice};
- FlowerFactoryImpl flowerFactory = new FlowerFactoryImpl();
- Flower flower = flowerFactory.breadNewFlower(flowerParam);
- String sql = "insert into flower(`name`,typeName,store_id,price) values(?,?,?,?)";
- Object[] param = {flower.getName(), flower.getTypeName(),
- flower.getStoreId(), flower.getPrice()};
- FlowerDao flowerDao = new FlowerDaoImpl();
- int count = flowerDao.updateFlower(sql, param);
- if (count > 0) {
- System.out.println(store.getName() + "成功培育了一种" + flower.getTypeName() + "鲜花");
- }
- return flower;
- }
-
- /**
- * 鲜花商店购买鲜花
- */
- @Override
- public void buy(Flower flower, FlowerStore oldstore) {
- String sql = "select * from flowerStore where id=?";
- String paramStore[] = {String.valueOf(oldstore.getId())};
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- FlowerStore store = storeDao.getFlowerStore(sql, paramStore);
- FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
- sql = "select * from flowerOwner where id = ?";
- String paramOwner[] = {String.valueOf(flower.getOwnerId())};
- FlowerOwner owner = ownerDao.selectOwner(sql, paramOwner);
- int updateFlower = modifyFlower(flower, null, store);// 更新鲜花信息
- if (updateFlower > 0) {// 更新顾客的信息
- int updateOwner = modifyOwner(owner, flower, 1);
- if (updateOwner > 0) {// 更新鲜花商店的信息
- int updateStore = modifyStore(flower, 1, store);
- if (updateStore > 0) {// 更新鲜花商店信息
- int insertAccount = modifyAccount(flower, owner);
- if (insertAccount > 0) {
- System.out.println(store.getName() + "已成功购买鲜花:" + flower.getName() + "价格为" + flower.getPrice());
- }
- }
- }
- } else {
- System.out.println("修改鲜花信息失败");
- }
- }
-
- /**
- * 商店卖鲜花
- */
- @Override
- public void sell(Flower flower) {
- FlowerDaoImpl FlowerDao = new FlowerDaoImpl();
- FlowerStoreDaoImpl storeDao = new FlowerStoreDaoImpl();
- FlowerOwnerDaoImpl ownerDao = new FlowerOwnerDaoImpl();
- FlowerStoreService FlowerStore = new FlowerStoreServiceImpl();
- String updatesql = "update Flower set store_id = null ,owner_id=? where id=?";
- Object[] param = {flower.getOwnerId(), flower.getId()};
- int updateFlower = FlowerDao.executeSQL(updatesql, param);// 更新鲜花信息
- if (updateFlower > 0) {// 更新顾客的信息
- String ownersql = "select * from Flowerowner where id=?";
- String ownerparam[] = {String.valueOf(flower.getOwnerId())};
-
- FlowerOwner owner = ownerDao.selectOwner(ownersql, ownerparam);
- String updateOwnerSql = "update Flowerowner set money=? where id=?";
- double count = flower.getPrice();
- Object[] ownerParam = {(owner.getMoney() - count), owner.getId()};
- int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);
- if (updateOwner > 0) {// 更新鲜花商店的信息
- FlowerStore store = FlowerStore.getFlowerStore(flower.getStoreId());
- String updateStore = "update Flowerstore set balance=? where id=?";
- Object[] storeParam = {(store.getBalance() + count),
- store.getId()};
- int updatestore = storeDao.executeSQL(updateStore, storeParam);
- if (updatestore > 0) {// 更新鲜花商店账户的信息
- String insertsql = "insert into account(deal_type,Flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?, ?)";
- String date = new SimpleDateFormat("yyyy-MM-dd")
- .format(new Date());
- Object[] accountParam = {1, flower.getId(), owner.getId(), flower.getStoreId(), count, date};
- AccountDao accountDao = new AccountDaoImpl();
- int insertAccount = accountDao.updateAccount(insertsql, accountParam);
- if (insertAccount > 0) {
- System.out.println(store.getName() + "已成功卖出价格为" + flower.getPrice() + "的鲜花" + flower.getName());
- }
- }
- }
- }
- }
-
- /**
- * 查询出所有库存鲜花
- */
- @Override
- public List<Flower> getFlowersInstock(long storeId) {
- FlowerDao flowerDao = new FlowerDaoImpl();
- String[] param = {String.valueOf(storeId)};
- String sql = "";
- // 当storeId不为0时,要执行查询指定商店库存鲜花
- if (storeId != 0) {
- sql = "select * from flower where owner_id is null and store_id=?";
- }
- // 当storeId为0时,要执行查询所有商店的库存鲜花
- if (0 == storeId) {
- sql = "select * from flower where owner_id is null";
- param = null;
- }
- List<Flower> flowerList = flowerDao.selectFlower(sql, param);
- return flowerList;
- }
-
- /**
- * 查询出所有新培育的鲜花,
- */
- @Override
- public List<Flower> getFlowersBread() {
- FlowerDao FlowerDao = new FlowerDaoImpl();
- String sql = "SELECT * FROM flower WHERE owner_id IS NULL AND typeName NOT IN (?,?,?) AND store_id IS Not NULL";
- String[] flowerParam = {"香槟玫瑰", "白玫瑰", "粉玫瑰"};
- List<Flower> flowerList = FlowerDao.selectFlower(sql, flowerParam);
- return flowerList;
- }
-
- /**
- * 根据顾客信息修改鲜花信息 根据FlowerOwnerEntity和FlowerStoreEntity的值判断是顾客买鲜花或者鲜花商店买鲜花
- * FlowerOwnerEntity=null是鲜花商店买鲜花,FlowerStoreEntity=null是顾客买鲜花
- */
- @Override
- public int modifyFlower(Flower flower, FlowerOwner flowerOwner, FlowerStore store) {
- String updatesql = null;
- long id = 0;
- if (null == store) {
- updatesql = "update flower set owner_id=? where id=?";
- id = flowerOwner.getId();
- } else if (null == flowerOwner) {
- updatesql = "update flower set store_id=?,owner_id=null where id=?";
- id = store.getId();
- }
-
- Object[] param = {id, flower.getId()};
- FlowerDaoImpl flowerDao = new FlowerDaoImpl();
- int updateFlower = flowerDao.executeSQL(updatesql, param);// 更新鲜花信息
-
- return updateFlower;
- }
-
- /**
- * 修改顾客信息 type=0是顾客买鲜花,type=1是鲜花商店买鲜花,价钱的变动
- */
- @Override
- public int modifyOwner(FlowerOwner owner, Flower flower, int type) {
- FlowerOwnerDaoImpl ownerDao = new FlowerOwnerDaoImpl();
- String updateOwnerSql = "update flowerowner set money=? where id=?";
-
- double count = 0;
- if (0 == type) {
- count = (owner.getMoney() - flower.getPrice());
- }
- if (1 == type) {
- count = (owner.getMoney() + flower.getPrice());
- }
- Object[] ownerParam = {count, owner.getId()};
- int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);
- return updateOwner;
- }
-
- /**
- * 修改鲜花商店信息 type=0是顾客买鲜花,type=1是鲜花商店买鲜花
- */
- @Override
- public int modifyStore(Flower flower, int type, FlowerStore oldstore) {
- FlowerStoreService store = new FlowerStoreServiceImpl();
- FlowerStore flowerStore = store.getFlowerStore(oldstore.getId());
- String updateStore = "update flowerstore set balance=? where id=?";
-
- double count = 0;
- if (0 == type) {
- count = (flowerStore.getBalance() + flower.getPrice());
- }
- if (1 == type) {
- count = (flowerStore.getBalance() - flower.getPrice());
- }
- Object[] storeParam = {count, oldstore.getId()};
- FlowerStoreDaoImpl storeDao = new FlowerStoreDaoImpl();
- int updatestore = storeDao.executeSQL(updateStore, storeParam);
- return updatestore;
- }
-
- /**
- * 鲜花商店登录
- */
- @Override
- public FlowerStore login() {
- Scanner input = new Scanner(System.in);
- FlowerStore flowerStore = null;
- // 1、输入鲜花商店名字
- boolean type = true;
- while (type) {
- System.out.println("请先登录,请输入鲜花商店名字:");
- String storeName = input.nextLine().trim();
- System.out.println("请输入鲜花商店的密码:");
- String storePassword = input.nextLine().trim();
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- String sql = "select * from flowerstore where name=? and password=?";
- String[] param = {storeName, storePassword};
- flowerStore = storeDao.getFlowerStore(sql, param);
- if (null != flowerStore) {
- System.out.println("-------恭喜成功登录-------");
- System.out.println("-------鲜花商店的基本信息:-------");
- System.out.println("名字:" + flowerStore.getName());
- System.out.println("资金:" + flowerStore.getBalance());
- type = false;
- } else {
- System.out.println("登录失败,请确认您的用户名和密码是否正确,重新登录");
- type = true;
- }
- }
- return flowerStore;
- }
-
- /**
- * 查询出所有鲜花商店正在出售的鲜花
- */
- @Override
- public List<Flower> getFlowerSelling() {
- FlowerDao flowerDao = new FlowerDaoImpl();
- String sql = "select * from flower where owner_id is not null";
- String[] flowerParam = null;
- List<Flower> flowerList = flowerDao.selectFlower(sql, flowerParam);
- return flowerList;
- }
-
- /**
- * 根据鲜花商店标识符查询鲜花商店信息
- */
- @Override
- public FlowerStore getFlowerStore(long id) {
- //TODO-2 根据鲜花商店标识符查询鲜花商店信息方法的实现
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- String sql = "select * from flowerStore where id=?";
- String paramStore[] = {String.valueOf(id)};
- FlowerStore store = storeDao.getFlowerStore(sql, paramStore);
- return store;
-
- }
- }
- package step4.service.impl;
-
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.List;
- import java.util.Scanner;
-
- import step4.dao.*;
- import step4.dao.impl.*;
- import step4.entity.*;
- import step4.service.FlowerStoreService;
-
- /**
- * 鲜花商店实现类
- */
- public class FlowerStoreServiceImpl implements FlowerStoreService {
-
- /**
- * 查询鲜花商店账目 其中的1代表鲜花商店卖给顾客,2代表顾客卖给商店,代表鲜花商店的各种交易
- */
- @Override
- public List<Account> account(long storeId) {
- String sql = "select * from account where deal_type=? and seller_id=? union select * from account where deal_type=? and buyer_id=?";
- String[] param = {"1", String.valueOf(storeId), "2", String.valueOf(storeId)};
- AccountDao accountDao = new AccountDaoImpl();
- List<Account> list = accountDao.getFlowerStoreAccount(sql, param);
- return list;
- }
-
- /**
- * 修改鲜花商店资金信息
- */
- @Override
- public int modifyAccount(Flower flower, FlowerOwner owner) {
- String insertsql = "insert into account(deal_type,flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?,?)";
- String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
- Object[] accountParam = {1, flower.getId(), flower.getStoreId(), owner.getId(), flower.getPrice(), date};
- AccountDao accountDao = new AccountDaoImpl();
- int insertAccount = accountDao.updateAccount(insertsql, accountParam);
- return insertAccount;
- }
-
- /**
- * 培育鲜花
- */
- @Override
- public Flower bread(String flowerType, FlowerStore store) {
- Scanner input = new Scanner(System.in);
- System.out.println("请输入您期望培育的鲜花名字:");
- String flowerName = input.nextLine();
- System.out.println("请输入您期望的鲜花价格:");
- String flowerprice = input.nextLine();
- String storeId = String.valueOf(store.getId());
- String[] flowerParam = {flowerName, flowerType, storeId, flowerprice};
- FlowerFactoryImpl flowerFactory = new FlowerFactoryImpl();
- Flower flower = flowerFactory.breadNewFlower(flowerParam);
- String sql = "insert into flower(`name`,typeName,store_id,price) values(?,?,?,?)";
- Object[] param = {flower.getName(), flower.getTypeName(), flower.getStoreId(), flower.getPrice()};
- FlowerDao flowerDao = new FlowerDaoImpl();
- int count = flowerDao.updateFlower(sql, param);
- if (count > 0) {
- System.out.println(store.getName() + "成功培育了一种" + flower.getTypeName() + "鲜花");
- }
- return flower;
- }
-
- /**
- * 鲜花商店购买鲜花
- */
- @Override
- public void buy(Flower flower, FlowerStore oldstore) {
- String sql = "select * from flowerStore where id=?";
- String paramStore[] = {String.valueOf(oldstore.getId())};
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- FlowerStore store = storeDao.getFlowerStore(sql, paramStore);
- FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
- sql = "select * from flowerOwner where id = ?";
- String paramOwner[] = {String.valueOf(flower.getOwnerId())};
- FlowerOwner owner = ownerDao.selectOwner(sql, paramOwner);
- int updateFlower = modifyFlower(flower, null, store);// 更新鲜花信息
- if (updateFlower > 0) {// 更新顾客的信息
- int updateOwner = modifyOwner(owner, flower, 1);
- if (updateOwner > 0) {// 更新鲜花商店的信息
- int updateStore = modifyStore(flower, 1, store);
- if (updateStore > 0) {// 更新鲜花商店信息
- int insertAccount = modifyAccount(flower, owner);
- if (insertAccount > 0) {
- System.out.println(store.getName() + "已成功购买鲜花:" + flower.getName() + "价格为" + flower.getPrice());
- }
- }
- }
- } else {
- System.out.println("修改鲜花信息失败");
- }
- }
-
- /**
- * 商店卖鲜花
- */
- @Override
- public void sell(Flower flower) {
- FlowerDaoImpl FlowerDao = new FlowerDaoImpl();
- FlowerStoreDaoImpl storeDao = new FlowerStoreDaoImpl();
- FlowerOwnerDaoImpl ownerDao = new FlowerOwnerDaoImpl();
- FlowerStoreService FlowerStore = new FlowerStoreServiceImpl();
- String updatesql = "update Flower set store_id = null ,owner_id=? where id=?";
- Object[] param = {flower.getOwnerId(), flower.getId()};
- int updateFlower = FlowerDao.executeSQL(updatesql, param);// 更新鲜花信息
- if (updateFlower > 0) {// 更新顾客的信息
- String ownersql = "select * from Flowerowner where id=?";
- String ownerparam[] = {String.valueOf(flower.getOwnerId())};
-
- FlowerOwner owner = ownerDao.selectOwner(ownersql, ownerparam);
- String updateOwnerSql = "update Flowerowner set money=? where id=?";
- double count = flower.getPrice();
- Object[] ownerParam = {(owner.getMoney() - count), owner.getId()};
- int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);
- if (updateOwner > 0) {// 更新鲜花商店的信息
- FlowerStore store = FlowerStore.getFlowerStore(flower.getStoreId());
- String updateStore = "update Flowerstore set balance=? where id=?";
- Object[] storeParam = {(store.getBalance() + count),
- store.getId()};
- int updatestore = storeDao.executeSQL(updateStore, storeParam);
- if (updatestore > 0) {// 更新鲜花商店账户的信息
- String insertsql = "insert into account(deal_type,Flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?, ?)";
- String date = new SimpleDateFormat("yyyy-MM-dd")
- .format(new Date());
- Object[] accountParam = {1, flower.getId(), owner.getId(), flower.getStoreId(), count, date};
- AccountDao accountDao = new AccountDaoImpl();
- int insertAccount = accountDao.updateAccount(insertsql, accountParam);
- if (insertAccount > 0) {
- System.out.println(store.getName() + "已成功卖出价格为" + flower.getPrice() + "的鲜花" + flower.getName());
- }
- }
- }
- }
- }
-
- /**
- * 查询出所有库存鲜花
- */
- @Override
- public List<Flower> getFlowersInstock(long storeId) {
- FlowerDao flowerDao = new FlowerDaoImpl();
- String[] param = {String.valueOf(storeId)};
- String sql = "";
- // 当storeId不为0时,要执行查询指定商店库存鲜花
- if (storeId != 0) {
- sql = "select * from flower where owner_id is null and store_id=?";
- }
- // 当storeId为0时,要执行查询所有商店的库存鲜花
- if (0 == storeId) {
- sql = "select * from flower where owner_id is null";
- param = null;
- }
- List<Flower> flowerList = flowerDao.selectFlower(sql, param);
- return flowerList;
- }
-
- /**
- * 查询出所有新培育的鲜花,
- */
- @Override
- public List<Flower> getFlowersBread() {
- FlowerDao FlowerDao = new FlowerDaoImpl();
- String sql = "SELECT * FROM flower WHERE owner_id IS NULL AND typeName NOT IN (?,?,?) AND store_id IS Not NULL";
- String[] flowerParam = {"香槟玫瑰", "白玫瑰", "粉玫瑰"};
- List<Flower> flowerList = FlowerDao.selectFlower(sql, flowerParam);
- return flowerList;
- }
-
-
- /**
- * 根据顾客信息修改鲜花信息 根据FlowerOwnerEntity和FlowerStoreEntity的值判断是顾客买鲜花或者鲜花商店买鲜花
- * FlowerOwnerEntity=null是鲜花商店买鲜花,FlowerStoreEntity=null是顾客买鲜花
- */
- @Override
- public int modifyFlower(Flower flower, FlowerOwner flowerOwner, FlowerStore store) {
- String updatesql = null;
- long id = 0;
- if (null == store) {
- updatesql = "update flower set owner_id=? where id=?";
- id = flowerOwner.getId();
- } else if (null == flowerOwner) {
- updatesql = "update flower set store_id=?,owner_id=null where id=?";
- id = store.getId();
- }
-
- Object[] param = {id, flower.getId()};
- FlowerDaoImpl flowerDao = new FlowerDaoImpl();
- int updateFlower = flowerDao.executeSQL(updatesql, param);// 更新鲜花信息
-
- return updateFlower;
- }
-
- /**
- * 修改顾客信息 type=0是顾客买鲜花,type=1是鲜花商店买鲜花,价钱的变动
- */
- @Override
- public int modifyOwner(FlowerOwner owner, Flower flower, int type) {
- FlowerOwnerDaoImpl ownerDao = new FlowerOwnerDaoImpl();
- String updateOwnerSql = "update flowerowner set money=? where id=?";
-
- double count = 0;
- if (0 == type) {
- count = (owner.getMoney() - flower.getPrice());
- }
- if (1 == type) {
- count = (owner.getMoney() + flower.getPrice());
- }
- Object[] ownerParam = {count, owner.getId()};
- int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);
- return updateOwner;
- }
-
-
- /**
- * 修改鲜花商店信息 type=0是顾客买鲜花,type=1是鲜花商店买鲜花
- */
- @Override
- public int modifyStore(Flower flower, int type, FlowerStore oldstore) {
- FlowerStoreService store = new FlowerStoreServiceImpl();
- FlowerStore flowerStore = store.getFlowerStore(oldstore.getId());
- String updateStore = "update flowerstore set balance=? where id=?";
-
- double count = 0;
- if (0 == type) {
- count = (flowerStore.getBalance() + flower.getPrice());
- }
- if (1 == type) {
- count = (flowerStore.getBalance() - flower.getPrice());
- }
- Object[] storeParam = {count, oldstore.getId()};
- FlowerStoreDaoImpl storeDao = new FlowerStoreDaoImpl();
- int updatestore = storeDao.executeSQL(updateStore, storeParam);
- return updatestore;
- }
-
- /**
- * 鲜花商店登录
- *
- * @param storeName
- * @param storePassword
- * @return
- */
- @Override
- public FlowerStore login(String storeName, String storePassword) {
- FlowerStore flowerStore;
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- //TODO-2 鲜花商店登录(访问数据库,查询鲜花商店名称和密码是否正确)
- String sql = "select * from flowerStore where name=? and password=?";
- String paramStore[] = {String.valueOf(storeName),String.valueOf(storePassword)};
- flowerStore = storeDao.getFlowerStore(sql, paramStore);
-
- return flowerStore;
- }
-
- /**
- * 查询出所有鲜花商店正在出售的鲜花
- */
- @Override
- public List<Flower> getFlowerSelling() {
- FlowerDao flowerDao = new FlowerDaoImpl();
- String sql = "select * from flower where owner_id is not null";
- String[] flowerParam = null;
- List<Flower> flowerList = flowerDao.selectFlower(sql, flowerParam);
- return flowerList;
- }
-
- /**
- * 根据鲜花商店标识符查询鲜花信息
- */
- @Override
- public FlowerStore getFlowerStore(long id) {
- String sql = "select * from flowerstore where id=" + id;
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- FlowerStore flowerStore = storeDao.getFlowerStore(sql, null);
- return flowerStore;
- }
- }
- package step4.test;
-
- import dbtest.InitDB;
- import step4.entity.*;
- import step4.dao.*;
- import step4.dao.impl.*;
- import step4.service.*;
- import step4.service.impl.*;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
-
- public class FlowerStoreApp {
- private static Scanner input = new Scanner(System.in);
-
- public static void main(String[] args) {
- //1-初始化 NySQL 数据库
- InitDB db = new InitDB();
- //2-启动鲜花商店系统
- startFlowerShop();
- }
-
- private static void startFlowerShop() {
- System.out.println("----------------------鲜花商店启动-----------------");
- System.out.println("鲜花信息");
- System.out.println("****************************************************");
- FlowerDao flowerDao = new FlowerDaoImpl();
- List<Flower> flowerList = flowerDao.getAllFlower();
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花品种\t" + "鲜花售价\t" + "");
- for (int i = 0; i < flowerList.size(); i++) {
- Flower flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("****************************************************");
- System.out.print("\n");
- System.out.println("顾客信息");
- FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
- List<FlowerOwner> ownerList = ownerDao.getAllOwner();
- System.out.println("****************************************************");
- System.out.println("序号\t" + "顾客姓名\t");
- for (int i = 0; i < ownerList.size(); i++) {
- FlowerOwner owner = ownerList.get(i);
- System.out.println((i + 1) + "\t" + owner.getName() + "\t");
- }
- System.out.println("****************************************************");
- System.out.print("\n");
- System.out.println("鲜花商店信息");
- System.out.println("****************************************************");
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- List<FlowerStore> storeList = storeDao.getAllStore();
- System.out.println("序号\t" + "鲜花商店名称\t");
- for (int i = 0; i < storeList.size(); i++) {
- FlowerStore store = storeList.get(i);
- System.out.println((i + 1) + "\t" + store.getName() + "\t");
- }
- System.out.println("****************************************************");
- System.out.print("\n");
- //系统登录
- FlowerStore flowerStore = storeLogin();
- }
-
- //鲜花商店登录
- private static FlowerStore storeLogin() {
- // Scanner input = new Scanner(System.in);
- FlowerStoreService fsService = new FlowerStoreServiceImpl();
- FlowerStore flowerStore = null;
- //TODO-1 鲜花商店登录代码实现(允许多次输入商店名称和密码)
- while(true){
- System.out.println("请先登录,请输入鲜花商店名字:");
- String n = input.next();
- System.out.println("请输入鲜花商店的密码:");
- String p = input.next();
- flowerStore = fsService.login(n,p);
- if(flowerStore==null){
- System.out.println("登录失败,请确认您的用户名和密码是否正确,重新登录");
- continue;
- }
- break;
- }
- System.out.println("-------恭喜成功登录-------");
- System.out.println("-------鲜花商店的基本信息:-------");
- System.out.printf("名字:%s\n资金:%.1f\n",flowerStore.getName(),flowerStore.getBalance());
-
- System.out.println("您已登录成功,可以进行如下操作");
- StoreChoose(flowerStore);
-
- return flowerStore;
- }
-
- //创建鲜花商店
- private static FlowerStore createFlowerStore(FlowerStore store) {
- FlowerStoreFactory storeFactory = new FlowerStoreFactoryImpl();
- storeFactory.createFlowerStore();
- IsStoreLogOut(store);
-
- return store;//返回的是鲜花商店创建者
- }
-
- private static void IsStoreLogOut(FlowerStore store) {
- System.out.println("您是否继续其它操作若是请输入y,退出请按任意键");
- //Scanner input = new Scanner(System.in);
- String code = input.next();
- if (code.equals("y")) {
- StoreChoose(store);
- } else {
- System.out.println("您已成功退出系统");
- }
- }
-
- private static void StoreChoose(FlowerStore store) {
- System.out.println("1:购买鲜花");
- System.out.println("2:卖出鲜花");
- System.out.println("3:培育鲜花");
- System.out.println("4:查询待售鲜花");
- System.out.println("5:查看商店结余");
- System.out.println("6:查看商店账目");
- System.out.println("7:开鲜花商店");
- System.out.println("请根据需要执行的操作,选择序号输入,退出请输入0");
- // Scanner input = new Scanner(System.in);
- boolean type = true;
- while (type) {
-
- int num = input.nextInt();
- switch (num) {
- case 0:
- System.out.println("退出成功");
- type = false;
- break;
- case 1:
- storeBuy(store);
- type = false;
- break;
- case 2:
- storeSell(store);
- type = false;
- break;
- case 3:
- storeBread(store);//传入登录的商店
- type = false;
- break;
- case 4:
- queryFlowerStock(store.getId());
- type = false;
- break;
- case 5:
- queryStoreBalance(store);
- type = false;
- break;
- case 6:
- getAccount(store.getId());
- type = false;
- break;
- case 7:
- createFlowerStore(store);
- type = false;
- break;
- default:
- System.out.println("输入有误,请重新输入");
- type = true;
- break;
- }
- }
- }
-
-
- //获得鲜花商店的资金1代表商店顾客,2顾客卖给商店,3代表顾客之间的交易
- private static FlowerStore getAccount(long id) {
- FlowerStoreService storeService = new FlowerStoreServiceImpl();
- FlowerStore store = storeService.getFlowerStore(id);
- List<Account> list = storeService.account(id);
- for (int i = 0; i < list.size(); i++) {
- Account account = list.get(i);
- String type = null;
- if (1 == account.getDealType()) {
- type = "商店卖给顾客";
- } else if (2 == account.getDealType()) {
- type = "顾客卖给商店";
- } else {
- type = "顾客之间交易";
- }
- System.out.println("第" + (i + 1) + "笔交易,交易类型为:" + type + ",交易金额是:"
- + account.getPrice());
- }
- IsStoreLogOut(store);
- return store;
- }
-
- //查询商店余额
- private static FlowerStore queryStoreBalance(FlowerStore store) {
- double balance = store.getBalance();
- System.out.println(store.getName() + "鲜花商店的结余为:" + balance);
- IsStoreLogOut(store);
- return store;
- }
-
- //查询待售鲜花
- private static FlowerStore queryFlowerStock(long storeId) {
- FlowerStoreService flowerStoreService = new FlowerStoreServiceImpl();
- FlowerStore store = flowerStoreService.getFlowerStore(storeId);
- Flower flower = null;
- List<Flower> flowerList = flowerStoreService.getFlowersInstock(storeId);
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- IsStoreLogOut(store);
- return store;
- }
-
- //鲜花商店培育鲜花
- private static FlowerStore storeBread(FlowerStore store) {
- FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
- //Scanner input = new Scanner(System.in);
- System.out.println("请输入要培育鲜花的品种(例如:玫瑰))");
- String flowerType = input.next();
- flowerStore.bread(flowerType, store);
- IsStoreLogOut(store);
- return store;
- }
-
- //以商店的身份登录,商店向顾客出售鲜花
- private static void storeSell(FlowerStore store) {
- //Scanner input = new Scanner(System.in);
- FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
- Flower flower = null;
- List<Flower> flowerList = flowerStore.getFlowersInstock(store.getId());
- System.out.println("-------以下是鲜花商店正在出售的鲜花-------");
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("---------请选择要购买的鲜花序号--------");
- boolean type = true;
- while (type) {
- int num = input.nextInt();
- if ((num - 1) < flowerList.size() && (num - 1) >= 0) {
- flower = flowerList.get(num - 1);
- System.out.println("------要卖出的鲜花信息如下------");
- System.out.println("鲜花名称为:" + flower.getName() + " 鲜花类别是:" + flower.getTypeName() + "鲜花价格是:" + flower.getPrice());
- System.out.println("请确认是否卖出,y代表卖出,n代表不卖");
- String code = input.next();
- if (null != code) {
- if ("y".equals(code)) {
- System.out.println("------下面是现有顾客买家,请选择您要卖给买家序号------");
- List<FlowerOwner> ownerList = new ArrayList<FlowerOwner>();
- FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
- ownerList = ownerDao.getAllOwner();
- FlowerOwner flowerOwner = null;
- System.out.println("序号\t" + "顾客姓名\t");
- for (int i = 0; i < ownerList.size(); i++) {
- flowerOwner = ownerList.get(i);
- System.out.println((i + 1) + "\t" + flowerOwner.getName() + "\t");
- }
- num = input.nextInt();
- if ((num - 1) < ownerList.size() && (num - 1) >= 0) {
- flowerOwner = ownerList.get(num - 1);
- }
- flower.setOwnerId(flowerOwner.getId());
- flowerStore.sell(flower);
- } else if ("n".equals(code)) {
- System.out
- .println("--------您选择放弃本次交易,希望您再次光顾----------");
-
- } else {
- System.out.println("--------您的输入有误----------");
- }
- }
- type = true;
- } else {
- System.out.println("输入有误,请按照序号重新输入");
- type = false;
- }
- type = false;// 标识符更改为false,退出系统
- }
- IsStoreLogOut(store);
- }
-
- //以商店的身份登录,商店找顾客购买鲜花
- private static void storeBuy(FlowerStore store) {
- //Scanner input = new Scanner(System.in);
- FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
- Flower flower = null;
- List<Flower> flowerList = flowerStore.getFlowerSelling();
- System.out.println("-------以下是顾客正在出售的鲜花-------");
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
- int num = input.nextInt();
- flower = flowerList.get(num - 1);
- //System.out.println("选中的鲜花信息"+flower.getName()+"id"+flower.getId());
- flowerStore.buy(flower, store);
- IsStoreLogOut(store);
- }
-
- //顾客登录
- private static FlowerOwner ownerLogin() {
- //Scanner input = new Scanner(System.in);
- System.out.println("请先登录,请您输入姓名:");
- String ownerName = input.nextLine().trim();
- System.out.println("请您输入密码:");
- String ownerPassword = input.nextLine().trim();
- FlowerOwnerService flowerOwner = new FlowerOwnerServiceImpl();
- FlowerOwner Owner = flowerOwner.login(ownerName, ownerPassword);
- boolean reg = true;
- while (reg) {
- if (null == Owner) {
- System.out.println("登录失败,请确认您的用户名和密码后重新输入");
- System.out.println("请先登录,请您输入姓名:");
- ownerName = input.nextLine().trim();
- System.out.println("请您输入密码:");
- ownerPassword = input.nextLine().trim();
- Owner = flowerOwner.login(ownerName, ownerPassword);
- reg = true;
- } else {
- reg = false;
- System.out.println("登录成功,您可以购买和卖出鲜花,如果您想购买鲜花请输入1,如果想卖出鲜花请输入2");
- System.out.println("1:购买鲜花");
- System.out.println("2:卖出鲜花");
- boolean type = true;
- //----------------------
- FlowerCirculate(type, Owner);
- }
- }
- return Owner;
- }
-
- private static boolean FlowerCirculate(boolean type, FlowerOwner owner) {
- while (type) {
- int num = input.nextInt();
- if (1 == num) {
- ownerBuy(owner);
- type = false;
- } else if (2 == num) {
- ownerSell(owner);
- type = false;
- } else {
- System.out.println("输入有误,请重新输入");
- type = true;
- }
- }
- return type;
- }
-
- //顾客向商店卖鲜花
- private static void ownerSell(FlowerOwner flowerowner) {
- }
-
- //顾客找鲜花商店买鲜花
- private static void ownerBuy(FlowerOwner flowerowner) {
- }
- }
- package step5.service.impl;
-
- import step5.dao.AccountDao;
- import step5.dao.FlowerDao;
- import step5.dao.FlowerOwnerDao;
- import step5.dao.FlowerStoreDao;
- import step5.dao.impl.AccountDaoImpl;
- import step5.dao.impl.FlowerDaoImpl;
- import step5.dao.impl.FlowerOwnerDaoImpl;
- import step5.dao.impl.FlowerStoreDaoImpl;
- import step5.entity.Account;
- import step5.entity.Flower;
- import step5.entity.FlowerOwner;
- import step5.entity.FlowerStore;
- import step5.service.FlowerStoreService;
-
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.List;
- import java.util.Scanner;
-
- /**
- * 鲜花商店实现类
- */
- public class FlowerStoreServiceImpl implements FlowerStoreService {
- /**
- * 查询鲜花商店账目 其中的1代表鲜花商店卖给顾客,2代表顾客卖给商店,代表鲜花商店的各种交易
- */
- @Override
- public List<Account> account(long storeId) {
- String sql = "select * from account where deal_type=? and seller_id=? union select * from account where deal_type=? and buyer_id=?";
- String[] param = {"1", String.valueOf(storeId), "2", String.valueOf(storeId)};
- AccountDao accountDao = new AccountDaoImpl();
- List<Account> list = accountDao.getFlowerStoreAccount(sql, param);
- return list;
- }
-
- /**
- * 修改鲜花商店资金信息
- */
- @Override
- public int modifyAccount(Flower flower, FlowerOwner owner) {
- String insertsql = "insert into account(deal_type,flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?,?)";
- String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
- Object[] accountParam = {1, flower.getId(), flower.getStoreId(), owner.getId(), flower.getPrice(), date};
- AccountDao accountDao = new AccountDaoImpl();
- int insertAccount = accountDao.updateAccount(insertsql, accountParam);
- return insertAccount;
- }
-
- /**
- * 培育鲜花
- */
- @Override
- public Flower bread(String flowerType, FlowerStore store) {
- Scanner input = new Scanner(System.in);
- System.out.println("请输入您期望培育的鲜花名字:");
- String flowerName = input.nextLine();
- System.out.println("请输入您期望的鲜花价格:");
- String flowerprice = input.nextLine();
- String storeId = String.valueOf(store.getId());
- String[] flowerParam = {flowerName, flowerType, storeId, flowerprice};
- FlowerFactoryImpl flowerFactory = new FlowerFactoryImpl();
- Flower flower = flowerFactory.breadNewFlower(flowerParam);
- String sql = "insert into flower(`name`,typeName,store_id,price) values(?,?,?,?)";
- Object[] param = {flower.getName(), flower.getTypeName(), flower.getStoreId(), flower.getPrice()};
- FlowerDao flowerDao = new FlowerDaoImpl();
- int count = flowerDao.updateFlower(sql, param);
- if (count > 0) {
- System.out.println(store.getName() + "成功培育了一种" + flower.getTypeName() + "鲜花");
- }
- return flower;
- }
-
- /**
- * 鲜花商店购买鲜花
- */
- @Override
- public void buy(Flower flower, FlowerStore oldstore) {
- String sql = "select * from flowerStore where id=?";
- String paramStore[] = {String.valueOf(oldstore.getId())};
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- FlowerStore store = storeDao.getFlowerStore(sql, paramStore);
- FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
- sql = "select * from flowerOwner where id = ?";
- String paramOwner[] = {String.valueOf(flower.getOwnerId())};
- FlowerOwner owner = ownerDao.selectOwner(sql, paramOwner);
- int updateFlower = modifyFlower(flower, null, store);// 更新鲜花信息
- if (updateFlower > 0) {// 更新顾客的信息
- int updateOwner = modifyOwner(owner, flower, 1);
- if (updateOwner > 0) {// 更新鲜花商店的信息
- int updateStore = modifyStore(flower, 1, store);
- if (updateStore > 0) {// 更新鲜花商店信息
- int insertAccount = modifyAccount(flower, owner);
- if (insertAccount > 0) {
- System.out.println(store.getName() + "已成功购买鲜花:" + flower.getName() + "价格为" + flower.getPrice());
- }
- }
- }
- } else {
- System.out.println("修改鲜花信息失败");
- }
- }
-
- /**
- * 商店卖鲜花
- */
- @Override
- public void sell(Flower flower) {
- FlowerDaoImpl FlowerDao = new FlowerDaoImpl();
- FlowerStoreDaoImpl storeDao = new FlowerStoreDaoImpl();
- FlowerOwnerDaoImpl ownerDao = new FlowerOwnerDaoImpl();
- FlowerStoreService FlowerStore = new FlowerStoreServiceImpl();
- String updatesql = "update Flower set store_id = null ,owner_id=? where id=?";
- Object[] param = {flower.getOwnerId(), flower.getId()};
- int updateFlower = FlowerDao.executeSQL(updatesql, param);// 更新鲜花信息
- if (updateFlower > 0) {// 更新顾客的信息
- String ownersql = "select * from Flowerowner where id=?";
- String ownerparam[] = {String.valueOf(flower.getOwnerId())};
-
- FlowerOwner owner = ownerDao.selectOwner(ownersql, ownerparam);
- String updateOwnerSql = "update Flowerowner set money=? where id=?";
- double count = flower.getPrice();
- Object[] ownerParam = {(owner.getMoney() - count), owner.getId()};
- int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);
- if (updateOwner > 0) {// 更新鲜花商店的信息
- FlowerStore store = FlowerStore.getFlowerStore(flower.getStoreId());
- String updateStore = "update Flowerstore set balance=? where id=?";
- Object[] storeParam = {(store.getBalance() + count),
- store.getId()};
- int updatestore = storeDao.executeSQL(updateStore, storeParam);
- if (updatestore > 0) {// 更新鲜花商店账户的信息
- String insertsql = "insert into account(deal_type,Flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?, ?)";
- String date = new SimpleDateFormat("yyyy-MM-dd")
- .format(new Date());
- Object[] accountParam = {1, flower.getId(), owner.getId(), flower.getStoreId(), count, date};
- AccountDao accountDao = new AccountDaoImpl();
- int insertAccount = accountDao.updateAccount(insertsql, accountParam);
- if (insertAccount > 0) {
- System.out.println(store.getName() + "已成功卖出价格为" + flower.getPrice() + "的鲜花" + flower.getName());
- }
- }
- }
- }
- }
-
- /**
- * 查询出所有库存鲜花
- */
- @Override
- public List<Flower> getFlowersInstock(long storeId) {
- FlowerDao flowerDao = new FlowerDaoImpl();
- String[] param = {String.valueOf(storeId)};
- String sql = "";
- // 当storeId不为 0 时,要执行查询指定商店库存鲜花
- if (storeId != 0) {
- // if (storeId != 1) {
- //TODO-2 填写查询指定商店库存鲜花SQL语句( sql="xxx"; )
- sql = "select * from Flower where store_id != ? and owner_id is not null";
-
- }
- // 当storeId为0时,要执行查询所有商店的库存鲜花
- if (0 == storeId) {
- //TODO-3 填写查询所有商店库存鲜花SQL语句( sql="xxx"; )
- // sql = "select * from Flower";
- sql = "select * from Flower where store_id <> 0 and owner_id is null";
-
- param = null;
- }
- List<Flower> flowerList = flowerDao.selectFlower(sql, param);
- return flowerList;
- }
-
- /**
- * 查询出所有新培育的鲜花,
- */
- @Override
- public List<Flower> getFlowersBread() {
- FlowerDao FlowerDao = new FlowerDaoImpl();
- String sql = "SELECT * FROM flower WHERE owner_id IS NULL AND typeName NOT IN (?,?,?) AND store_id IS Not NULL";
- String[] flowerParam = {"香槟玫瑰", "白玫瑰", "粉玫瑰"};
- List<Flower> flowerList = FlowerDao.selectFlower(sql, flowerParam);
- return flowerList;
- }
-
-
- /**
- * 根据顾客信息修改鲜花信息 根据FlowerOwnerEntity和FlowerStoreEntity的值判断是顾客买鲜花或者鲜花商店买鲜花
- * FlowerOwnerEntity=null是鲜花商店买鲜花,FlowerStoreEntity=null是顾客买鲜花
- */
- @Override
- public int modifyFlower(Flower flower, FlowerOwner flowerOwner, FlowerStore store) {
- String updatesql = null;
- long id = 0;
- if (null == store) {
- updatesql = "update flower set owner_id=? where id=?";
- id = flowerOwner.getId();
- } else if (null == flowerOwner) {
- updatesql = "update flower set store_id=?,owner_id=null where id=?";
- id = store.getId();
- }
-
- Object[] param = {id, flower.getId()};
- FlowerDaoImpl flowerDao = new FlowerDaoImpl();
- int updateFlower = flowerDao.executeSQL(updatesql, param);// 更新鲜花信息
-
- return updateFlower;
- }
-
- /**
- * 修改顾客信息 type=0是顾客买鲜花,type=1是鲜花商店买鲜花,价钱的变动
- */
- @Override
- public int modifyOwner(FlowerOwner owner, Flower flower, int type) {
- FlowerOwnerDaoImpl ownerDao = new FlowerOwnerDaoImpl();
- String updateOwnerSql = "update flowerowner set money=? where id=?";
-
- double count = 0;
- if (0 == type) {
- count = (owner.getMoney() - flower.getPrice());
- }
- if (1 == type) {
- count = (owner.getMoney() + flower.getPrice());
- }
- Object[] ownerParam = {count, owner.getId()};
- int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);
- return updateOwner;
- }
-
-
- /**
- * 修改鲜花商店信息 type=0是顾客买鲜花,type=1是鲜花商店买鲜花
- */
- @Override
- public int modifyStore(Flower flower, int type, FlowerStore oldstore) {
- FlowerStoreService store = new FlowerStoreServiceImpl();
- FlowerStore flowerStore = store.getFlowerStore(oldstore.getId());
- String updateStore = "update flowerstore set balance=? where id=?";
-
- double count = 0;
- if (0 == type) {
- count = (flowerStore.getBalance() + flower.getPrice());
- }
- if (1 == type) {
- count = (flowerStore.getBalance() - flower.getPrice());
- }
- Object[] storeParam = {count, oldstore.getId()};
- FlowerStoreDaoImpl storeDao = new FlowerStoreDaoImpl();
- int updatestore = storeDao.executeSQL(updateStore, storeParam);
- return updatestore;
- }
-
- /**
- * 鲜花商店登录
- *
- * @param storeName
- * @param storePassword
- * @return
- */
- @Override
- public FlowerStore login(String storeName, String storePassword) {
- FlowerStore flowerStore;
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- String sql = "select * from flowerstore where name=? and password=?";
- String[] param = {storeName, storePassword};
- flowerStore = storeDao.getFlowerStore(sql, param);
- return flowerStore;
-
- }
-
- /**
- * 查询出所有鲜花商店正在出售的鲜花
- */
- @Override
- public List<Flower> getFlowerSelling() {
- FlowerDao flowerDao = new FlowerDaoImpl();
- String sql = "select * from flower where owner_id is not null";
- String[] flowerParam = null;
- List<Flower> flowerList = flowerDao.selectFlower(sql, flowerParam);
- return flowerList;
- }
-
- /**
- * 根据鲜花商店标识符查询鲜花信息
- */
- @Override
- public FlowerStore getFlowerStore(long id) {
- String sql = "select * from flowerstore where id=" + id;
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- FlowerStore flowerStore = storeDao.getFlowerStore(sql, null);
- return flowerStore;
- }
- }
- package step5.test;
-
- import dbtest.InitDB;
- import step5.dao.*;
- import step5.dao.impl.*;
- import step5.entity.*;
- import step5.service.*;
- import step5.service.impl.*;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
-
- public class FlowerStoreApp {
- private static Scanner input = new Scanner(System.in);
-
- public static void main(String[] args) {
- //1-初始化 NySQL 数据库
- InitDB db = new InitDB();
- //2-启动鲜花商店系统
- startFlowerShop();
- }
-
- private static void startFlowerShop() {
- System.out.println("----------------------鲜花商店启动-----------------");
- System.out.println("鲜花信息");
- System.out.println("****************************************************");
- FlowerDao flowerDao = new FlowerDaoImpl();
- List<Flower> flowerList = flowerDao.getAllFlower();
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花品种\t" + "鲜花售价\t" + "");
- for (int i = 0; i < flowerList.size(); i++) {
- Flower flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("****************************************************");
- System.out.print("\n");
- System.out.println("顾客信息");
- FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
- List<FlowerOwner> ownerList = ownerDao.getAllOwner();
- System.out.println("****************************************************");
- System.out.println("序号\t" + "顾客姓名\t");
- for (int i = 0; i < ownerList.size(); i++) {
- FlowerOwner owner = ownerList.get(i);
- System.out.println((i + 1) + "\t" + owner.getName() + "\t");
- }
- System.out.println("****************************************************");
- System.out.print("\n");
- System.out.println("鲜花商店信息");
- System.out.println("****************************************************");
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- List<FlowerStore> storeList = storeDao.getAllStore();
- System.out.println("序号\t" + "鲜花商店名称\t");
- for (int i = 0; i < storeList.size(); i++) {
- FlowerStore store = storeList.get(i);
- System.out.println((i + 1) + "\t" + store.getName() + "\t");
- }
- System.out.println("****************************************************");
- System.out.print("\n");
- //系统登录
- System.out.println("请选择输入登录模式,输入1为顾客登录,输入2为鲜花商店登录");
- boolean type = true;
- String num;
- while (type) {
- num = input.nextLine();
- if ("1".equals(num)) {
- ownerLogin();
- type = false;
- } else if ("2".equals(num)) {
- storeLogin();
- type = false;
- } else {
- System.out.println("输入有误,请按照指定规则输入");
- System.out.println("请选择登录模式,输入1为顾客登录,输入2为鲜花商店登录");
- type = true;
- }
- }
- }
-
- //鲜花商店登录
- private static FlowerStore storeLogin() {
- FlowerStoreService fsService = new FlowerStoreServiceImpl();
- FlowerStore flowerStore = null;
- //TODO-1 鲜花商店登录代码实现
- boolean type = true;
- while (type) {
- System.out.println("请先登录,请输入鲜花商店名字:");
- String storeName = input.nextLine().trim();
- System.out.println("请输入鲜花商店的密码:");
- String storePassword = input.nextLine().trim();
- flowerStore = fsService.login(storeName, storePassword);
- if (null != flowerStore) {
- System.out.println("-------恭喜成功登录-------");
- System.out.println("-------鲜花商店的基本信息:-------");
- System.out.println("名字:" + flowerStore.getName());
- System.out.println("资金:" + flowerStore.getBalance());
- type = false;
- } else {
- System.out.println("登录失败,请确认您的用户名和密码是否正确,重新登录");
- type = true;
- }
- }
-
- System.out.println("您已登录成功,可以进行如下操作");
- StoreChoose(flowerStore);
-
- return flowerStore;
- }
-
- //创建鲜花商店
- private static FlowerStore createFlowerStore(FlowerStore store) {
- FlowerStoreFactory storeFactory = new FlowerStoreFactoryImpl();
- storeFactory.createFlowerStore();
- IsStoreLogOut(store);
-
- return store;//返回的是鲜花商店创建者
- }
-
- private static void IsStoreLogOut(FlowerStore store) {
- System.out.println("您是否继续其它操作若是请输入y,退出请按任意键");
- String code = input.next();
- if (code.equals("y")) {
- StoreChoose(store);
- } else {
- System.out.println("您已成功退出系统");
- }
- }
-
- private static void StoreChoose(FlowerStore store) {
- System.out.println("1:购买鲜花");
- System.out.println("2:卖出鲜花");
- System.out.println("3:培育鲜花");
- System.out.println("4:查询待售鲜花");
- System.out.println("5:查看商店结余");
- System.out.println("6:查看商店账目");
- System.out.println("7:开鲜花商店");
- System.out.println("请根据需要执行的操作,选择序号输入,退出请输入0");
- boolean type = true;
- while (type) {
- int num = input.nextInt();
- switch (num) {
- case 0:
- System.out.println("退出成功");
- type = false;
- break;
- case 1:
- storeBuy(store);
- type = false;
- break;
- case 2:
- storeSell(store);
- type = false;
- break;
- case 3:
- storeBread(store);//传入登录的商店
- type = false;
- break;
- case 4:
- queryFlowerStock(store.getId());
- type = false;
- break;
- case 5:
- queryStoreBalance(store);
- type = false;
- break;
- case 6:
- getAccount(store.getId());
- type = false;
- break;
- case 7:
- createFlowerStore(store);
- type = false;
- break;
- default:
- System.out.println("输入有误,请重新输入");
- type = true;
- break;
- }
- }
- }
-
-
- //获得鲜花商店的资金1代表商店顾客,2顾客卖给商店,3代表顾客之间的交易
- private static FlowerStore getAccount(long id) {
- FlowerStoreService storeService = new FlowerStoreServiceImpl();
- FlowerStore store = storeService.getFlowerStore(id);
- List<Account> list = storeService.account(id);
- for (int i = 0; i < list.size(); i++) {
- Account account = list.get(i);
- String type = null;
- if (1 == account.getDealType()) {
- type = "商店卖给顾客";
- } else if (2 == account.getDealType()) {
- type = "顾客卖给商店";
- } else {
- type = "顾客之间交易";
- }
- System.out.println("第" + (i + 1) + "笔交易,交易类型为:" + type + ",交易金额是:"
- + account.getPrice());
- }
- IsStoreLogOut(store);
- return store;
- }
-
- //查询商店余额
- private static FlowerStore queryStoreBalance(FlowerStore store) {
- double balance = store.getBalance();
- System.out.println(store.getName() + "鲜花商店的结余为:" + balance);
- IsStoreLogOut(store);
- return store;
- }
-
- //查询待售鲜花
- private static FlowerStore queryFlowerStock(long storeId) {
- FlowerStoreService flowerStoreService = new FlowerStoreServiceImpl();
- FlowerStore store = flowerStoreService.getFlowerStore(storeId);
- Flower flower = null;
- List<Flower> flowerList = flowerStoreService.getFlowersInstock(storeId);
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- IsStoreLogOut(store);
- return store;
- }
-
- //鲜花商店培育鲜花
- private static FlowerStore storeBread(FlowerStore store) {
- FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
- System.out.println("请输入要培育鲜花的品种(例如:玫瑰))");
- String flowerType = input.next();
- flowerStore.bread(flowerType, store);
- IsStoreLogOut(store);
- return store;
- }
-
- //以商店的身份登录,商店向顾客出售鲜花
- private static void storeSell(FlowerStore store) {
- FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
- Flower flower = null;
- List<Flower> flowerList = flowerStore.getFlowersInstock(store.getId());
- System.out.println("-------以下是鲜花商店正在出售的鲜花-------");
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("---------请选择要购买的鲜花序号--------");
- boolean type = true;
- while (type) {
- int num = input.nextInt();
- if ((num - 1) < flowerList.size() && (num - 1) >= 0) {
- flower = flowerList.get(num - 1);
- System.out.println("------要卖出的鲜花信息如下------");
- System.out.println("鲜花名称为:" + flower.getName() + " 鲜花类别是:" + flower.getTypeName() + "鲜花价格是:" + flower.getPrice());
- System.out.println("请确认是否卖出,y代表卖出,n代表不卖");
- String code = input.next();
- if (null != code) {
- if ("y".equals(code)) {
- System.out.println("------下面是现有顾客买家,请选择您要卖给买家序号------");
- List<FlowerOwner> ownerList = new ArrayList<FlowerOwner>();
- FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
- ownerList = ownerDao.getAllOwner();
- FlowerOwner flowerOwner = null;
- System.out.println("序号\t" + "顾客姓名\t");
- for (int i = 0; i < ownerList.size(); i++) {
- flowerOwner = ownerList.get(i);
- System.out.println((i + 1) + "\t" + flowerOwner.getName() + "\t");
- }
- num = input.nextInt();
- if ((num - 1) < ownerList.size() && (num - 1) >= 0) {
- flowerOwner = ownerList.get(num - 1);
- }
- flower.setOwnerId(flowerOwner.getId());
- flowerStore.sell(flower);
- } else if ("n".equals(code)) {
- System.out
- .println("--------您选择放弃本次交易,希望您再次光顾----------");
-
- } else {
- System.out.println("--------您的输入有误----------");
- }
- }
- type = true;
- } else {
- System.out.println("输入有误,请按照序号重新输入");
- type = false;
- }
- type = false;// 标识符更改为false,退出系统
- }
- IsStoreLogOut(store);
- }
-
- //以商店的身份登录,商店找顾客购买鲜花
- private static void storeBuy(FlowerStore store) {
- FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
- Flower flower = null;
- List<Flower> flowerList = flowerStore.getFlowerSelling();
- System.out.println("-------以下是顾客正在出售的鲜花-------");
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
- int num = input.nextInt();
- flower = flowerList.get(num - 1);
- //System.out.println("选中的鲜花信息"+flower.getName()+"id"+flower.getId());
- flowerStore.buy(flower, store);
- IsStoreLogOut(store);
- }
-
- //顾客登录
- private static FlowerOwner ownerLogin() {
- System.out.println("请先登录,请您输入姓名:");
- String ownerName = input.nextLine().trim();
- System.out.println("请您输入密码:");
- String ownerPassword = input.nextLine().trim();
- FlowerOwnerService flowerOwner = new FlowerOwnerServiceImpl();
- FlowerOwner Owner = flowerOwner.login(ownerName, ownerPassword);
- boolean reg = true;
- while (reg) {
- if (null == Owner) {
- System.out.println("登录失败,请确认您的用户名和密码后重新输入");
- System.out.println("请先登录,请您输入姓名:");
- ownerName = input.nextLine().trim();
- System.out.println("请您输入密码:");
- ownerPassword = input.nextLine().trim();
- Owner = flowerOwner.login(ownerName, ownerPassword);
- reg = true;
- } else {
- reg = false;
- System.out.println("登录成功,您可以购买和卖出鲜花,如果您想购买鲜花请输入1,如果想卖出鲜花请输入2");
- System.out.println("1:购买鲜花");
- System.out.println("2:卖出鲜花");
- boolean type = true;
- //----------------------
- FlowerCirculate(type, Owner);
- }
- }
- return Owner;
- }
-
- private static boolean FlowerCirculate(boolean type, FlowerOwner owner) {
- while (type) {
- int num = input.nextInt();
- if (1 == num) {
- ownerBuy(owner);
- type = false;
- } else if (2 == num) {
- ownerSell(owner);
- type = false;
- } else {
- System.out.println("输入有误,请重新输入");
- type = true;
- }
- }
- return type;
- }
-
- //顾客向商店卖鲜花
- private static void ownerSell(FlowerOwner flowerowner) {
- }
-
- //顾客找鲜花商店买鲜花
- private static void ownerBuy(FlowerOwner flowerowner) {
- System.out.println("-------请输入选择要购买范围:只需要按照下文要求输入选择项的序号即可--------");
- System.out.println("1:购买库存鲜花");
- System.out.println("2:购买新培育鲜花");
- FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
- FlowerOwnerService flowerOwner = new FlowerOwnerServiceImpl();
- Flower flower = null;
- int num = input.nextInt();
- List<Flower> flowerList = null;
-
- // num为1时购买库存鲜花
- boolean type = true;
- while (type) {
- if (num == 1) {
- System.out.println(num + "库存鲜花: -------以下是库存鲜花-------");
- //TODO-1 通过服务层接口查询库存鲜花
- flowerList = flowerStore.getFlowersInstock(0);
-
- //显示所有库存鲜花
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
- num = input.nextInt();
- flower = flowerList.get(num - 1);
- flower.setOwnerId(flowerowner.getId());
- flowerOwner.FlowerOwnerbuy(flower);
- type = false;
- } else if (num == 2) { // num为2时购买新培育鲜花
- System.out.println(num + "培育鲜花: -------以下是库存鲜花-------");
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- //通过服务层接口查询所有培育鲜花
- flowerList = flowerStore.getFlowersBread();
- //显示所有培育鲜花
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
- String count = input.next();
- if (count.matches("[0-9]*")) {
- num = Integer.parseInt(count);
- flower = flowerList.get(num - 1);
- flower.setOwnerId(flowerowner.getId());
- flowerOwner.FlowerOwnerbuy(flower);
- }
- type = false;
- } else {
- System.out.println("您的输入有误,请按照上诉提示输入");
- type = true;
- }
-
- }
- boolean isLogOut = true;
- System.out.println("您是否要继续进行其它操作,若是请输入Y,否则输入任意字母退出系统");
- String flag = input.next();
- if (flag.equals("Y")) {
- System.out.println("您可以购买和卖出鲜花,如果您想购买鲜花请输入1,如果想卖出鲜花请输入2");
- System.out.println("1:购买鲜花");
- System.out.println("2:卖出鲜花");
- //循环
- FlowerCirculate(isLogOut, flowerowner);
- } else {
- System.out.println("您已成功退出系统");
- }
- }
- }
- package step6.service.impl;
-
- import step6.dao.AccountDao;
- import step6.dao.FlowerDao;
- import step6.dao.FlowerOwnerDao;
- import step6.dao.FlowerStoreDao;
- import step6.dao.impl.AccountDaoImpl;
- import step6.dao.impl.FlowerDaoImpl;
- import step6.dao.impl.FlowerOwnerDaoImpl;
- import step6.dao.impl.FlowerStoreDaoImpl;
- import step6.entity.Account;
- import step6.entity.Flower;
- import step6.entity.FlowerOwner;
- import step6.entity.FlowerStore;
- import step6.service.FlowerStoreService;
-
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.List;
- import java.util.Scanner;
-
- /**
- * 鲜花商店实现类
- */
- public class FlowerStoreServiceImpl implements FlowerStoreService {
- /**
- * 查询鲜花商店账目 其中的1代表鲜花商店卖给顾客,2代表顾客卖给商店,代表鲜花商店的各种交易
- */
- @Override
- public List<Account> account(long storeId) {
- String sql = "select * from account where deal_type=? and seller_id=? union select * from account where deal_type=? and buyer_id=?";
- String[] param = {"1", String.valueOf(storeId), "2", String.valueOf(storeId)};
- AccountDao accountDao = new AccountDaoImpl();
- List<Account> list = accountDao.getFlowerStoreAccount(sql, param);
- return list;
- }
-
- /**
- * 修改鲜花商店资金信息
- */
- @Override
- public int modifyAccount(Flower flower, FlowerOwner owner) {
- String insertsql = "insert into account(deal_type,flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?,?)";
- String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
- Object[] accountParam = {1, flower.getId(), flower.getStoreId(), owner.getId(), flower.getPrice(), date};
- AccountDao accountDao = new AccountDaoImpl();
- int insertAccount = accountDao.updateAccount(insertsql, accountParam);
- return insertAccount;
- }
-
- /**
- * 培育鲜花
- */
- @Override
- public Flower bread(String flowerType, FlowerStore store) {
- Scanner input = new Scanner(System.in);
- System.out.println("请输入您期望培育的鲜花名字:");
- String flowerName = input.nextLine();
- System.out.println("请输入您期望的鲜花价格:");
- String flowerprice = input.nextLine();
- String storeId = String.valueOf(store.getId());
- String[] flowerParam = {flowerName, flowerType, storeId, flowerprice};
- FlowerFactoryImpl flowerFactory = new FlowerFactoryImpl();
- Flower flower = flowerFactory.breadNewFlower(flowerParam);
- String sql = "insert into flower(`name`,typeName,store_id,price) values(?,?,?,?)";
- Object[] param = {flower.getName(), flower.getTypeName(), flower.getStoreId(), flower.getPrice()};
- FlowerDao flowerDao = new FlowerDaoImpl();
- int count = flowerDao.updateFlower(sql, param);
- if (count > 0) {
- System.out.println(store.getName() + "成功培育了一种" + flower.getTypeName() + "鲜花");
- }
- return flower;
- }
-
- /**
- * 鲜花商店购买鲜花
- */
- @Override
- public void buy(Flower flower, FlowerStore oldstore) {
- String sql = "select * from flowerStore where id=?";
- String paramStore[] = {String.valueOf(oldstore.getId())};
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- FlowerStore store = storeDao.getFlowerStore(sql, paramStore);
- FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
- sql = "select * from flowerOwner where id = ?";
- String paramOwner[] = {String.valueOf(flower.getOwnerId())};
- FlowerOwner owner = ownerDao.selectOwner(sql, paramOwner);
- int updateFlower = modifyFlower(flower, null, store);// 更新鲜花信息
- if (updateFlower > 0) {// 更新顾客的信息
- int updateOwner = modifyOwner(owner, flower, 1);
- if (updateOwner > 0) {// 更新鲜花商店的信息
- int updateStore = modifyStore(flower, 1, store);
- if (updateStore > 0) {// 更新鲜花商店信息
- int insertAccount = modifyAccount(flower, owner);
- if (insertAccount > 0) {
- System.out.println(store.getName() + "已成功购买鲜花:" + flower.getName() + "价格为" + flower.getPrice());
- }
- }
- }
- } else {
- System.out.println("修改鲜花信息失败");
- }
- }
-
- /**
- * 商店卖鲜花
- */
- @Override
- public void sell(Flower flower) {
- FlowerDaoImpl FlowerDao = new FlowerDaoImpl();
- FlowerStoreDaoImpl storeDao = new FlowerStoreDaoImpl();
- FlowerOwnerDaoImpl ownerDao = new FlowerOwnerDaoImpl();
- FlowerStoreService FlowerStore = new FlowerStoreServiceImpl();
- String updatesql = "update Flower set store_id = null ,owner_id=? where id=?";
- Object[] param = {flower.getOwnerId(), flower.getId()};
- int updateFlower = FlowerDao.executeSQL(updatesql, param);// 更新鲜花信息
- if (updateFlower > 0) {// 更新顾客的信息
- String ownersql = "select * from Flowerowner where id=?";
- String ownerparam[] = {String.valueOf(flower.getOwnerId())};
-
- FlowerOwner owner = ownerDao.selectOwner(ownersql, ownerparam);
- String updateOwnerSql = "update Flowerowner set money=? where id=?";
- double count = flower.getPrice();
- Object[] ownerParam = {(owner.getMoney() - count), owner.getId()};
- int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);
- if (updateOwner > 0) {// 更新鲜花商店的信息
- FlowerStore store = FlowerStore.getFlowerStore(flower.getStoreId());
- String updateStore = "update Flowerstore set balance=? where id=?";
- Object[] storeParam = {(store.getBalance() + count),
- store.getId()};
- int updatestore = storeDao.executeSQL(updateStore, storeParam);
- if (updatestore > 0) {// 更新鲜花商店账户的信息
- String insertsql = "insert into account(deal_type,Flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?, ?)";
- String date = new SimpleDateFormat("yyyy-MM-dd")
- .format(new Date());
- Object[] accountParam = {1, flower.getId(), owner.getId(), flower.getStoreId(), count, date};
- AccountDao accountDao = new AccountDaoImpl();
- int insertAccount = accountDao.updateAccount(insertsql, accountParam);
- if (insertAccount > 0) {
- System.out.println(store.getName() + "已成功卖出价格为" + flower.getPrice() + "的鲜花" + flower.getName());
- }
- }
- }
- }
- }
-
- /**
- * 查询出所有库存鲜花
- */
- @Override
- public List<Flower> getFlowersInstock(long storeId) {
- FlowerDao flowerDao = new FlowerDaoImpl();
- String[] param = {String.valueOf(storeId)};
- String sql = "";
- // 当storeId不为 0 时,要执行查询指定商店库存鲜花
- if (storeId != 0) {
- //填写查询指定商店库存鲜花SQL语句
- sql = "select * from flower where owner_id is null and store_id=?";
- }
- // 当storeId为0时,要执行查询所有商店的库存鲜花
- if (0 == storeId) {
- //填写查询所有商店库存鲜花SQL语句
- sql = "select * from flower where owner_id is null";
- param = null;
- }
- List<Flower> flowerList = flowerDao.selectFlower(sql, param);
- return flowerList;
- }
-
- /**
- * 查询出所有新培育的鲜花,
- */
- @Override
- public List<Flower> getFlowersBread() {
- FlowerDao flowerDao = new FlowerDaoImpl();
- String sql = "SELECT * FROM flower WHERE owner_id IS NULL AND typeName NOT IN (?,?,?) AND store_id IS Not NULL";
- String[] flowerParam = {"香槟玫瑰", "白玫瑰", "粉玫瑰"};
- //TODO-2 通过Dao层访问新培育的鲜花,并将结果返回
- return flowerDao.selectFlower(sql, flowerParam);
-
-
- }
-
-
- /**
- * 根据顾客信息修改鲜花信息 根据FlowerOwnerEntity和FlowerStoreEntity的值判断是顾客买鲜花或者鲜花商店买鲜花
- * FlowerOwnerEntity=null是鲜花商店买鲜花,FlowerStoreEntity=null是顾客买鲜花
- */
- @Override
- public int modifyFlower(Flower flower, FlowerOwner flowerOwner, FlowerStore store) {
- String updatesql = null;
- long id = 0;
- if (null == store) {
- updatesql = "update flower set owner_id=? where id=?";
- id = flowerOwner.getId();
- } else if (null == flowerOwner) {
- updatesql = "update flower set store_id=?,owner_id=null where id=?";
- id = store.getId();
- }
-
- Object[] param = {id, flower.getId()};
- FlowerDaoImpl flowerDao = new FlowerDaoImpl();
- int updateFlower = flowerDao.executeSQL(updatesql, param);// 更新鲜花信息
-
- return updateFlower;
- }
-
- /**
- * 修改顾客信息 type=0是顾客买鲜花,type=1是鲜花商店买鲜花,价钱的变动
- */
- @Override
- public int modifyOwner(FlowerOwner owner, Flower flower, int type) {
- FlowerOwnerDaoImpl ownerDao = new FlowerOwnerDaoImpl();
- String updateOwnerSql = "update flowerowner set money=? where id=?";
-
- double count = 0;
- if (0 == type) {
- count = (owner.getMoney() - flower.getPrice());
- }
- if (1 == type) {
- count = (owner.getMoney() + flower.getPrice());
- }
- Object[] ownerParam = {count, owner.getId()};
- int updateOwner = ownerDao.executeSQL(updateOwnerSql, ownerParam);
- return updateOwner;
- }
-
-
- /**
- * 修改鲜花商店信息 type=0是顾客买鲜花,type=1是鲜花商店买鲜花
- */
- @Override
- public int modifyStore(Flower flower, int type, FlowerStore oldstore) {
- FlowerStoreService store = new FlowerStoreServiceImpl();
- FlowerStore flowerStore = store.getFlowerStore(oldstore.getId());
- String updateStore = "update flowerstore set balance=? where id=?";
-
- double count = 0;
- if (0 == type) {
- count = (flowerStore.getBalance() + flower.getPrice());
- }
- if (1 == type) {
- count = (flowerStore.getBalance() - flower.getPrice());
- }
- Object[] storeParam = {count, oldstore.getId()};
- FlowerStoreDaoImpl storeDao = new FlowerStoreDaoImpl();
- int updatestore = storeDao.executeSQL(updateStore, storeParam);
- return updatestore;
- }
-
- /**
- * 鲜花商店登录
- *
- * @param storeName
- * @param storePassword
- * @return
- */
- @Override
- public FlowerStore login(String storeName, String storePassword) {
- FlowerStore flowerStore;
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- String sql = "select * from flowerstore where name=? and password=?";
- String[] param = {storeName, storePassword};
- flowerStore = storeDao.getFlowerStore(sql, param);
- return flowerStore;
-
- }
-
- /**
- * 查询出所有鲜花商店正在出售的鲜花
- */
- @Override
- public List<Flower> getFlowerSelling() {
- FlowerDao flowerDao = new FlowerDaoImpl();
- String sql = "select * from flower where owner_id is not null";
- String[] flowerParam = null;
- List<Flower> flowerList = flowerDao.selectFlower(sql, flowerParam);
- return flowerList;
- }
-
- /**
- * 根据鲜花商店标识符查询鲜花信息
- */
- @Override
- public FlowerStore getFlowerStore(long id) {
- String sql = "select * from flowerstore where id=" + id;
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- FlowerStore flowerStore = storeDao.getFlowerStore(sql, null);
- return flowerStore;
- }
- }
- package step6.test;
-
- import dbtest.InitDB;
- import step6.dao.*;
- import step6.dao.impl.*;
- import step6.entity.*;
- import step6.service.*;
- import step6.service.impl.*;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
-
- public class FlowerStoreApp {
- private static Scanner input = new Scanner(System.in);
-
- public static void main(String[] args) {
- //1-初始化 NySQL 数据库 ownerBuy TODO
- InitDB db = new InitDB();
- //2-启动鲜花商店系统
- startFlowerShop();
- }
-
- private static void startFlowerShop() {
- System.out.println("----------------------鲜花商店启动-----------------");
- System.out.println("鲜花信息");
- System.out.println("****************************************************");
- FlowerDao flowerDao = new FlowerDaoImpl();
- List<Flower> flowerList = flowerDao.getAllFlower();
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花品种\t" + "鲜花售价\t" + "");
- for (int i = 0; i < flowerList.size(); i++) {
- Flower flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("****************************************************");
- System.out.print("\n");
- System.out.println("顾客信息");
- FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
- List<FlowerOwner> ownerList = ownerDao.getAllOwner();
- System.out.println("****************************************************");
- System.out.println("序号\t" + "顾客姓名\t");
- for (int i = 0; i < ownerList.size(); i++) {
- FlowerOwner owner = ownerList.get(i);
- System.out.println((i + 1) + "\t" + owner.getName() + "\t");
- }
- System.out.println("****************************************************");
- System.out.print("\n");
- System.out.println("鲜花商店信息");
- System.out.println("****************************************************");
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- List<FlowerStore> storeList = storeDao.getAllStore();
- System.out.println("序号\t" + "鲜花商店名称\t");
- for (int i = 0; i < storeList.size(); i++) {
- FlowerStore store = storeList.get(i);
- System.out.println((i + 1) + "\t" + store.getName() + "\t");
- }
- System.out.println("****************************************************");
- System.out.print("\n");
- //系统登录
- System.out.println("请选择输入登录模式,输入1为顾客登录,输入2为鲜花商店登录");
- boolean type = true;
- String num;
- while (type) {
- num = input.nextLine();
- if ("1".equals(num)) {
- ownerLogin();
- type = false;
- } else if ("2".equals(num)) {
- storeLogin();
- type = false;
- } else {
- System.out.println("输入有误,请按照指定规则输入");
- System.out.println("请选择登录模式,输入1为顾客登录,输入2为鲜花商店登录");
- type = true;
- }
- }
- }
-
- //鲜花商店登录
- private static FlowerStore storeLogin() {
- FlowerStoreService fsService = new FlowerStoreServiceImpl();
- FlowerStore flowerStore = null;
- //TODO-1 鲜花商店登录代码实现
- boolean type = true;
- while (type) {
- System.out.println("请先登录,请输入鲜花商店名字:");
- String storeName = input.nextLine().trim();
- System.out.println("请输入鲜花商店的密码:");
- String storePassword = input.nextLine().trim();
- flowerStore = fsService.login(storeName, storePassword);
- if (null != flowerStore) {
- System.out.println("-------恭喜成功登录-------");
- System.out.println("-------鲜花商店的基本信息:-------");
- System.out.println("名字:" + flowerStore.getName());
- System.out.println("资金:" + flowerStore.getBalance());
- type = false;
- } else {
- System.out.println("登录失败,请确认您的用户名和密码是否正确,重新登录");
- type = true;
- }
- }
-
- System.out.println("您已登录成功,可以进行如下操作");
- StoreChoose(flowerStore);
-
- return flowerStore;
- }
-
- //创建鲜花商店
- private static FlowerStore createFlowerStore(FlowerStore store) {
- FlowerStoreFactory storeFactory = new FlowerStoreFactoryImpl();
- storeFactory.createFlowerStore();
- IsStoreLogOut(store);
-
- return store;//返回的是鲜花商店创建者
- }
-
- private static void IsStoreLogOut(FlowerStore store) {
- System.out.println("您是否继续其它操作若是请输入y,退出请按任意键");
- String code = input.next();
- if (code.equals("y")) {
- StoreChoose(store);
- } else {
- System.out.println("您已成功退出系统");
- }
- }
-
- private static void StoreChoose(FlowerStore store) {
- System.out.println("1:购买鲜花");
- System.out.println("2:卖出鲜花");
- System.out.println("3:培育鲜花");
- System.out.println("4:查询待售鲜花");
- System.out.println("5:查看商店结余");
- System.out.println("6:查看商店账目");
- System.out.println("7:开鲜花商店");
- System.out.println("请根据需要执行的操作,选择序号输入,退出请输入0");
- boolean type = true;
- while (type) {
- int num = input.nextInt();
- switch (num) {
- case 0:
- System.out.println("退出成功");
- type = false;
- break;
- case 1:
- storeBuy(store);
- type = false;
- break;
- case 2:
- storeSell(store);
- type = false;
- break;
- case 3:
- storeBread(store);//传入登录的商店
- type = false;
- break;
- case 4:
- queryFlowerStock(store.getId());
- type = false;
- break;
- case 5:
- queryStoreBalance(store);
- type = false;
- break;
- case 6:
- getAccount(store.getId());
- type = false;
- break;
- case 7:
- createFlowerStore(store);
- type = false;
- break;
- default:
- System.out.println("输入有误,请重新输入");
- type = true;
- break;
- }
- }
- }
-
-
- //获得鲜花商店的资金1代表商店顾客,2顾客卖给商店,3代表顾客之间的交易
- private static FlowerStore getAccount(long id) {
- FlowerStoreService storeService = new FlowerStoreServiceImpl();
- FlowerStore store = storeService.getFlowerStore(id);
- List<Account> list = storeService.account(id);
- for (int i = 0; i < list.size(); i++) {
- Account account = list.get(i);
- String type = null;
- if (1 == account.getDealType()) {
- type = "商店卖给顾客";
- } else if (2 == account.getDealType()) {
- type = "顾客卖给商店";
- } else {
- type = "顾客之间交易";
- }
- System.out.println("第" + (i + 1) + "笔交易,交易类型为:" + type + ",交易金额是:"
- + account.getPrice());
- }
- IsStoreLogOut(store);
- return store;
- }
-
- //查询商店余额
- private static FlowerStore queryStoreBalance(FlowerStore store) {
- double balance = store.getBalance();
- System.out.println(store.getName() + "鲜花商店的结余为:" + balance);
- IsStoreLogOut(store);
- return store;
- }
-
- //查询待售鲜花
- private static FlowerStore queryFlowerStock(long storeId) {
- FlowerStoreService flowerStoreService = new FlowerStoreServiceImpl();
- FlowerStore store = flowerStoreService.getFlowerStore(storeId);
- Flower flower = null;
- List<Flower> flowerList = flowerStoreService.getFlowersInstock(storeId);
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- IsStoreLogOut(store);
- return store;
- }
-
- //鲜花商店培育鲜花
- private static FlowerStore storeBread(FlowerStore store) {
- FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
- System.out.println("请输入要培育鲜花的品种(例如:玫瑰))");
- String flowerType = input.next();
- flowerStore.bread(flowerType, store);
- IsStoreLogOut(store);
- return store;
- }
-
- //以商店的身份登录,商店向顾客出售鲜花
- private static void storeSell(FlowerStore store) {
- FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
- Flower flower = null;
- List<Flower> flowerList = flowerStore.getFlowersInstock(store.getId());
- System.out.println("-------以下是鲜花商店正在出售的鲜花-------");
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("---------请选择要购买的鲜花序号--------");
- boolean type = true;
- while (type) {
- int num = input.nextInt();
- if ((num - 1) < flowerList.size() && (num - 1) >= 0) {
- flower = flowerList.get(num - 1);
- System.out.println("------要卖出的鲜花信息如下------");
- System.out.println("鲜花名称为:" + flower.getName() + " 鲜花类别是:" + flower.getTypeName() + "鲜花价格是:" + flower.getPrice());
- System.out.println("请确认是否卖出,y代表卖出,n代表不卖");
- String code = input.next();
- if (null != code) {
- if ("y".equals(code)) {
- System.out.println("------下面是现有顾客买家,请选择您要卖给买家序号------");
- List<FlowerOwner> ownerList = new ArrayList<FlowerOwner>();
- FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
- ownerList = ownerDao.getAllOwner();
- FlowerOwner flowerOwner = null;
- System.out.println("序号\t" + "顾客姓名\t");
- for (int i = 0; i < ownerList.size(); i++) {
- flowerOwner = ownerList.get(i);
- System.out.println((i + 1) + "\t" + flowerOwner.getName() + "\t");
- }
- num = input.nextInt();
- if ((num - 1) < ownerList.size() && (num - 1) >= 0) {
- flowerOwner = ownerList.get(num - 1);
- }
- flower.setOwnerId(flowerOwner.getId());
- flowerStore.sell(flower);
- } else if ("n".equals(code)) {
- System.out
- .println("--------您选择放弃本次交易,希望您再次光顾----------");
-
- } else {
- System.out.println("--------您的输入有误----------");
- }
- }
- type = true;
- } else {
- System.out.println("输入有误,请按照序号重新输入");
- type = false;
- }
- type = false;// 标识符更改为false,退出系统
- }
- IsStoreLogOut(store);
- }
-
- //以商店的身份登录,商店找顾客购买鲜花
- private static void storeBuy(FlowerStore store) {
- FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
- Flower flower = null;
- List<Flower> flowerList = flowerStore.getFlowerSelling();
- System.out.println("-------以下是顾客正在出售的鲜花-------");
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
- int num = input.nextInt();
- flower = flowerList.get(num - 1);
- //System.out.println("选中的鲜花信息"+flower.getName()+"id"+flower.getId());
- flowerStore.buy(flower, store);
- IsStoreLogOut(store);
- }
-
- //顾客登录
- private static FlowerOwner ownerLogin() {
- System.out.println("请先登录,请您输入姓名:");
- String ownerName = input.nextLine().trim();
- System.out.println("请您输入密码:");
- String ownerPassword = input.nextLine().trim();
- FlowerOwnerService flowerOwner = new FlowerOwnerServiceImpl();
- FlowerOwner Owner = flowerOwner.login(ownerName, ownerPassword);
- boolean reg = true;
- while (reg) {
- if (null == Owner) {
- System.out.println("登录失败,请确认您的用户名和密码后重新输入");
- System.out.println("请先登录,请您输入姓名:");
- ownerName = input.nextLine().trim();
- System.out.println("请您输入密码:");
- ownerPassword = input.nextLine().trim();
- Owner = flowerOwner.login(ownerName, ownerPassword);
- reg = true;
- } else {
- reg = false;
- System.out.println("登录成功,您可以购买和卖出鲜花,如果您想购买鲜花请输入1,如果想卖出鲜花请输入2");
- System.out.println("1:购买鲜花");
- System.out.println("2:卖出鲜花");
- boolean type = true;
- //----------------------
- FlowerCirculate(type, Owner);
- }
- }
- return Owner;
- }
-
- private static boolean FlowerCirculate(boolean type, FlowerOwner owner) {
- while (type) {
- int num = input.nextInt();
- if (1 == num) {
- ownerBuy(owner);
- type = false;
- } else if (2 == num) {
- ownerSell(owner);
- type = false;
- } else {
- System.out.println("输入有误,请重新输入");
- type = true;
- }
- }
- return type;
- }
-
- //顾客向商店卖鲜花
- private static void ownerSell(FlowerOwner flowerowner) {
- }
-
- //顾客找鲜花商店买鲜花
- private static void ownerBuy(FlowerOwner flowerowner) {
- System.out.println("-------请输入选择要购买范围:只需要按照下文要求输入选择项的序号即可--------");
- System.out.println("1:购买库存鲜花");
- System.out.println("2:购买新培育鲜花");
- FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
- FlowerOwnerService flowerOwner = new FlowerOwnerServiceImpl();
- Flower flower = null;
- int num = input.nextInt();
- List<Flower> flowerList = null;
-
- // num为1时购买库存鲜花
- boolean type = true;
- while (type) {
- if (num == 1) {
- System.out.println(num + "库存鲜花: -------以下是库存鲜花-------");
- //通过服务层接口查询库存鲜花
- flowerList = flowerStore.getFlowersInstock(0);
-
- //显示所有库存鲜花
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
- num = input.nextInt();
- flower = flowerList.get(num - 1);
- flower.setOwnerId(flowerowner.getId());
- flowerOwner.FlowerOwnerbuy(flower);
- type = false;
- } else if (num == 2) { // num为2时购买新培育鲜花
- System.out.println(num + "培育鲜花: -------以下是库存鲜花-------");
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- //TODO-1 通过服务层接口查询所有培育鲜花 FlowerStoreServiceImpl public List<Flower> getFlowersBread()
- flowerList = null;
- flowerList = flowerStore.getFlowersBread();
-
- //显示所有培育鲜花
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
- String count = input.next();
- if (count.matches("[0-9]*")) {
- num = Integer.parseInt(count);
- flower = flowerList.get(num - 1);
- flower.setOwnerId(flowerowner.getId());
- flowerOwner.FlowerOwnerbuy(flower);
- }
- type = false;
- } else {
- System.out.println("您的输入有误,请按照上诉提示输入");
- type = true;
- }
-
- }
- boolean isLogOut = true;
- System.out.println("您是否要继续进行其它操作,若是请输入Y,否则输入任意字母退出系统");
- String flag = input.next();
- if (flag.equals("Y")) {
- System.out.println("您可以购买和卖出鲜花,如果您想购买鲜花请输入1,如果想卖出鲜花请输入2");
- System.out.println("1:购买鲜花");
- System.out.println("2:卖出鲜花");
- //循环
- FlowerCirculate(isLogOut, flowerowner);
- } else {
- System.out.println("您已成功退出系统");
- }
- }
- }
- package step7.service.impl;
-
- import step7.dao.AccountDao;
- import step7.dao.FlowerDao;
- import step7.dao.FlowerOwnerDao;
- import step7.dao.FlowerStoreDao;
- import step7.dao.impl.AccountDaoImpl;
- import step7.dao.impl.FlowerDaoImpl;
- import step7.dao.impl.FlowerOwnerDaoImpl;
- import step7.dao.impl.FlowerStoreDaoImpl;
- import step7.entity.Flower;
- import step7.entity.FlowerOwner;
- import step7.entity.FlowerStore;
- import step7.service.FlowerOwnerService;
- import step7.service.FlowerStoreService;
-
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
-
- /**
- * @author 鲜花顾客实现类
- */
- public class FlowerOwnerServiceImpl implements FlowerOwnerService {
- /**
- * 顾客购买库存鲜花,根据用户控制台输入获得到的序号,来实际调用购买库存鲜花或者购买新培育的鲜花 sell
- */
- @Override
- public void sell(Flower flower) {
- FlowerDao flowerDao = new FlowerDaoImpl();
- FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
- String updatesql = "update flower set store_id=?,owner_id=NUll where id=?";
- Object[] param = {flower.getStoreId(), flower.getId()};
- int updateFlower = flowerDao.updateFlower(updatesql, param);// 更新鲜花信息
-
- if (updateFlower > 0) {// 更新顾客的信息
- String ownersql = "select * from flowerowner where id=?";
- String ownerparam[] = {String.valueOf(flower.getOwnerId())};
-
- FlowerOwner owner = ownerDao.selectOwner(ownersql, ownerparam);
- String updateOwnerSql = "update flowerowner set money=? where id=?";
- String[] ownerParam = {String.valueOf((owner.getMoney() + flower.getPrice())), String.valueOf(owner.getId())};
- int updateOwner = ownerDao.updateOwner(updateOwnerSql, ownerParam);
- if (updateOwner > 0) {// 更新鲜花商店的信息
- FlowerStoreService store = new FlowerStoreServiceImpl();
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- FlowerStore flowerStore = store.getFlowerStore(flower.getStoreId());
- //TODO-4 在 Dao层更新鲜花商店的信息
- String sql = "update flowerstore set balance=? where id=?";
- Object[] ownerParam2 ={(flowerStore.getBalance()-flower.getPrice()),flowerStore.getId()};
- int updatestore = storeDao.updateStore(sql, ownerParam2);
-
-
- if (updatestore > 0) {// 更新鲜花商店台帐信息
- String insertsql = "insert into account(deal_type,flower_id,seller_id,buyer_id,price,deal_time) values (?, ?, ?, ?, ?, ?)";
- String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
- Object[] accountParam = {2, flower.getId(), owner.getId(), flower.getStoreId(), flower.getPrice(), date};
- //TODO-5 在 Dao层在 account表中插入数据
- AccountDaoImpl ac = new AccountDaoImpl();
- int insertAccount = ac.updateAccount(insertsql,accountParam);
-
- if (insertAccount > 0) {
- System.out.println("您已成功卖出鲜花:" + flower.getName() + "获得收入" + flower.getPrice());
- }
- }
- }
- }
- }
-
- @Override
- public void FlowerOwnerbuy(Flower flower) {
- String sql = "select * from flowerowner where id=?";
- String param[] = {String.valueOf(flower.getOwnerId())};
- FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
- FlowerOwner owner = ownerDao.selectOwner(sql, param);
- String sql1 = "select * from flowerStore where id=?";
- String param1[] = {String.valueOf(flower.getStoreId())};
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- FlowerStore store = storeDao.getFlowerStore(sql, param1);
- FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
- int updateFlower = flowerStore.modifyFlower(flower, owner, null);// 更新鲜花信息
- if (updateFlower > 0) {// 更新顾客的信息
- int updateOwner = flowerStore.modifyOwner(owner, flower, 0);
- if (updateOwner > 0) {// 更新鲜花商店的信息
- int updateStore = flowerStore.modifyStore(flower, 0, store);
- if (updateStore > 0) {// 更新鲜花商店帐户信息
- int insertAccount = flowerStore.modifyAccount(flower, owner);
- if (insertAccount > 0) {
- System.out.println("您已成功购买价格为" + flower.getPrice() + "的" + flower.getName());
- }
- }
- }
- }
- }
-
- /**
- * 顾客登录
- */
- @Override
- public FlowerOwner login(String ownerName,String ownerPassword) {
- FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
- String sql = "select * from flowerowner where name=? and password=?";
- String[] param = {ownerName, ownerPassword};
- FlowerOwner owner = ownerDao.selectOwner(sql, param);
- return owner;
- }
-
- /**
- * 根据顾客标识符(id)获得到该顾客所有鲜花信息
- */
- @Override
- public List<Flower> getMyFlower(int ownerId) {
- List<Flower> flowerList = new ArrayList<Flower>();
- String sql = "select * from flower where owner_id=?";
- String[] param = {String.valueOf(ownerId)};
- FlowerDao petDao = new FlowerDaoImpl();
- flowerList = petDao.selectFlower(sql, param);
- return flowerList;
- }
- }
- package step7.test;
-
- import dbtest.InitDB;
- import step7.dao.*;
- import step7.dao.impl.*;
- import step7.entity.*;
- import step7.service.*;
- import step7.service.impl.*;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
-
- public class FlowerStoreApp {
- private static Scanner input = new Scanner(System.in);
-
- public static void main(String[] args) {
- //1-初始化 NySQL 数据库
- InitDB db = new InitDB();
- //2-启动鲜花商店系统
- startFlowerShop();
- }
-
- private static void startFlowerShop() {
- System.out.println("----------------------鲜花商店启动-----------------");
- System.out.println("鲜花信息");
- System.out.println("****************************************************");
- FlowerDao flowerDao = new FlowerDaoImpl();
- List<Flower> flowerList = flowerDao.getAllFlower();
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花品种\t" + "鲜花售价\t" + "");
- for (int i = 0; i < flowerList.size(); i++) {
- Flower flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("****************************************************");
- System.out.print("\n");
- System.out.println("顾客信息");
- FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
- List<FlowerOwner> ownerList = ownerDao.getAllOwner();
- System.out.println("****************************************************");
- System.out.println("序号\t" + "顾客姓名\t");
- for (int i = 0; i < ownerList.size(); i++) {
- FlowerOwner owner = ownerList.get(i);
- System.out.println((i + 1) + "\t" + owner.getName() + "\t");
- }
- System.out.println("****************************************************");
- System.out.print("\n");
- System.out.println("鲜花商店信息");
- System.out.println("****************************************************");
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- List<FlowerStore> storeList = storeDao.getAllStore();
- System.out.println("序号\t" + "鲜花商店名称\t");
- for (int i = 0; i < storeList.size(); i++) {
- FlowerStore store = storeList.get(i);
- System.out.println((i + 1) + "\t" + store.getName() + "\t");
- }
- System.out.println("****************************************************");
- System.out.print("\n");
- //系统登录
- System.out.println("请选择输入登录模式,输入1为顾客登录,输入2为鲜花商店登录");
- boolean type = true;
- String num;
- while (type) {
- num = input.nextLine();
- if ("1".equals(num)) {
- ownerLogin();
- type = false;
- } else if ("2".equals(num)) {
- storeLogin();
- type = false;
- } else {
- System.out.println("输入有误,请按照指定规则输入");
- System.out.println("请选择登录模式,输入1为顾客登录,输入2为鲜花商店登录");
- type = true;
- }
- }
- }
-
- //鲜花商店登录
- private static FlowerStore storeLogin() {
- FlowerStoreService fsService = new FlowerStoreServiceImpl();
- FlowerStore flowerStore = null;
- //TODO-1 鲜花商店登录代码实现
- boolean type = true;
- while (type) {
- System.out.println("请先登录,请输入鲜花商店名字:");
- String storeName = input.nextLine().trim();
- System.out.println("请输入鲜花商店的密码:");
- String storePassword = input.nextLine().trim();
- flowerStore = fsService.login(storeName, storePassword);
- if (null != flowerStore) {
- System.out.println("-------恭喜成功登录-------");
- System.out.println("-------鲜花商店的基本信息:-------");
- System.out.println("名字:" + flowerStore.getName());
- System.out.println("资金:" + flowerStore.getBalance());
- type = false;
- } else {
- System.out.println("登录失败,请确认您的用户名和密码是否正确,重新登录");
- type = true;
- }
- }
-
- System.out.println("您已登录成功,可以进行如下操作");
- StoreChoose(flowerStore);
-
- return flowerStore;
- }
-
- //创建鲜花商店
- private static FlowerStore createFlowerStore(FlowerStore store) {
- FlowerStoreFactory storeFactory = new FlowerStoreFactoryImpl();
- storeFactory.createFlowerStore();
- IsStoreLogOut(store);
-
- return store;//返回的是鲜花商店创建者
- }
-
- private static void IsStoreLogOut(FlowerStore store) {
- System.out.println("您是否继续其它操作若是请输入y,退出请按任意键");
- String code = input.next();
- if (code.equals("y")) {
- StoreChoose(store);
- } else {
- System.out.println("您已成功退出系统");
- }
- }
-
- private static void StoreChoose(FlowerStore store) {
- System.out.println("1:购买鲜花");
- System.out.println("2:卖出鲜花");
- System.out.println("3:培育鲜花");
- System.out.println("4:查询待售鲜花");
- System.out.println("5:查看商店结余");
- System.out.println("6:查看商店账目");
- System.out.println("7:开鲜花商店");
- System.out.println("请根据需要执行的操作,选择序号输入,退出请输入0");
- boolean type = true;
- while (type) {
- int num = input.nextInt();
- switch (num) {
- case 0:
- System.out.println("退出成功");
- type = false;
- break;
- case 1:
- storeBuy(store);
- type = false;
- break;
- case 2:
- storeSell(store);
- type = false;
- break;
- case 3:
- storeBread(store);//传入登录的商店
- type = false;
- break;
- case 4:
- queryFlowerStock(store.getId());
- type = false;
- break;
- case 5:
- queryStoreBalance(store);
- type = false;
- break;
- case 6:
- getAccount(store.getId());
- type = false;
- break;
- case 7:
- createFlowerStore(store);
- type = false;
- break;
- default:
- System.out.println("输入有误,请重新输入");
- type = true;
- break;
- }
- }
- }
-
-
- //获得鲜花商店的资金1代表商店顾客,2顾客卖给商店,3代表顾客之间的交易
- private static FlowerStore getAccount(long id) {
- FlowerStoreService storeService = new FlowerStoreServiceImpl();
- FlowerStore store = storeService.getFlowerStore(id);
- List<Account> list = storeService.account(id);
- for (int i = 0; i < list.size(); i++) {
- Account account = list.get(i);
- String type = null;
- if (1 == account.getDealType()) {
- type = "商店卖给顾客";
- } else if (2 == account.getDealType()) {
- type = "顾客卖给商店";
- } else {
- type = "顾客之间交易";
- }
- System.out.println("第" + (i + 1) + "笔交易,交易类型为:" + type + ",交易金额是:"
- + account.getPrice());
- }
- IsStoreLogOut(store);
- return store;
- }
-
- //查询商店余额
- private static FlowerStore queryStoreBalance(FlowerStore store) {
- double balance = store.getBalance();
- System.out.println(store.getName() + "鲜花商店的结余为:" + balance);
- IsStoreLogOut(store);
- return store;
- }
-
- //查询待售鲜花
- private static FlowerStore queryFlowerStock(long storeId) {
- FlowerStoreService flowerStoreService = new FlowerStoreServiceImpl();
- FlowerStore store = flowerStoreService.getFlowerStore(storeId);
- Flower flower = null;
- List<Flower> flowerList = flowerStoreService.getFlowersInstock(storeId);
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- IsStoreLogOut(store);
- return store;
- }
-
- //鲜花商店培育鲜花
- private static FlowerStore storeBread(FlowerStore store) {
- FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
- System.out.println("请输入要培育鲜花的品种(例如:玫瑰))");
- String flowerType = input.next();
- flowerStore.bread(flowerType, store);
- IsStoreLogOut(store);
- return store;
- }
-
- //以商店的身份登录,商店向顾客出售鲜花
- private static void storeSell(FlowerStore store) {
- FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
- Flower flower = null;
- List<Flower> flowerList = flowerStore.getFlowersInstock(store.getId());
- System.out.println("-------以下是鲜花商店正在出售的鲜花-------");
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("---------请选择要购买的鲜花序号--------");
- boolean type = true;
- while (type) {
- int num = input.nextInt();
- if ((num - 1) < flowerList.size() && (num - 1) >= 0) {
- flower = flowerList.get(num - 1);
- System.out.println("------要卖出的鲜花信息如下------");
- System.out.println("鲜花名称为:" + flower.getName() + " 鲜花类别是:" + flower.getTypeName() + "鲜花价格是:" + flower.getPrice());
- System.out.println("请确认是否卖出,y代表卖出,n代表不卖");
- String code = input.next();
- if (null != code) {
- if ("y".equals(code)) {
- System.out.println("------下面是现有顾客买家,请选择您要卖给买家序号------");
- List<FlowerOwner> ownerList = new ArrayList<FlowerOwner>();
- FlowerOwnerDao ownerDao = new FlowerOwnerDaoImpl();
- ownerList = ownerDao.getAllOwner();
- FlowerOwner flowerOwner = null;
- System.out.println("序号\t" + "顾客姓名\t");
- for (int i = 0; i < ownerList.size(); i++) {
- flowerOwner = ownerList.get(i);
- System.out.println((i + 1) + "\t" + flowerOwner.getName() + "\t");
- }
- num = input.nextInt();
- if ((num - 1) < ownerList.size() && (num - 1) >= 0) {
- flowerOwner = ownerList.get(num - 1);
- }
- flower.setOwnerId(flowerOwner.getId());
- flowerStore.sell(flower);
- } else if ("n".equals(code)) {
- System.out
- .println("--------您选择放弃本次交易,希望您再次光顾----------");
-
- } else {
- System.out.println("--------您的输入有误----------");
- }
- }
- type = true;
- } else {
- System.out.println("输入有误,请按照序号重新输入");
- type = false;
- }
- type = false;// 标识符更改为false,退出系统
- }
- IsStoreLogOut(store);
- }
-
- //以商店的身份登录,商店找顾客购买鲜花
- private static void storeBuy(FlowerStore store) {
- FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
- Flower flower = null;
- List<Flower> flowerList = flowerStore.getFlowerSelling();
- System.out.println("-------以下是顾客正在出售的鲜花-------");
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
- int num = input.nextInt();
- flower = flowerList.get(num - 1);
- //System.out.println("选中的鲜花信息"+flower.getName()+"id"+flower.getId());
- flowerStore.buy(flower, store);
- IsStoreLogOut(store);
- }
-
- //顾客登录
- private static FlowerOwner ownerLogin() {
- System.out.println("请先登录,请您输入姓名:");
- String ownerName = input.nextLine().trim();
- System.out.println("请您输入密码:");
- String ownerPassword = input.nextLine().trim();
- FlowerOwnerService flowerOwner = new FlowerOwnerServiceImpl();
- FlowerOwner Owner = flowerOwner.login(ownerName, ownerPassword);
- boolean reg = true;
- while (reg) {
- if (null == Owner) {
- System.out.println("登录失败,请确认您的用户名和密码后重新输入");
- System.out.println("请先登录,请您输入姓名:");
- ownerName = input.nextLine().trim();
- System.out.println("请您输入密码:");
- ownerPassword = input.nextLine().trim();
- Owner = flowerOwner.login(ownerName, ownerPassword);
- reg = true;
- } else {
- reg = false;
- System.out.println("登录成功,您可以购买和卖出鲜花,如果您想购买鲜花请输入1,如果想卖出鲜花请输入2");
- System.out.println("1:购买鲜花");
- System.out.println("2:卖出鲜花");
- boolean type = true;
- //----------------------
- FlowerCirculate(type, Owner);
- }
- }
- return Owner;
- }
-
- private static boolean FlowerCirculate(boolean type, FlowerOwner owner) {
- while (type) {
- int num = input.nextInt();
- if (1 == num) {
- ownerBuy(owner);
- type = false;
- } else if (2 == num) {
- ownerSell(owner);
- type = false;
- } else {
- System.out.println("输入有误,请重新输入");
- type = true;
- }
- }
- return type;
- }
-
- //顾客向商店卖鲜花
- private static void ownerSell(FlowerOwner flowerowner) {
- FlowerOwnerService owner = new FlowerOwnerServiceImpl();
- System.out.println("---------我的鲜花列表--------");
- List<Flower> flowerList = owner.getMyFlower(flowerowner.getId());
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格");
- for (int i = 0; i < flowerList.size(); i++) {
- Flower flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("---------请选择要出售的鲜花序号--------");
- boolean type = true;
- while (type) {
- int num = input.nextInt();
- if ((num - 1) < flowerList.size() && (num - 1) >= 0) {
- Flower flower = flowerList.get(num - 1);
- System.out.println("------您要卖出的鲜花信息如下------");
- System.out.println("鲜花名字为:" + flower.getName() + "鲜花类别是:"
- + flower.getTypeName() + "鲜花价格为" + flower.getPrice());
-
- boolean again = true;
- while (again)//y,n的循环
- {
- System.out.println("请确认是否卖出,Y代表卖出,N代表不卖");
- String code = input.next();
-
- if (null != code) {
- if ("Y".equals(code)) {
- System.out.println("------下面是现有鲜花商店,请选择您要卖的商店序号------");
- List<FlowerStore> storeList = new ArrayList<FlowerStore>();
- //TODO-1 通过 Dao层查询所有的鲜花商店 您已成功卖出鲜花
-
- FlowerStoreDao storeDao = new FlowerStoreDaoImpl();
- storeList = storeDao.getAllStore();
-
- FlowerStore flowerStore = null;
- System.out.println("序号\t" + "鲜花商店名字\t");
- for (int i = 0; i < storeList.size(); i++) {
- flowerStore = storeList.get(i);
- System.out.println((i + 1) + "\t" + flowerStore.getName() + "\t");
- }
- num = input.nextInt();
- if ((num - 1) < storeList.size() && (num - 1) >= 0) {
- flowerStore = storeList.get(num - 1);
- }
- //TODO-2 要销售的鲜花设置商店 id
- flower.setStoreId(num);
-
- //TODO-3 在 service 层顾客卖出该鲜花 Flower
- owner.sell(flower);
-
-
- again = false;//退出 y,n的循环
- } else if ("N".equals(code)) {
- System.out.println("--------您选择放弃本次交易,希望您再次光顾----------");
- again = false;
- } else {
- System.out.println("--------您的输入有误,请按照上诉要求输入----------");
- again = true;
- }
- }
- }//y,n循环结束
- } else {
- System.out.println("输入有误,请按照序号重新输入");
- type = true;//重新循环一次
- }
- type = false;
- }
- boolean isLogOut = true;
- System.out.println("您是否要继续进行其它操作,若是请输入Y,否则退出系统");
- String flag = input.next();
- if (flag.equals("Y")) {
- System.out.println("您可以购买和卖出鲜花,如果您想购买鲜花请输入1,如果想卖出鲜花请输入2");
- System.out.println("1:购买鲜花");
- System.out.println("2:卖出鲜花");
- //循环
- FlowerCirculate(isLogOut, flowerowner);
- } else {
- System.out.println("您已成功退出系统");
- }
- }
-
- //顾客找鲜花商店买鲜花
- private static void ownerBuy(FlowerOwner flowerowner) {
- System.out.println("-------请输入选择要购买范围:只需要按照下文要求输入选择项的序号即可--------");
- System.out.println("1:购买库存鲜花");
- System.out.println("2:购买新培育鲜花");
- FlowerStoreService flowerStore = new FlowerStoreServiceImpl();
- FlowerOwnerService flowerOwner = new FlowerOwnerServiceImpl();
- Flower flower = null;
- int num = input.nextInt();
- List<Flower> flowerList = null;
-
- // num为1时购买库存鲜花
- boolean type = true;
- while (type) {
- if (num == 1) {
- System.out.println(num + "库存鲜花: -------以下是库存鲜花-------");
- //通过服务层接口查询库存鲜花
- flowerList = flowerStore.getFlowersInstock(0);
-
- //显示所有库存鲜花
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
- num = input.nextInt();
- flower = flowerList.get(num - 1);
- flower.setOwnerId(flowerowner.getId());
- flowerOwner.FlowerOwnerbuy(flower);
- type = false;
- } else if (num == 2) { // num为2时购买新培育鲜花
- System.out.println(num + "培育鲜花: -------以下是库存鲜花-------");
- System.out.println("序号\t" + "鲜花名称\t" + "鲜花类型\t" + "鲜花价格\t");
- //TODO-1 通过服务层接口查询所有培育鲜花
- flowerList = flowerStore.getFlowersBread();
-
- //显示所有培育鲜花
- for (int i = 0; i < flowerList.size(); i++) {
- flower = flowerList.get(i);
- System.out.println((i + 1) + "\t" + flower.getName() + "\t" + flower.getTypeName() + "\t" + flower.getPrice() + "\t");
- }
- System.out.println("-------请选择要购买哪一种鲜花,并输入选择项的序号-------");
- String count = input.next();
- if (count.matches("[0-9]*")) {
- num = Integer.parseInt(count);
- flower = flowerList.get(num - 1);
- flower.setOwnerId(flowerowner.getId());
- flowerOwner.FlowerOwnerbuy(flower);
- }
- type = false;
- } else {
- System.out.println("您的输入有误,请按照上诉提示输入");
- type = true;
- }
-
- }
- boolean isLogOut = true;
- System.out.println("您是否要继续进行其它操作,若是请输入Y,否则输入任意字母退出系统");
- String flag = input.next();
- if (flag.equals("Y")) {
- System.out.println("您可以购买和卖出鲜花,如果您想购买鲜花请输入1,如果想卖出鲜花请输入2");
- System.out.println("1:购买鲜花");
- System.out.println("2:卖出鲜花");
- //循环
- FlowerCirculate(isLogOut, flowerowner);
- } else {
- System.out.println("您已成功退出系统");
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。