java專案實戰(1)—基礎——超市購物管理系統V1.0

愛吃魚的荔果果發表於2020-09-24

目錄

1.前言

2.專案基礎:

3.專案實現內容:

4.程式碼介紹:

1、MainPage.java

2、GoodsDao.java:

3、SalesManDao.java

4、MysqlData.java



1.前言

記錄一下我的第一個java實戰小專案,零零星星的一個星期完成了(2020.9.16—9.24)。但是好像沒有整體框架的思維,還有好多程式碼實現的方法沒有達到最優,這樣做專案估計會被大佬罵吧,如有大佬無意間看到,還請批評指正,提出建議。   

菜鳥很菜,繼續學習呀!

2.專案基礎:

  1. JDBC基礎。學會使用jdbc連線資料庫,訪問資料庫,修改資料庫。(本專案使用mysql)
  2. 基礎的sql語句,查詢,修改,刪除,真的很基礎。
  3. java基礎(我的java真的太基礎了,好多還沒學會)。

3.專案實現內容:

 

4.程式碼介紹:

程式碼簡單的分為四個類,以下的程式碼能全部實現,且沒有bug(自己測過很多次沒發現bug)。

MainPage.java:  實現主

介面的功能

GoodsDao.java:  實現與商品有關的操作

SalesManDao.java:  實現與售貨員有關的操作

MysqlData.java:  實現資料庫的操作

1、MainPage.java

import java.util.Scanner;

public class MainPage {

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        Boolean flag = true;

        while (flag){
            System.out.println("**********************************");
            System.out.println("           1.商品維護");
            System.out.println("           2.前臺收銀");
            System.out.println("           3.商品管理");
            System.out.println("**********************************");
            System.out.println("請選擇,輸入數字或者按0退出:");
            int select = 0;
            while (true){
                try{
                    select = Integer.valueOf(s.nextLine());
                    break;
                }catch (Exception e){
                    System.out.println("輸入錯誤!");
                }
            }

            switch (select){
                case 1:
                    GoodMaintenance();
                    break;
                case 2:
                    QianTaiLogin();
                    break;
                case 3:
                    GoodsMagLogin();
                    break;
                case 0:
                    flag = false;
                    System.out.println("已退出系統!");
                    break;
                default:
                    System.out.println("請輸入正確的數字!");
            }
        }
    }

    /****************part1*****************/
    public static void GoodMaintenance(){
        System.out.println("執行顯示商品維護選單");
        System.out.println("商超購物管理系統>>商品維護");
        Scanner s = new Scanner(System.in);

        GoodsDao operate = new GoodsDao();

        while (true){
            System.out.println("**********************************");
            System.out.println("          1.新增商品");
            System.out.println("          2.更改商品");
            System.out.println("          3.刪除商品");
            System.out.println("          4.顯示所有商品");
            System.out.println("          5.查詢商品");
            System.out.println("**********************************");
            System.out.println("請選擇,輸入數字或按0返回上一級選單:");
            int select = 0;

            while (true){
                try{
                    select = Integer.valueOf(s.nextLine());
                    break;
                }catch (Exception e){
                    System.out.println("輸入錯誤!");
                }
            }

            if (select == 1){
                operate.addGoods();
            } else if (select == 2){
                operate.alterGoods();
            } else if (select == 3){
                operate.deleteGoods();
            } else if (select == 4){
                operate.showGoods(1);
            } else if (select == 5){
                operate.quaryGoods();
            } else if (select == 0){
                //flag = false;
                break;
            } else {
                System.out.println("請輸入正確的數字!");
            }
        }
        System.out.println("已退出<商品維護>,返回上一級選單。");
    }


    /****************part2*****************/
    //登入介面
    public static void QianTaiLogin(){
        Scanner s = new Scanner(System.in);
        MysqlData md = new MysqlData();
        GoodsDao gd = new GoodsDao();
        int select = 3;

        while (true) {
            System.out.println("**********************************");
            System.out.println("       歡迎使用bala超市購物管理系統");
            System.out.println("       1.登入系統");
            System.out.println("       2.退出");
            System.out.println("**********************************");
            System.out.print("請選擇,輸入數字:");

            while (true){
                try{
                    select = Integer.valueOf(s.nextLine());
                    break;
                }catch (Exception e){
                    System.out.println("輸入錯誤!");
                }
            }

            //System.out.println("select:" + select);
            if(select == 1){
                int i;
                for(i = 2; i >= 0; i--){

                    System.out.print("請輸入使用者名稱:");
                    String name = s.nextLine();
                    System.out.print("請輸入密碼:");
                    String pass = s.nextLine();

                    //檢查使用者密碼,count=1,正確,count=0,使用者名稱密碼錯誤。
                    int[] arr = md.quaryData(name,pass,1);
                    //返回售貨員ID
                    int count = arr[0];
                    int sid = arr[1];

                    if(count == 1){
                        //登入成功
                        System.out.println("登入成功");
                        gd.shopping(sid);
                        break;
                    }else {
                        System.out.println("使用者名稱和密碼不匹配!");
                        if(i != 0)
                            System.out.println("您還有"+i+"次登入機會,請重新輸入:");
                        if(i == 0)
                            System.out.println("密碼錯誤次數超過3次,已退出!");
                    }
                }

            }else if(select == 2){
                break;
            }else {
                System.out.println("輸入錯誤!");
            }
        }
    }

    /****************part3*****************/
    public static void GoodsMagLogin(){
        System.out.println("執行商品管理!");
        System.out.println();
        System.out.println("商超購物管理系統>>商品管理");
        GoodsDao gd = new GoodsDao();
        SalesManDao sm = new SalesManDao();
        Scanner s = new Scanner(System.in);
        boolean flag = true; //退出當前系統標誌

        while (flag){
            System.out.println("***************************************");
            System.out.println("           1、列出當日賣出商品列表");
            System.out.println("           2、售貨員管理");
            System.out.println("***************************************");
            System.out.println("請選擇,輸入數字或者按0返回上一級選單:");
            int select = 0;

            while (true){
                try{
                    select = Integer.valueOf(s.nextLine());
                    break;
                }catch (Exception e){
                    System.out.println("輸入錯誤!");
                }
            }

            if (select == 1){
                gd.showTodaySales();
            } else if (select == 2){
                sm.jieMian();
            } else if (select == 0){
                flag = false;
            } else {
                System.out.println("輸入錯誤!");
            }
        }
        System.out.println("已退出<商品管理>,返回上一級選單。");
    }
}

