获取Int的存储值并在其他类中使用它

问题描述 投票:0回答:2

我不熟悉Java编程和编写迷你停车场系统。

我正在尝试获取((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);
        }
    }

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

java sql variables integer public
2个回答
0
投票

如果要在多个类中使用变量,可以将它们设为公共静态,完成此操作后,您只需输入className.variableName即可访问它们。

我希望这是您在寻找答案的地方。祝您有美好的一天DeVeenix


0
投票

我不知道您到底想要什么,但是我已经修改了代码以使其起作用:

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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.