java - 获取 Int 的存储值并在不同的类中使用它

标签 java sql variables integer public

我是 Java 编程新手,目前正在制作迷你 parking 系统。

我正在尝试获取 (carSlotLeft, motorSlotLeft, vanSlotLeft) - 包含当前剩余插槽的变量。我将在类“SQLConnections”中使用这些变量,然后在不同的类“Park”上使用它

我所做的是合并我需要的 SQL 结果,因为我将使用结果到不同的类和方法 - 让事情组织起来。但我无法通过结果。

对于“SQL Connections”类,我还将在不同的方法中使用变量,但是当我尝试调用(carSlotLeft, motorSlotLeft, vanSlotLeft)时,它无法获得准确的计算vehicleALLReport()

这是代码。

public class SQLConnections {
    public Connection con = null;

    //Count of Parked in Vehicles
    public int countCar;
    public int countMotor;
    public int countVan;

    //Default Parking Slot Count
    public int cDefaultSpace = 10;
    public int mDefaultSpace = 5;
    public int vDefaultSpace = 5;

    //Count for Parking Slot Left per Vehicle Type
    public int carSlotLeft;
    public int motorSlotLeft;
    public int vanSlotLeft;

    //Park Vehicle Report Choice
    public int pVReport;

    Scanner input = new Scanner(System.in);