2、GoodsDao.java:

import java.util.Scanner;

public class GoodsDao {
    //goods
    private String gName;
    private float gPrice;
    private int gNum;

    //goods sales
    java.util.Date date = new java.util.Date();
    java.sql.Date sData = new java.sql.Date(date.getTime());

    public String getgName() {
        return gName;
    }

    public void setgName(String gName) {
        this.gName = gName;
    }

    public float getgPrice() {
        return gPrice;
    }

    public void setgPrice(float gPrice) {
        this.gPrice = gPrice;
    }

    public int getgNum() {
        return gNum;
    }

    public void setgNum(int gNum) {
        this.gNum = gNum;
    }


    //新增商品
    public void addGoods(){

        MysqlData igd = new MysqlData();
        System.out.println("執行新增商品操作:");
        Scanner s = new Scanner(System.in);

        char flag = 'y';
        while (flag == 'y'){

            while (true){
                System.out.println("新增商品名稱:");//不能為空

                this.setgName(s.nextLine());
                if(this.getgName().equals(""))  //or this.getgName().length() == 0
                    System.out.println("輸入不能為空!");
                else
                    break;
            }
            while (true){
                System.out.println("新增商品價格:");
                try{
                    this.setgPrice(s.nextFloat());
                    break;
                }catch (Exception e){
                    System.out.println("輸入錯誤!");
                    String huanchong = s.next();
                }
            }
            while (true){
                System.out.println("新增商品數量:");
                try{
                    this.setgNum(s.nextInt());
                    break;
                }catch (Exception e){
                    System.out.println("輸入錯誤!");
                    String huanchong = s.next();
                }
            }
            System.out.println("輸入的商品資訊為:");
            System.out.printf(this.getgName() + "  " + this.getgPrice() + "   " + this.getgNum() + "\n");

            //存入資料庫
            igd.InsertData(this.getgName(),this.getgPrice(),this.getgNum());

            //繼續新增商品
            System.out.print("是否繼續商品(y/n): ");

            while (true){
                try{
                    flag = s.nextLine().charAt(0);
                }catch (Exception e){
                    System.out.println("輸入不能為空!");
                }
                if(flag == 'n' || flag == 'y')
                    break;
                else
                    System.out.println("輸入錯誤!請重新輸入:");
            }
        }
    }

