用java写宠物商店,1.系统启动,2.宠物主人登陆,3.宠物主人购买库存宠

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用java写宠物商店,1.系统启动,2.宠物主人登陆,3.宠物主人购买库存宠相关的知识,希望对你有一定的参考价值。

用java写宠物商店,1.系统启动,2.宠物主人登陆,3.宠物主人购买库存宠物,4.宠物主人购买新培育宠物,5.宠物主人卖出宠物给商店,我老是写错求大神给分你写的代码,我参考参考

参考技术A 有意愿私信我

java入门:用jdbc实现宠物商店管理系统续篇

用jdbc实现宠物商店管理系统
1,开发语言:java(JDK8)
2,开发工具:IntelliJ IDEA 2021.2.3
3,数据库:MySQL
4,操作系统:Windows10
5,需要引入的jar包:mysql-connector-java-8.0.26.jar,下载地址:https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.26

因为上篇的数据库文件没有了,重新建了数据库以及修改了代码里的脚本,用下面的新代码就可以了
以下是运行图片:




代码实现:
逻辑代码

package com.impl;

import com.dao.BaseDAO;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.InputMismatchException;
import java.util.Scanner;


/**
 * @author 咕噜科
 * ClassName: PetManage
 * date: 2021/8/26 22:12
 * Description:
 * version 1.0
 */
public class PetManage extends BaseDAO {

    /**
     *
     */
    public void showAll() {
        showPetName();
        showPetOwner();
        showPetStore();
        login();
    }

    /**
     * 显示宠物的姓名以及ID方法
     */
    public void showPetName() {
        conn = getConnection();
        String sql = "SELECT id,pet_name from pet";
        try {
            state = conn.prepareStatement(sql);
            rs = state.executeQuery();
            System.out.println("Wonderland醒来,所有宠物从MySQL中醒来");
            System.out.println("*************************************");
            int num = 1;
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("pet_name");
                System.out.println("第" + num + "只宠物,名字叫:" + name);
                num++;
            }
            System.out.println("*************************************\\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeObject1();
        }

    }

