You are on page 1of 14

package com.javayihao.top.

service;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.Scanner;

import com.javayihao.top.pojo.Good;

import com.javayihao.top.utils.DbUtil;

/**

* @date 2019-12-9

* @Description System main interface

* @author com.javayihao.top

*/

public class ShopView {

//Get keyboard input object

Scanner input = new Scanner(System.in);

/*

* System operation method

*/

public void ShopStart() {

System.out.println("=================Welcome to the supermarket shopping


management system=================");
//Flag quantity whether to continue, default is

String isGo="y";

do{

//Call the function shown in the menu

showMenu();

System.out.println("Please enter the action to be performed");

//Accept the input from the keyboard. Use String here to process the
number and character input at one time. It is not recommended to use int type numbers

String select =input.next();

//Execute the corresponding method according to the input selection

switch (select) {

//Execute goods warehousing method

case "1":

insertGood();

break;

//Execute commodity query method

case "2":

System.out.println("Enter the item number to query");

int goodId = input.nextInt();

//Call the method to query the product,

Good good = searchGoodById(goodId);

//existence

if(good!=null){

System.out.println("Commodity number:"+goodId+"
Trade name:"+good.getName()

+" commodity price:"+good.getPrice()+" Quantity of


commodities:"+good.getNum());
}else{

System.out.println("This product does not exist");

break;

//Execute product list method

case "3":

getGoodList();

break;

//Execute commodity purchase method

case "4":

buyGood();

break;

//Execute commodity purchase method

case "5":

System.out.println("Enter item number to delete");

int id = input.nextInt();

//Call the method to query the product,

if(searchGoodById(id)!=null){

deleteGood(id);

}else{

System.out.println("There is no such product");

break;

case "6":

updateGood();
break;

//Exit system

case "0":

System.out.println("*************Welcome to the next use


bye!*************");

//Termination procedure

System.exit(0);

default:

System.err.println("Wrong input, please input again!");

continue;

System.out.println("input y Continue/Otherwise quit");

isGo = input.next();

}while(isGo.equals("y"));

System.out.println("*************Welcome to the next use bye!*************");

/**

* Update product action

* 1.First query whether the current product inventory to be updated does not exist

* 2.If there is an update, there is no prompt

*/

private void updateGood() {

System.out.println("Enter the item to modify id");

int gid = input.nextInt();

Good good = searchGoodById(gid);

System.out.println("The product information is as follows");


if(good!=null){

System.out.println("Commodity number:"+gid+" Trade name:"+good.getName()

+" commodity price:"+good.getPrice()+" Quantity of


commodities:"+good.getNum());

System.out.println("Modify product name");

String name = input.next();

System.out.println("Modify commodity unit price");

float price = input.nextFloat();

System.out.println("Modify commodity inventory");

int num = input.nextInt();

String sql="update t_good set name=?,price=?,num=? where id=? ";

try {

//Create an object to operate the database

Connection con = DbUtil.getConnection();

//Create an execution object PreparedStatement to execute sql

PreparedStatement pst = con.prepareStatement(sql);

//Set values for placeholders

pst.setString(1, name);

pst.setFloat(2, price);

pst.setInt(3, num);

pst.setInt(4, gid);

//execute() returns true if it is a query or false if it is an update or insert

if(!pst.execute()){

System.out.println("Update success");

//Close connection
DbUtil.close(con, pst);

} catch (Exception e) {

e.printStackTrace();

System.out.println("Update exception"+e.getMessage());

}else{

System.out.println("This product does not exist");

//Method for displaying system interface menu

private void showMenu() {

System.out.println("1.Goods warehousing");

System.out.println("2.Query products according to product number");

System.out.println("3.List of commodities");

System.out.println("4.Purchase goods");

System.out.println("5.Delete merchandise");

System.out.println("6.Update commodity");

System.out.println("0.Exit system");

/*

* Delete merchandise

* 1.First of all, we have to judge the existence of the commodity

* 2.Delete according to item id

*/
private void deleteGood(int id) {

String sql = "delete from t_good where id=?";

try {

//Create an object to operate the database

Connection con = DbUtil.getConnection();

//Create an execution object PreparedStatement to execute sql

PreparedStatement pst = con.prepareStatement(sql);

//Set values for placeholders

pst.setInt(1, id);

//execute() returns true if it is a query or false if it is an update or insert

if(!pst.execute()){

System.out.println("Delete successful");

//Close connection

DbUtil.close(con, pst);

} catch (Exception e) {

e.printStackTrace();

System.out.println("Delete exception"+e.getMessage());

/*

* Goods warehousing

* Here we only deal with the logic that the number cannot be repeated,

* As for whether the input number is a number, there is no judgment here. Interested friends
can try it

*/
private void insertGood(){

//Commodity number

int id=0;

System.out.println("Enter item number");

while(true){

id= input.nextInt();

//Judge whether the number entered is duplicate or not, and re-enter it


repeatedly

if(searchGoodById(id)==null){

break;

System.err.println("Duplicate number, please re-enter product number");

System.out.println("Enter product name");

String name = input.next();

System.out.println("Enter unit price of goods");

float price = input.nextFloat();

System.out.println("Enter item quantity");

int num = input.nextInt();

//To execute the sql statement, use placeholders here to prevent sql intrusion

String sql = "insert into t_good()values(?,?,?,?)";

try {

//Create an object to operate the database

Connection con = DbUtil.getConnection();

//Create an execution object PreparedStatement to execute sql

PreparedStatement pst = con.prepareStatement(sql);


//Set values for placeholders

pst.setInt(1, id);

pst.setString(2, name);

pst.setFloat(3, price);

pst.setInt(4, num);

//Execute sql statement

if(!pst.execute()){

System.out.println("Warehousing success");

//Close connection

DbUtil.close(con, pst);

} catch (Exception e) {

e.printStackTrace();

System.out.println("Abnormal storage"+e.getMessage());

/*Commodity inquiry

* The returned object is a product object. If there is no such product, null will be returned

*/

private Good searchGoodById(int id) {

//Executed sql statement

String sql="select id,name,price,num from t_good where id=?";

try {

//Create an object to operate the database

Connection con = DbUtil.getConnection();


//Create an object PreparedStatement to execute sql

PreparedStatement pst = con.prepareStatement(sql);

pst.setInt(1, id);

ResultSet rs = pst.executeQuery();

if(rs.next()){//As a result, the queried data is encapsulated into a commodity


object through the constructor

Good good = new Good(rs.getInt("id"), rs.getString("name"),

rs.getFloat("price"), rs.getInt("num"));

return good;

//Close connection

DbUtil.close(con, pst);

} catch (SQLException e) {

e.printStackTrace();

return null;

//List of commodities

private void getGoodList(){

//Executed sql statement

String sql="select id,name,price,num from t_good";

try {

//Create an object to operate the database

Connection con = DbUtil.getConnection();

//Create an object PreparedStatement to execute sql

PreparedStatement pst = con.prepareStatement(sql);


ResultSet rs = pst.executeQuery();

System.out.println("number\t"+"Name\t"+"Unit
Price\t"+"Number\t");

if(rs.wasNull()){

System.out.println("No goods");

}else{

while(rs.next()){//Results, printing

//Through rs.getxxx("yy") method parameter is


the database column name

System.out.println(rs.getInt("id")
+"\t"+rs.getString("name")+"\t"+

rs.getFloat("price")
+"\t"+rs.getInt("num")+"\t");

//Close connection

DbUtil.close(con, pst);

} catch (SQLException e) {

e.printStackTrace();

//Purchase goods

public void buyGood() {

//Collection used to store purchased items

ArrayList<Good> goods = new ArrayList<>();

//Do you want to continue to purchase the tag

String flag = "y";


do{

System.out.println("Enter the purchase item number");

int id = input.nextInt();

Good good = searchGoodById(id);

if(good!=null){

System.out.println("Enter quantity of goods purchased");

int num = input.nextInt();

if(good.getNum()<num){

System.out.println("This product inventory"+good.getNum()+"


Unable to purchase;");

}else{

try{

String sql="update t_good set num=? where id=?";

//Create an object to operate the database

Connection con = DbUtil.getConnection();

//Create an object PreparedStatement to execute sql

PreparedStatement pst = con.prepareStatement(sql);

pst.setInt(1, good.getNum()-num);//Update stock

pst.setInt(2, id);

if(pst.executeUpdate()==1){

//Set goods stored in the purchased goods

Good g = new
Good(id,good.getName(),good.getPrice(),num);

if(goods.size()>0){

for (int i = 0; i < goods.size(); i++) {


if(goods.get(i).getId()==id){//If
the shopping cart has the product quantity plus

goods.get(0).setNum(num+goods.get(0).getNum());

}else{//If there is no quantity


plus in the shopping cart

goods.add(g);

}else{//There is no item in the shopping cart,


add it to the shopping cart

goods.add(g);

System.out.println("Purchase success");

}else{

System.out.println("Purchase failure");

}catch(Exception e){

e.printStackTrace();

System.out.println("Purchase
exception"+e.getMessage());

System.out.println("input y Continue to buy/Enter other settlement");

flag = input.next();

if(!flag.equals("y")){

//Settlement
account(goods);

}else{

System.out.println("There is no such product");

}while(flag.equals("y"));

//Checkout cart

private void account(ArrayList<Good> goods) {

System.out.println("number\t"+"Name\t"+"Number\t"+"Total price");

//lambda expression traverses the collection. Of course, it is ok to use for loop

goods.forEach(in->System.out.println(in.getId()+"\t"+in.getName()+

"\t"+in.getNum()+"\t"+in.getNum()*in.getPrice()));

//Total

float sum=0;

for (int i = 0; i < goods.size(); i++) {

//Sum total price

sum += (goods.get(i).getNum())*(goods.get(i).getPrice());

System.out.println("Aggregate consumption:"+sum+"element");

You might also like