    //更改商品,有個Scanner第二次迴圈,輸入為空的問題沒想明白
    public void alterGoods(){
        MysqlData md = new MysqlData();
        System.out.println("執行更改商品操作");

        //為什麼在外面定義scann類,在while迴圈裡,迴圈第二次的時候預設是空的,不能從鍵盤輸入?
        //Scanner s = new Scanner(System.in);
        //String alterName = "";

        char flag = 'y';
        while (flag == 'y'){

            System.out.println("輸入更改商品名稱:");
            Scanner s = new Scanner(System.in);

            String alterName = s.nextLine();

            int count = md.quaryData(alterName,"acc_goods");
            System.out.println("查詢商品返回的值:" + count);

            if(count > 0){
                System.out.println("選擇你要更改的內容:");
                System.out.println("1、更改商品名稱");
                System.out.println("2、更改商品價格");
                System.out.println("3、更改商品數量");
                int select = 0;

                while (true){
                    try{
                        select = Integer.valueOf(s.nextLine());
                        break;
                    }catch (Exception e){
                        System.out.println("輸入錯誤!");
                    }
                }

                if(select == 1){

                    while (true){
                        System.out.println("請輸入要更改商品名稱:");//不能為空
                        try{
                            this.setgName(s.nextLine());
                            if(this.getgName().equals("")) //or this.getgName().length() == 0
                                System.out.println("輸入不能為空!");
                            else {
                                md.alterData(alterName, this.getgName(), 1);
                                break;
                            }
                        }catch (Exception e){
                            System.out.println("輸入錯誤!");
                        }
                    }

                }else if(select == 2){
                    System.out.println("請輸入要更改商品價格");
                    float alprice;
                    while (true){
                        try{
                            alprice = Float.valueOf(s.nextLine());
                            break;
                        }catch (Exception e){
                            System.out.println("輸入錯誤!");
                        }
                    }
                    md.alterData(alterName,alprice);

                }else if(select ==3){
                    System.out.println("請輸入要更改商品數量");
                    int alnum;
                    while (true){
                        try{
                            alnum = Integer.valueOf(s.nextLine());
                            break;
                        }catch (Exception e){
                            System.out.println("輸入錯誤!");
                        }
                    }
                    md.alterData(alterName,alnum,1);

                }else {
                    System.out.println("輸入錯誤!");
                }
            }


            while (true){
                System.out.println("是否繼續(y/n)");
                try{
                    flag = s.nextLine().charAt(0);
                }catch (Exception e){
                    System.out.println("輸入不能為空!");
                }
                if(flag == 'n' || flag == 'y')
                    break;
                else
                    System.out.println("輸入錯誤!請重新輸入:");
            }

        }
    }

    //刪除商品
    public void deleteGoods(){
        System.out.println("執行刪除商品操作!");

        MysqlData igd = new MysqlData();
        char flag = 'y';

        while(flag == 'y'){
            System.out.println("輸入刪除的商品名稱:");
            Scanner s = new Scanner(System.in);
            String delname = s.nextLine();
            //按照名字檢查商品是否存在
            int count = igd.quaryData(delname,"acc_goods");

            if(count > 0){
                System.out.println("是否確定要刪除(y/n)?:");
                if(s.next().charAt(0) == 'y'){
                    //執行刪除商品
                    igd.deleteData(delname,1);
                }else {
                    System.out.println("未刪除。");
                }
            }
            System.out.println("是否繼續(y/n):");

            while (true){
                try{
                    flag = s.nextLine().charAt(0);
                }catch (Exception e){
                    System.out.println("輸入不能為空!");
                }
                if(flag == 'n' || flag == 'y')
                    break;
                else
                    System.out.println("輸入錯誤!請重新輸入:");
            }

        }
    }

    //顯示所有商品資訊
    public void showGoods(int method){
        System.out.println("顯示所有商品");
        MysqlData md = new MysqlData();
        md.showData(method);
    }

    public void quaryGoods(){
        System.out.println("執行查詢商品操作!");
        MysqlData md = new MysqlData();
        Scanner s = new Scanner(System.in);
        char flag = 'y';

        while(flag == 'y'){
            System.out.println("1、按商品價格升序查詢");
            System.out.println("2、按商品數量升序查詢");
            System.out.println("3、輸入關鍵字查詢商品");
            System.out.println("請選擇,輸入數字或0返回上一級選單:");
            int select = 0;

            while (true){
                try{
                    select = Integer.valueOf(s.nextLine());
                    break;
                }catch (Exception e){
                    System.out.println("輸入錯誤!");
                }
            }

            if(select == 1){
                md.showData(2);
            }else if(select == 2){
                md.showData(3);
            }else if(select == 3){
                System.out.println("請輸入商品關鍵字:");
                Scanner sc = new Scanner(System.in);
                String name = sc.nextLine();
                md.quaryData(name,"inacc_goods"); //按照模糊方法查詢商品是否存在
            }else if(select == 0){
                break;
            }
            else {
                System.out.println("輸入錯誤!");
            }

            System.out.println("是否繼續(y/n):");
            while (true){
                try{
                    flag = s.nextLine().charAt(0);
                }catch (Exception e){
                    System.out.println("輸入不能為空!");
                }
                if(flag == 'n' || flag == 'y')
                    break;
                else
                    System.out.println("輸入錯誤!請重新輸入:");
            }
        }
    }