    public void parkStatus() {

        con = dbConnect.con();

        String qCar = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 1 AND `parkout` IS NULL";
        String qMotor = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 2 AND `parkout` IS NULL";
        String qVan = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 3 AND `parkout` IS NULL";

        try {
            Statement stmtCar = con.createStatement();
            Statement stmtMotor = con.createStatement();
            Statement stmtVan = con.createStatement();

            ResultSet rsCar = stmtCar.executeQuery(qCar);
            ResultSet rsMotor = stmtMotor.executeQuery(qMotor);
            ResultSet rsVan = stmtVan.executeQuery(qVan);

            rsCar.next();
            rsMotor.next();
            rsVan.next();

            countCar = rsCar.getInt(1);
            countMotor = rsMotor.getInt(1);
            countVan = rsVan.getInt(1);

            carSlotLeft = cDefaultSpace - countCar;
            motorSlotLeft = mDefaultSpace - countMotor;
            vanSlotLeft = vDefaultSpace - countVan;

            System.out.println("Current Parking Slot Status");
            System.out.printf("%-20s %-20s %-20s \n", "Vehicle Type", "Available Slot Left", "Parking Limit");
            System.out.printf("%-20s %-20s %-20s \n", "Car", carSlotLeft, cDefaultSpace);
            System.out.printf("%-20s %-20s %-20s \n", "Motorcycle", motorSlotLeft, mDefaultSpace);
            System.out.printf("%-20s %-20s %-20s \n", "Van", vanSlotLeft, vDefaultSpace);
            System.out.printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void vehicleALLReport(){
            int slotAvaiblable = carSlotLeft + motorSlotLeft + vanSlotLeft
            System.out.printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
            System.out.printf("Available Slots Left -- ", slotAvaiblable);

}

至于“公园等级” - 也是同样的事情。我也无法得到结果。 这是代码

    public class Park{
    public Connection con = null;
    Scanner input = new Scanner(System.in);

    SQLConnections sqlConnections = new SQLConnections();

    int carSlotLeft = sqlConnections.carSlotLeft;
    int motorSlotLeft = sqlConnections.motorSlotLeft;
    int vanSlotLeft = sqlConnections.vanSlotLeft;

    public void limitCarPark(int vType) {
        con = dbConnect.con();
            if (carSlotLeft == 0){
                System.out.println("No Parking Slot Available!!!\n\n\n");
            } else {
                Park p = new Park();
                p.parkMethod(vType);
            }
        }


    public void limitMotorPark(int vType) {
        con = dbConnect.con();
        if(motorSlotLeft == 0){
            System.out.println("No Parking Slot Available!!!\n\n\n");
        } else {
            Park p = new Park();
            p.parkMethod(vType);
        }
    }

    public void limitVanPark(int vType) {
        con = dbConnect.con();
        if(vanSlotLeft == 0){
            System.out.println("No Parking Slot Available!!!\n\n\n");
        } else {
            Park p = new Park();
            p.parkMethod(vType);
        }
    }

我是不是错过了什么?我做错了什么吗?请帮忙。 非常感谢。

最佳答案

我不知道你到底想要什么,但我修改了代码以使其正常工作:

import java.sql.*;
import java.util.Scanner;

class DbConnect {

    protected String databaseUsername;
    protected String databasePassword;
    protected String databaseURL;
    protected Connection databaseConnection;
    protected String databaseDriverClassName = "com.mysql.cj.jdbc.Driver";
    private static final DbConnect INSTANCE = new DbConnect();

    private DbConnect() {
        this.databaseUsername = "root";
        this.databasePassword = "123456";
        this.databaseURL = "jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf8";
    }


    public static DbConnect getInstance() {
        return INSTANCE;
    }

    public Connection con() {
        if(databaseConnection == null) {
            synchronized (this) {
                if(databaseConnection == null) {
                    databaseConnection = connectToDatabase();
                }
            }
        }
        return databaseConnection;
    }

    protected Connection connectToDatabase() {
        Connection connection = null;
        try {
            Class.forName(databaseDriverClassName);
            connection = DriverManager.getConnection(databaseURL, databaseUsername, databasePassword);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
}


class SQLConnections {
    public Connection con = null;

    //Count of Parked in Vehicles
    public int countCar;
    public int countMotor;
    public int countVan;

    //Default Parking Slot Count
    public int cDefaultSpace = 10;
    public int mDefaultSpace = 5;
    public int vDefaultSpace = 5;

    //Count for Parking Slot Left per Vehicle Type
    public int carSlotLeft;
    public int motorSlotLeft;
    public int vanSlotLeft;

    //Park Vehicle Report Choice
    public int pVReport;

    Scanner input = new Scanner(System.in);

    public DbConnect dbConnect = DbConnect.getInstance();

    public void parkStatus() {

        con = dbConnect.con();

        String qCar = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 1 AND `parkout` IS NULL";
        String qMotor = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 2 AND `parkout` IS NULL";
        String qVan = "SELECT COUNT(*) FROM `vehicle` WHERE `vType` = 3 AND `parkout` IS NULL";

        try {
            Statement stmtCar = con.createStatement();
            Statement stmtMotor = con.createStatement();
            Statement stmtVan = con.createStatement();

            ResultSet rsCar = stmtCar.executeQuery(qCar);
            ResultSet rsMotor = stmtMotor.executeQuery(qMotor);
            ResultSet rsVan = stmtVan.executeQuery(qVan);

            rsCar.next();
            rsMotor.next();
            rsVan.next();

            countCar = rsCar.getInt(1);
            countMotor = rsMotor.getInt(1);
            countVan = rsVan.getInt(1);

            carSlotLeft = cDefaultSpace - countCar;
            motorSlotLeft = mDefaultSpace - countMotor;
            vanSlotLeft = vDefaultSpace - countVan;

            System.out.println("Current Parking Slot Status");
            System.out.printf("%-20s %-20s %-20s \n", "Vehicle Type", "Available Slot Left", "Parking Limit");
            System.out.printf("%-20s %-20s %-20s \n", "Car", carSlotLeft, cDefaultSpace);
            System.out.printf("%-20s %-20s %-20s \n", "Motorcycle", motorSlotLeft, mDefaultSpace);
            System.out.printf("%-20s %-20s %-20s \n", "Van", vanSlotLeft, vDefaultSpace);
            System.out.printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void vehicleALLReport() {
        int slotAvaiblable = carSlotLeft + motorSlotLeft + vanSlotLeft;
        System.out.printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        System.out.printf("Available Slots Left -- %d", slotAvaiblable);

    }
}

public class Park {

    Scanner input = new Scanner(System.in);

    SQLConnections sqlConnections = new SQLConnections();

    int carSlotLeft = sqlConnections.carSlotLeft;
    int motorSlotLeft = sqlConnections.motorSlotLeft;
    int vanSlotLeft = sqlConnections.vanSlotLeft;

    public DbConnect dbConnect = DbConnect.getInstance();

    public void limitCarPark(int vType) {
        sqlConnections.parkStatus();
        carSlotLeft = sqlConnections.carSlotLeft;
        if (carSlotLeft == 0) {
            System.out.println("No Parking Slot Available!!!\n\n\n");
        } else {
            parkMethod(vType);
        }
    }


    public void limitMotorPark(int vType) {
        sqlConnections.parkStatus();
        motorSlotLeft = sqlConnections.motorSlotLeft;
        if (motorSlotLeft == 0) {
            System.out.println("No Parking Slot Available!!!\n\n\n");
        } else {
            parkMethod(vType);
        }
    }

    public void limitVanPark(int vType) {
        sqlConnections.parkStatus();
        vanSlotLeft = sqlConnections.vanSlotLeft;
        if (vanSlotLeft == 0) {
            System.out.println("No Parking Slot Available!!!\n\n\n");
        } else {
            parkMethod(vType);
        }
    }

    public void parkMethod(int vType) {
        sqlConnections.vehicleALLReport();
    }

    public static void main(String[] args) {
        Park park = new Park();
        park.limitCarPark(1);
        park.limitMotorPark(1);
        park.limitVanPark(1);
    }
}

关于java - 获取 Int 的存储值并在不同的类中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59462987/

相关文章:

使用来自reflect.Method的通用签名信息生成Java字节码

mysql - 将sql列的内容更改为全部相同

php - 如何生成 OTP 并通过短信将密码发送到手机

variables - 使用Kotlin的URL变量

PHP/MySQL : How do a concatenate a variable in a mysql query?

java - Netty 中的并发

java - Spring XD 流中的异常处理

sql - 检查MS Access SQL语句中的空值

powershell - 从变量获取服务

java - 具有简单类型的 AspectJ 连接点