    /**
     * 显示宠物主人方法
     */
    public void showPetOwner() {
        conn = getConnection();
        String sql = "select id,owner_id,owner_name,money from pet_owner where owner_type = 1";
        try {
            state = conn.prepareStatement(sql);
            rs = state.executeQuery();
            System.out.println("所有宠物主人从MySQL中醒来");
            System.out.println("*************************************");
            int num = 1;
            while (rs.next()) {
                String name = rs.getString("owner_name");
                System.out.println("第" + num + "主人的名字叫:" + name);
                num++;
            }
            System.out.println("*************************************\\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeObject1();
        }
    }

    /**
     * 显示宠物商店方法
     */
    public void showPetStore() {
        conn = getConnection();
        String sql = "select id,owner_id,owner_name,money,address from pet_owner where owner_type = 2";
        try {
            state = conn.prepareStatement(sql);
            rs = state.executeQuery();
            System.out.println("所有宠物商店从MySQL中醒来");
            System.out.println("*************************************");
            int num = 1;
            while (rs.next()) {
                String storeName = rs.getString("owner_name");
                String storeAddress = rs.getString("address");
                System.out.println("第" + num + "个商店的名字叫: " + storeName + " ,地址在: " + storeAddress);
                num++;
            }
            System.out.println("*************************************\\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeObject1();
        }
    }

    /**
     * 登录界面选择是主人登录还是商店登录方法
     */
    public void login() {

        System.out.println("请选择输入登录模式\\n1.宠物主人登录\\n2.宠物商店登录\\n3.退出系统\\n-------------------");
        try {
            Scanner input = new Scanner(System.in);
            int choise = input.nextInt();
            if (choise < 1 || choise > 3) {
                System.out.println("输入有误,请重新选择");
                login();
            } else {
                switch (choise) {
                    case 1:
                        petOwnerLogin();
                        break;
                    case 2:
                        petStoreLogin();
                        break;
                    case 3:
                        System.out.println("谢谢使用");
                        System.exit(0);
                        break;
                    default:
                        break;
                }
            }
        } catch (InputMismatchException e) {
            System.out.println("输入有误,请重新选择");
            login();
        }

    }

    /**
     * 宠物主人登录方法
     *
     * @return
     */
    public boolean petOwnerLogin() {
        boolean flag = false;
        try {
            Scanner input = new Scanner(System.in);
            System.out.println("请先登录,请您先输入主人的名字");
            String name = input.next();
            System.out.println("请您输入主人的密码:");
            String password = input.next();
            conn = getConnection();
            String sql = "SELECT owner_name,password from pet_owner where owner_name=? and password=?";
            try {
                state = conn.prepareStatement(sql);
                state.setString(1, name);
                state.setString(2, password);
                rs = state.executeQuery();
                if (rs.next()) {
                    System.out.println("---------恭喜您成功登录!---------");
                    System.out.println("----------您的基本信息---------");
                    conn = getConnection();
                    String sql2 = "SELECT id,owner_name,owner_id,password,money from pet_owner where owner_name=?";
//					state=conn.prepareStatement(sql2);
//					state.setString(1, name);
//					rs=state.executeQuery();
                    rs = search(sql2, name);
                    if (rs.next()) {
                        int uid = rs.getInt("owner_id");
                        String uname = rs.getString("owner_name");
                        Double uMoney = rs.getDouble("money");
                        System.out.println("登录成功!!!!");
                        System.out.println("姓名:" + uname);
                        System.out.println("元宝数:" + uMoney);
                        dealPet(uname, uid);
                    }
                } else {
                    System.out.println("登录失败,账户与密码不匹配");
                    login();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (InputMismatchException e) {
            System.out.println("输入有误");
            login();
        }

        return false;
    }

    /**
     * 选择买宠物或者卖宠物的方法
     */
    public void dealPet(String ownerName, int uid) {

        System.out.println("您可以购买和卖出宠物,购买宠物请输入1,卖出宠物请输入2\\n1.购买宠物\\n2.卖出宠物\\n3.返回上一级\\n4.退出系统");
        try {
            Scanner input2 = new Scanner(System.in);
            int choise2 = input2.nextInt();
            if (choise2 < 1 || choise2 > 4) {
                System.out.println("输入有误");
                dealPet(ownerName, uid);
            } else {
                switch (choise2) {
                    case 1:
                        //购买宠物
                        buyPet(ownerName, uid);
                        break;
                    case 2:
                        //出售宠物
                        showSellPet(ownerName, uid);
                        break;
                    case 3:
                        //返回上一级
                        login();
                        break;
                    case 4:
                        System.out.println("--------谢谢使用------");
                        System.exit(0);
                    default:
                        break;
                }
            }
        } catch (InputMismatchException e) {
            System.out.println("输入有误");
            dealPet(ownerName, uid);
        }
    }

    /**
     * 显示主人拥有的宠物
     */
    public void showSellPet(String ownerName, int uid) {
        conn = getConnection();
        String sql25 = "select id,pet_name,pet_price,pet_role_type,pet_type from pet where owner_type = 1 and owner_id =" + uid;
        try {
            state = conn.prepareStatement(sql25);
            rs = state.executeQuery();
            System.out.println("以下是你拥有的宠物:");
//			//如果结果集为空,即该主人没有宠物,就返回上一级进行选择
//			if(!rs.next()){
//				System.out.println("您没有宠物,将自动返回上一级");
//				buyPet(ownerName, uid);
//			}
            int num = 1;
            while (rs.next()) {
                int petid = rs.getInt("id");
                String petName = rs.getString("pet_name");
                String petPrice = rs.getString("pet_price");
                System.out.println("这是" + num + "只宠物,编号是" + petid + ",名字叫:" + petName + ",需要" + petPrice + "个元宝");
                num++;
            }
            System.out.println("**************************************");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        closeObject1();
        sellPet(ownerName, uid);
    }

    public void sellPet(String ownerName, int uid) {
        System.out.println("请输入你想卖出的宠物编号:");
        try {
            Scanner input27 = new Scanner(System.in);
            int choisePetId = input27.nextInt();
            //先查询下有哪些商店
            String queryStore = "select owner_id,owner_name from pet_owner where owner_type = 2";
            Connection queryStoreConn = getConnection();
            try {
                state = queryStoreConn.prepareStatement(queryStore);
                rs = state.executeQuery();
                int num = 1;
                if (rs.next()) {
                    //说明有商店存在
                    System.out.println("请输入你要卖给的商店编号");
                    int storeId1 = rs.getInt("owner_id");
                    String storeName = rs.getString("owner_name");
                    System.out.println(num + "." + storeName+"编号\\t:"+storeId1);
                    num++;
                    while (rs.next()) {
                        int storeId = rs.getInt("owner_id");
                        String storeName2 = rs.getString("owner_name");
                        System.out.println(num + "." + storeName2+"\\t编号:"+storeId);
                        num++;
                    }
                    System.out.println("8888.返回上一级\\n9999.退出系统");
                    int storeId = input27.nextInt();
                    if (storeId == 8888) {
                        dealPet(ownerName, uid);
                    } else if (storeId == 9999) {
                        System.out.println("-------谢谢使用-------");
                        System.exit(0);
                    }
                    String sql30 = "select pet_name,pet_price,pet_role_type,pet_type from pet where owner_type = 1 and owner_id = "+uid+" and id = "+choisePetId;
                    Connection conn6 = getConnection();
                    try {
                        state = conn6.prepareStatement(sql30);
                        rs = state.executeQuery();
                        if (rs.next()) {
                            String petPrice = rs.getString("pet_price");//宠物价格
                            Connection conn9 = getConnection();
                            conn9.setAutoCommit(false);
                            //修改宠物信息
                            String sql40 = "update pet set owner_type= 2,owner_id=" + storeId + " where id=" + choisePetIdjava数据库----宠物商店

实训任务求助:宠物店管理系统

java程序设计题,设计一个宠物商店,内有猫和狗两种宠物,研究名字、颜色和年龄等属

java入门:用jdbc实现宠物商店管理系统续篇

怎么写宠物商店管理系统运用数据库增删改查

Java宠物商店代码,求求各位大神帮忙