    //購物結算
    public void shopping(int sid){
        System.out.println("                       1.購物結算");
        Scanner sc = new Scanner(System.in);
        MysqlData md = new MysqlData();
        float[] arr = {0,0};
        int gid = 0;
        float gprice = 0;
        int gnum = 0;
        float money = 0;
        int num = 0;

        System.out.println("輸入商品關鍵字:");

        while(true){
            String name = sc.nextLine();
            int count = md.quaryData(name, "inacc_goods");
            if(count == 1){
                while (true){
                    System.out.print("請選擇商品:");
                    String selname = sc.nextLine();
                    //有輸入就會有錯誤!
                    int count2 = md.quaryData(selname,"acc_goods");
                    //返回商品ID,價格,數量
                    if(count2 == 1){
                        arr = md.quaryData(selname);
                        gid = (int)arr[0];
                        gprice = arr[1];
                        gnum = (int)arr[2];

                        while (true){
                            System.out.print("請輸入購買數量:");
                            try{
                                num = Integer.valueOf(sc.nextLine());
                                if(num >= 0 & num <= gnum)
                                    break;
                            }catch (Exception e){
                                System.out.println("輸入錯誤!");
                            }
                            if(num > gnum)
                                System.out.println("庫存不足!請調整購買數量!");
                        }

                        money = gprice * num;
                        System.out.printf("總價:%.1f",money);
                        System.out.println();
                        System.out.println("請輸入實際交費金額:");
                        while (true){
                            try{
                                int jiaofei = Integer.valueOf(sc.nextLine());
                                if(jiaofei < 0){
                                    System.out.println("輸入錯誤!");
                                }else if(jiaofei < money){
                                    System.out.println("您的錢不夠買呀!請重新輸入:");
                                }else {
                                    System.out.println("找錢:" + (jiaofei - money));
                                    break;
                                }
                            }catch (Exception e){
                                System.out.println("輸入錯誤!");
                            }
                        }
                        System.out.println("謝謝光臨!");

                        //修改資料庫商品數量
                        int alternum = (int) (gnum - num);
                        md.alterData(selname,alternum,2);
                        //將sid,gid,sdate,snum 資訊存到gsales表中
                        md.insertGsales(gid,sid,this.sData,num);
                        break;
                    }
                }
                break;
            }else {
                System.out.println("請重新輸入商品關鍵字!");
            }
        }
    }

    //列出當日賣出商品列表
    public void showTodaySales(){
        System.out.println("執行列出當日賣出商品列表操作!");
        System.out.println("今日售出商品:");
        MysqlData md = new MysqlData();
        md.showgoodsales(this.sData);
    }
}

3、SalesManDao.java

import java.util.Scanner;

public class SalesManDao {
    private String sName;
    private String sPass;

    public String getsPass() {
        return sPass;
    }

    public void setsPass(String sPass) {
        this.sPass = sPass;
    }

    public String getsName() {
        return sName;
    }

    public void setsName(String sName) {
        this.sName = sName;
    }

    public void jieMian(){
        System.out.println("商超購物管理系統>>商品管理>>售貨員管理");
        Scanner s = new Scanner(System.in);
        while (true) {
            System.out.println("*****************************************");
            System.out.println("              1、新增售貨員");
            System.out.println("              2、更改售貨員");
            System.out.println("              3、刪除售貨員");
            System.out.println("              4、顯示所有售貨員");
            System.out.println("              5、查詢售貨員");
            System.out.println("*****************************************");
            System.out.println("請選擇,輸入數字或者按0返回上一級選單:");
            int select = 0;
            while (true){
                try{
                    select = Integer.valueOf(s.nextLine());
                    break;
                }catch (Exception e){
                    System.out.println("輸入錯誤!");
                }
            }

            if (select == 1) {
                this.addSalesMan();
            } else if (select == 2) {
                this.alterSalesMan();
            } else if (select == 3) {
                this.deleteSalesMan();
            } else if (select == 4) {
                this.showSalesMan();
            } else if (select == 5) {
                this.quarySalesMan();
            } else if (select == 0) {
                break;
            } else {
                System.out.println("輸入錯誤!");
            }
        }
    }

    //新增售貨員資訊
    public void addSalesMan(){
        MysqlData md = new MysqlData();
        System.out.println("執行新增售貨員操作:");
        Scanner s = new Scanner(System.in);
        char flag = 'y';
        while (true){
            System.out.println("新增售貨員姓名:");
            while (true){
                this.setsName(s.nextLine());
                if(this.getsName().equals(""))
                    System.out.println("輸入不能為空!");
                else
                    break;
            }

            System.out.println("新增售貨員密碼:");
            while (true){
                this.setsPass(s.nextLine());
                if(this.getsPass().equals(""))
                    System.out.println("輸入不能為空!");
                else
                    break;
            }
            //存入資料庫
            md.InsertData(this.getsName(),this.getsPass());
            System.out.println("是否繼續(y/n)?");

            while (true){
                try{
                    flag = s.nextLine().charAt(0);
                }catch (Exception e){
                    System.out.println("輸入不能為空!");
                }
                if(flag == 'n' || flag == 'y')
                    break;
                else
                    System.out.println("輸入錯誤!請重新輸入:");
            }
            if(flag == 'n')
                break;
        }
    }
    //更改售貨員資訊
    public void alterSalesMan(){
        System.out.println("執行更改售貨員操作");
        Scanner s = new Scanner(System.in);
        MysqlData md = new MysqlData();
        String sname;
        String alname;
        String alpass;

        while (true){
            System.out.println("輸入更改的售貨員姓名:");

            while (true){
                sname = s.nextLine();
                if(sname.equals(""))
                    System.out.println("輸入不能為空!");
                else
                    break;
            }

            int count = 0;
            count = md.quaryData(sname,"acc_salesman"); // 準確查詢

            if(count > 0){
                System.out.println("選擇您要更改的內容");
                System.out.println("1、更改售貨員姓名");
                System.out.println("2、更改售貨員密碼");
                int select;
                while(true){
                    try{
                        select = s.nextInt();
                        break;
                    }catch (Exception e){
                        System.out.println("輸入錯誤!");
                    }
                }

                if(select == 1){
                    System.out.println("請輸入新的名字:");
                    while (true){
                        alname = s.nextLine();
                        if(alname.length() == 0)
                            System.out.println("不能為空!");
                        else {
                            md.alterData(sname,alname,2);
                            break;
                        }
                    }
                }else if(select == 2){
                    System.out.println("請輸入新的密碼:");
                    while (true){
                        alpass = s.nextLine();
                        if(alpass.equals(""))
                            System.out.println("不能為空!");
                        else {
                            md.alterData(sname,alpass,3);
                            break;
                        }
                    }
                }else {
                    System.out.println("輸入錯誤!");
                }
            }
            System.out.println("是否繼續(y/n)?");
            char flag = 'y';
            while(true){
                try{
                    flag = s.nextLine().charAt(0);
                }catch (Exception e){
                    System.out.println("輸入不能為空!");
                }
                if(flag == 'y' || flag == 'n')
                    break;
                else
                    System.out.println("輸入錯誤!請重新輸入:");
            }
            if(flag == 'n')
                break;
        }
    }

