Professional Documents
Culture Documents
Package Com - Jav1
Package Com - Jav1
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
* @author com.javayihao.top
*/
/*
*/
String isGo="y";
do{
showMenu();
//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
switch (select) {
case "1":
insertGood();
break;
case "2":
//existence
if(good!=null){
System.out.println("Commodity number:"+goodId+"
Trade name:"+good.getName()
break;
case "3":
getGoodList();
break;
case "4":
buyGood();
break;
case "5":
int id = input.nextInt();
if(searchGoodById(id)!=null){
deleteGood(id);
}else{
break;
case "6":
updateGood();
break;
//Exit system
case "0":
//Termination procedure
System.exit(0);
default:
continue;
isGo = input.next();
}while(isGo.equals("y"));
/**
* 1.First query whether the current product inventory to be updated does not exist
*/
try {
pst.setString(1, name);
pst.setFloat(2, price);
pst.setInt(3, num);
pst.setInt(4, gid);
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("1.Goods warehousing");
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
*/
private void deleteGood(int id) {
try {
pst.setInt(1, id);
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;
while(true){
id= input.nextInt();
if(searchGoodById(id)==null){
break;
//To execute the sql statement, use placeholders here to prevent sql intrusion
try {
pst.setInt(1, id);
pst.setString(2, name);
pst.setFloat(3, price);
pst.setInt(4, num);
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
*/
try {
pst.setInt(1, id);
ResultSet rs = pst.executeQuery();
rs.getFloat("price"), rs.getInt("num"));
return good;
//Close connection
DbUtil.close(con, pst);
} catch (SQLException e) {
e.printStackTrace();
return null;
//List of commodities
try {
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
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
int id = input.nextInt();
if(good!=null){
if(good.getNum()<num){
}else{
try{
pst.setInt(2, id);
if(pst.executeUpdate()==1){
Good g = new
Good(id,good.getName(),good.getPrice(),num);
if(goods.size()>0){
goods.get(0).setNum(num+goods.get(0).getNum());
goods.add(g);
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());
flag = input.next();
if(!flag.equals("y")){
//Settlement
account(goods);
}else{
}while(flag.equals("y"));
//Checkout cart
System.out.println("number\t"+"Name\t"+"Number\t"+"Total price");
goods.forEach(in->System.out.println(in.getId()+"\t"+in.getName()+
"\t"+in.getNum()+"\t"+in.getNum()*in.getPrice()));
//Total
float sum=0;
sum += (goods.get(i).getNum())*(goods.get(i).getPrice());
System.out.println("Aggregate consumption:"+sum+"element");