    public void deleteSalesMan() {
        System.out.println("執行刪除售貨員操作");
        MysqlData md = new MysqlData();
        Scanner s = new Scanner(System.in);
        char flag = 'y';
        while (true) {

            System.out.println("輸入刪除的售貨員姓名:");
            String sname = s.nextLine();
            int count = md.quaryData(sname, "acc_salesman");
            if (count > 0) {
                System.out.println("是否確定要刪除(y/n)?");

                while (true){
                    try{
                        flag = s.nextLine().charAt(0);
                    }catch (Exception e){
                        System.out.println("輸入不能為空!");
                    }
                    if(flag == 'n' || flag == 'y')
                        break;
                    else
                        System.out.println("輸入錯誤!請重新輸入:");
                }
                if (flag == 'y') {
                    //delete
                    md.deleteData(sname,2);
                }
            }

            while (true){
                System.out.println("是否繼續(y/n)?");
                try{
                    flag = s.nextLine().charAt(0);
                    if(flag == 'y' || flag == 'n')
                        break;
                    System.out.println("請正確輸入");
                }catch (Exception e){
                    System.out.println("請正確輸入");
                }
            }
            if(flag == 'n')
                break;
        }

    }

    public void showSalesMan(){
        System.out.println("執行顯示所有售貨員操作");
        MysqlData md = new MysqlData();
        md.showData(4);

    }
    public void quarySalesMan(){
        System.out.println("執行查詢售貨員操作");
        MysqlData md = new MysqlData();
        Scanner s = new Scanner(System.in);
        char flag = 'y';
        while (true){
            System.out.println("輸入要查詢的售貨員姓名關鍵字:");
            String sname = s.nextLine();
            md.quaryData(sname,"inacc_salesman");
            System.out.println("是夠繼續(y/n)?");

            while (true){
                try{
                    flag = s.nextLine().charAt(0);
                    if(flag == 'y' || flag == 'n')
                        break;
                    System.out.println("請正確輸入");
                }catch (Exception e){
                    System.out.println("請正確輸入");
                }
            }
            if(flag == 'n')
                break;
        }

    }
}

4、MysqlData.java

import java.sql.*;

public class MysqlData {
    private String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    private String DB_URL = "jdbc:mysql://localhost/long";
    private String USER = "root";
    private String PASS = "123456";

    Connection conn = null;
    PreparedStatement pstmt = null;
    Statement stmt = null;
    ResultSet rs = null;

/**************商品維護**********************/
    //新增商品資訊
    public void InsertData(String name, Float price, int num){

        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            //注意:第一個ID自動編號,不用新增
            String sql = "insert into goods(GName,GPrice,GNum) values(?,?,?) ";

            pstmt = conn.prepareStatement(sql);

            //System.out.println("name:" + name);

            pstmt.setString(1, name);
            pstmt.setFloat(2, price);
            pstmt.setInt(3, num);

            int i = pstmt.executeUpdate();
            if(i > 0){
                System.out.println("商品新增成功!");
            }

            pstmt.close();
            conn.close();

        }catch (SQLException se){
            se.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(pstmt != null)
                    conn.close();
            }catch (SQLException se){
            }
            try {
                if(conn != null)
                    conn.close();
            }catch (SQLException se){
                se.printStackTrace();
            }
        }
    }
    //新增售貨員資訊
    public void InsertData(String sname, String spass){

        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,USER,PASS);


            String sql = "insert into salesman(SName,SPassword) values(?,?) ";
            pstmt = conn.prepareStatement(sql);
            System.out.println("sqlname:" + sname);

            pstmt.setString(1, sname);
            pstmt.setString(2, spass);


            int i = pstmt.executeUpdate();
            if(i > 0){
                System.out.println("新增售貨員成功!");
            }

            pstmt.close();
            conn.close();

        }catch (SQLException se){
            se.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(pstmt != null)
                    conn.close();
            }catch (SQLException se){
            }
            try {
                if(conn != null)
                    conn.close();
            }catch (SQLException se){
                se.printStackTrace();
            }
        }
    }

    /* method 方法呼叫
    * method = "acc_goods",商品資訊精確查詢;
    * method = "inacc_goods",商品資訊模糊查詢;
    * method = "acc_salesman",售貨員資訊精確查詢;
    * method = "inacc_salesman",售貨員資訊模糊查詢;
    * 用int count>0判斷查詢存在,否則不存在*/
    //查詢資料(準確查,模糊查)
    public int quaryData(String name, String method){
        int count = 0;
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            //查詢資料是否存在資料庫,用count>0判斷是否存在
            //準確查詢-商品
            String sqlgood = "SELECT * FROM goods WHERE GName = ?;";

            //模糊查詢-商品
            //方法1
            String sqlgood1 =  "SELECT * FROM goods WHERE GName like \"%\"?\"%\"";
            //方法2
           /* String sql3 =  "SELECT * FROM goods WHERE ";
            sql1 += "GName like concat('%',?,'%')";*/

            //準確查詢-售貨員
            String sqlsales =  "SELECT * FROM salesman WHERE SName = ?;";
            //模糊查詢-售貨員
            String sqlsales1 = "SELECT * FROM salesman WHERE SName like \"%\"?\"%\"";

            String sql = "";

            if(method == "acc_goods"){
                sql = sqlgood;
            }else if(method == "inacc_goods"){
                sql = sqlgood1;
            }else if (method == "acc_salesman"){
                sql = sqlsales;
            }else if (method == "inacc_salesman"){
                sql = sqlsales1;
            }else {
                System.out.println("方法呼叫錯誤!");
            }

            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            rs = pstmt.executeQuery();

            if(method == "acc_goods" || method == "inacc_goods"){
                if(!rs.next()){
                    System.out.println("該商品不存在!");
                    count = 0;
                } else {
                    System.out.print("商品名稱     " + "商品價格     " + "商品數量" + "\t" + "\n");
                    String gname = rs.getString("GName");
                    Float gprice = rs.getFloat("GPrice");
                    int gnum = rs.getInt("GNum");
                    System.out.print(gname + "       " + gprice +"        " + gnum + "\t" + "\n");
                    while (rs.next()){
                        gname = rs.getString("GName");
                        gprice = rs.getFloat("GPrice");
                        gnum = rs.getInt("GNum");
                        System.out.print(gname + "       " + gprice +"        " + gnum + "\t" + "\n");
                    }
                    count = 1;
                }
            }else {
                if(!rs.next()){
                    System.out.println("該售貨員不存在!");
                    count = 0;
                } else {
                    System.out.print("售貨員姓名     " + "售貨員密碼" + "\t" + "\n");
                    String sname = rs.getString("SName");
                    String spass = rs.getString("spassword");
                    System.out.print(sname + "          " + spass + "\t" + "\n");
                    while (rs.next()){
                        sname = rs.getString("SName");
                        spass = rs.getString("spassword");
                        System.out.print(sname + "          " + spass + "\t" + "\n");
                    }
                    count = 1;
                }
            }

            pstmt.close();
            conn.close();
        } catch (SQLException se){
            se.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(pstmt != null)
                    conn.close();
            }catch (SQLException se){
            }
            try {
                if(conn != null)
                    conn.close();
            }catch (SQLException se){
                se.printStackTrace();
            }
        }
        /*System.out.println("count:" + count);*/
        return count;
    }


    //售貨員使用者名稱和密碼匹配,返回售貨員ID
    public int[] quaryData(String name, String spass, int i){
        int count = 0;
        int sid = 0;
        try{
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,USER,PASS);
            String sql = "SELECT *  FROM salesman WHERE SName = ? AND SPassword = ?;";

            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, spass);
            rs = pstmt.executeQuery();

            while (rs.next()){
                sid = rs.getInt("SID");
                count = 1;
            }


            pstmt.close();
            conn.close();
        } catch (SQLException se){
            se.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(pstmt != null)
                    conn.close();
            }catch (SQLException se){
            }
            try {
                if(conn != null)
                    conn.close();
            }catch (SQLException se){
                se.printStackTrace();
            }
        }
        int[] arr = {count, sid};
        return arr;
    }

    //返回商品價格(float)、數量(int),id
    public float[] quaryData(String name){
        float price = 0;
        float num = 0;
        float gid = 0;

        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,USER,PASS);
            String sql = "SELECT * FROM goods WHERE GName = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            rs = pstmt.executeQuery();
            while (rs.next()){
                price = rs.getFloat("gprice");
                num = rs.getFloat("gnum");
                gid = rs.getFloat("gid");
            }
            pstmt.close();
            conn.close();
        } catch (SQLException se){
            se.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(pstmt != null)
                    conn.close();
            }catch (SQLException se){
            }
            try {
                if(conn != null)
                    conn.close();
            }catch (SQLException se){
                se.printStackTrace();
            }
        }
        float[] arr = {gid, price, num};
        return arr;
    }


    //method=1:更改商品名稱;method=2:改售貨員姓名;method=3,改售貨員密碼,
    public void alterData(String name, String alname, int method){

        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            String sql1 = "UPDATE goods SET GName = ? WHERE GName = ?";
            String sql2 = "UPDATE salesman SET SName = ? WHERE SName = ?";
            String sql3 = "UPDATE salesman SET SPassword = ? WHERE SName = ?";
            String sql = "";

            if(method == 1){
                sql = sql1;
            }else if(method == 2){
                sql = sql2;
            }else if(method == 3){
                sql = sql3;
            }else{
                System.out.println("方法呼叫錯誤!");
            }

            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, alname);
            pstmt.setString(2, name);

            if(method == 4){
                rs = pstmt.executeQuery();
                if(!rs.next()){
                    System.out.println("使用者名稱和密碼不匹配!!");
                    int count = 0;
                }
            }else {
                int i = pstmt.executeUpdate();
                if(i > 0){
                    System.out.println("修改成功!");
                }
            }

            pstmt.close();
            conn.close();

        } catch (SQLException se){
            se.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(pstmt != null)
                    conn.close();
            }catch (SQLException se){
            }
            try {
                if(conn != null)
                    conn.close();
            }catch (SQLException se){
                se.printStackTrace();
            }
        }
    }

    //method=1:更改商品數量,並顯示修改成功;method=2:更改商品數量,不顯示修改成功
    public void alterData(String name, int alnum, int method){
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            String sql = "UPDATE goods SET GNum = ? WHERE GName = ?";

            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, alnum);
            pstmt.setString(2, name);
            //rs = pstmt.executeQuery();

            int i = pstmt.executeUpdate();
            if(i > 0 & method == 1){
                System.out.println("修改成功!");
            }

            pstmt.close();
            conn.close();

        } catch (SQLException se){
            se.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(pstmt != null)
                    conn.close();
            }catch (SQLException se){
            }
            try {
                if(conn != null)
                    conn.close();
            }catch (SQLException se){
                se.printStackTrace();
            }
        }
    }
    //更改商品價格
    public void alterData(String name, Float alprice){
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            String sql = "UPDATE goods SET GPrice = ? WHERE GName = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setFloat(1, alprice);
            pstmt.setString(2, name);
            //rs = pstmt.executeQuery();

            int i = pstmt.executeUpdate();
            if(i > 0){
                System.out.println("商品價格修改成功!");
            }

            pstmt.close();
            conn.close();
        } catch (SQLException se){
            se.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(pstmt != null)
                    conn.close();
            }catch (SQLException se){
            }
            try {
                if(conn != null)
                    conn.close();
            }catch (SQLException se){
                se.printStackTrace();
            }
        }
    }

    //method=1:刪除商品;method=2:刪除售貨員
    public void deleteData(String name, int method){
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            String sql1 = "DELETE FROM goods WHERE GName = ?";
            String sql2 = "DELETE FROM salesman WHERE SName = ?";
            String sql = "";
            if(method == 1){
                sql = sql1;
            }else if(method == 2){
                sql = sql2;
            }else{
                System.out.println("方法呼叫失敗。");
            }
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);

            int i = pstmt.executeUpdate();
            if(i > 0){
                System.out.println("刪除成功!");
            }else {
                System.out.println("刪除失敗!");
            }

            pstmt.close();
            conn.close();

        } catch (SQLException se){
            se.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(pstmt != null)
                    conn.close();
            }catch (SQLException se){
            }
            try {
                if(conn != null)
                    conn.close();
            }catch (SQLException se){
                se.printStackTrace();
            }
        }
    }

    /*method = 1:顯示所有商品資訊;
    * method = 2:價格升序顯示商品資訊;
    * method = 3:數量升序顯示商品資訊;
    * method = 4:顯示所有售貨員*/
    //顯示所有商品資訊(普通顯示,價格升序顯示,數量升序顯示)
    public void showData(int method){
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            //method=1,顯示所有商品資訊
            String sql1 = "SELECT *, (CASE WHEN gnum >= 10 THEN NULL WHEN gnum < 10 THEN \"*該商品已不足10件!\" END) 'note' FROM goods;";
            //method=2,根據價格升序顯示商品資訊
            String sql2 = "SELECT GName,GPrice,GNum FROM goods GROUP BY GPrice";
            //method=3,根據數量升序顯示商品資訊
            String sql3 = "SELECT GName,GPrice,GNum FROM goods GROUP BY GNum";
            String sql4 = "SELECT SName,SPassword FROM salesman";
            String sql = "";

            if(method == 1){
                sql = sql1;
            }else if(method == 2){
                sql = sql2;
            }else if(method == 3){
                sql = sql3;
            }else if(method == 4){
                sql = sql4;
            }else {
                System.out.println("方法呼叫錯誤!");
            }

            stmt = conn.createStatement();

            rs = stmt.executeQuery(sql);

            //普通查詢比其他查詢多了一個備註
            if(method == 1){
                System.out.printf("%-10s","商品名稱");
                System.out.printf("%-10s","商品價格");
                System.out.printf("%-10s","商品數量");
                System.out.printf("%-10s","備註");
                System.out.println();
                while (rs.next()){
                    //int gid = rs.getInt("GID");
                    String gname = rs.getString("GName");
                    Float gprice = rs.getFloat("GPrice");
                    int gnum = rs.getInt("GNum");
                    String note = rs.getString("note");

                    System.out.printf("%-14s",gname);
                    System.out.printf("%-13.1f",gprice);
                    System.out.printf("%-10d",gnum);
                    System.out.printf("%-10s",note);
                    System.out.println();

               /* System.out.printf("商品名稱"+"\t \t \t \t ");
                System.out.printf("商品價格"+"\t \t \t \t ");
                System.out.printf("商品數量"+"\t \t \t \t ");
                System.out.printf("備註"+"\t \t \t \t ");
                System.out.println();
                System.out.printf(gname +"\t \t \t \t ");
                System.out.printf(gprice+"\t \t \t \t ");
                System.out.printf(gnum+"\t \t \t \t ");
                System.out.printf(note+"\t \t \t \t ");
                System.out.println();*/
                }
            }else if(method == 4) {
                System.out.print("售貨員姓名     " + "售貨員密碼" + "\t" + "\n");
                while (rs.next()){
                    String sname = rs.getString("SName");
                    String spass = rs.getString("spassword");
                    System.out.print(sname + "       " + spass + "\t" + "\n");
                }
            } else {
                System.out.printf("商品名稱"+"\t \t \t \t ");
                System.out.printf("商品價格"+"\t \t \t \t ");
                System.out.printf("商品數量"+"\t \t \t \t ");
                System.out.println();
                while (rs.next()){
                    String gname = rs.getString("GName");
                    Float gprice = rs.getFloat("GPrice");
                    int gnum = rs.getInt("GNum");

                    System.out.printf(gname +"\t \t \t \t ");
                    System.out.printf(gprice+"\t \t \t \t ");
                    System.out.printf(gnum+"\t \t \t \t ");
                    System.out.println();
                }
            }

            stmt.close();
            conn.close();

        } catch (SQLException se){
            se.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(stmt != null)
                    conn.close();
            }catch (SQLException se){
            }
            try {
                if(conn != null)
                    conn.close();
            }catch (SQLException se){
                se.printStackTrace();
            }
        }
    }

    //銷售資訊存入gsales表
    public void insertGsales(int gid, int sid, Date sdate, int snum){
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            String sql = "insert into gsales(GID,SID,SDate,SNum) values(?,?,?,?) ";

            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, gid);
            pstmt.setInt(2, sid);
            pstmt.setDate(3, sdate);
            pstmt.setInt(4, snum);
            pstmt.executeUpdate();

            /*int i = pstmt.executeUpdate();
            if(i > 0){
                System.out.println("新增成功!");
            }else {
                System.out.println("新增失敗!");
            }*/

            pstmt.close();
            conn.close();

        } catch (SQLException se){
            se.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(pstmt != null)
                    conn.close();
            }catch (SQLException se){
            }
            try {
                if(conn != null)
                    conn.close();
            }catch (SQLException se){
                se.printStackTrace();
            }
        }
    }

    //顯示今日銷售的商品
    public void showgoodsales(Date sdate){
        try{

            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            String sql = "SELECT a.gid,a.GName,a.GPrice,a.GNum, b.snum,b.SDate," +
                    "(CASE WHEN a.gnum >= 10 THEN NULL WHEN a.gnum < 10 THEN\"*該商品已不足10件!\" END) 'note' " +
                    "FROM\tgoods a right JOIN gsales b on a.gid = b.gid where b.SDate = ? ";

            pstmt = conn.prepareStatement(sql);
            pstmt.setDate(1, sdate);
            rs = pstmt.executeQuery();

            System.out.print("商品名稱    "+"商品價格    "+"商品數量    "+"銷量    "+"備註"+"\t"+"\n");
            while (rs.next()){
                String gname = rs.getString("gname");
                float gprice = rs.getFloat("gprice");
                int gnum = rs.getInt("gnum");
                int snum = rs.getInt("snum");
                String note = rs.getString("note");
                System.out.println(gname+"      "+gprice+"       "+gnum+"         "+snum+"       "+note);
            }
            pstmt.close();
            conn.close();

        }catch (SQLException se){
            se.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(pstmt != null)
                    conn.close();
            }catch (SQLException se){
            }
            try {
                if(conn != null)
                    conn.close();
            }catch (SQLException se){
                se.printStackTrace();
            }
        }
    }
}

 

相關文章