Java:在对象数组上设置变量

标签 java arrays object

public class ParkingLotApplication {

static Scanner input = new Scanner(System.in);
public static ParkingDescription[][] ParkingLot = new ParkingDescription[3][15];
public StudentDescription StudDesc = new StudentDescription();
static int i = 0;
static int j = 0;
static int parkLevel = 0;
static int parkSlot = 0;

public static void main(String[] args) {
    // TODO: Add your code here
    ParkingLotApplication PA = new ParkingLotApplication();
    PA.menu();
}

public void menu() {
    Scanner input = new Scanner(System.in);
    System.out.println("WELCOME TO CAR PARK SYSTEM");
    System.out.println("Enter your name: ");
    String nm = input.nextLine();
    System.out.println("Enter your password: ");
    int pass = input.nextInt();
    if ((nm.equals("admin12")) && (pass == 12345)) {
        Login();
    } else {
        menu();
    }
}

public void Login() {
    System.out.println("|----------------------------------|");
    System.out.println("|           Admin Menu             |");
    System.out.println("|----------------------------------|");
    System.out.println("|  N- New Registration             |");
    System.out.println("|  U- Update Data                  |");
    System.out.println("|----------------------------------|");
    char ch = input.next().charAt(0);
    switch (ch) {
        case 'N':
        case 'n':
            Reg();
            break;

        case 'U':
        case 'u':
            UpdatePark();
            break;


        default:
            System.out.println("Choose Again!");
            Login();
            break;
    }
}

public void Reg() {
    System.out.println();
    System.out.println("                               **Parking Lot**        ");
    System.out.println("    ________________________________________________________________________________");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 15; j++) {
            ParkingLot[i][j] = new ParkingDescription();
            System.out.print("   *   " + ParkingLot[i][j].getStatus());
        }
        System.out.println();
        System.out.println("    ********************************************************************************");
    }
    System.out.println("Please insert number 1-3 to choose the parking level");
    parkLevel = input.nextInt();
    System.out.println("Please insert number 1-15 to choose the parking slot");
    parkSlot = input.nextInt();

    //check available
    if (parkLevel == 1) {
        ParkingLot[0][parkSlot - 1] = new ParkingDescription();
        if (ParkingLot[0][parkSlot - 1].getAvailable() == true) {
            System.out.println("Please Enter tp: ");
            int tp = input.nextInt();
            System.out.println("Please Enter First Name: ");
            String ft = input.next();
            System.out.println("Please Enter Last Name: ");
            String lt = input.next();
            System.out.println("Please Enter Contact Number: ");
            int cn = input.nextInt();
            System.out.println("Please Enter Email Address: ");
            String ea = input.next();
            System.out.println("Please Enter Car Number: ");
            String cnb = input.next();
            System.out.println("Please Enter Date Registered : ");
            int date = input.nextInt();

            StudDesc.setStudDesc(tp, ft, lt, cn, ea, cnb, date);

            int pID = (parkLevel * 1000) + parkSlot;
            ParkingLot[0][parkSlot - 1].setPark(pID, false, StudDesc);
            System.out.println("Thanks");
            menu();
        } else {
            System.out.println("Sorry");
            menu();
        }
    } else if (parkLevel == 2) {
        ParkingLot[1][parkSlot - 1] = new ParkingDescription();
        if (ParkingLot[1][parkSlot - 1].getAvailable() == true) {
            System.out.println("Please Enter tp: ");
            int tp = input.nextInt();
            System.out.println("Please Enter First Name: ");
            String ft = input.next();
            System.out.println("Please Enter Last Name: ");
            String lt = input.next();
            System.out.println("Please Enter Contact Number: ");
            int cn = input.nextInt();
            System.out.println("Please Enter Email Address: ");
            String ea = input.next();
            System.out.println("Please Enter Car Number: ");
            String cnb = input.next();
            System.out.println("Please Enter Date Registered : ");
            int date = input.nextInt();

            StudDesc.setStudDesc(tp, ft, lt, cn, ea, cnb, date);

            int pID = (parkLevel * 1000) + parkSlot;
            ParkingLot[1][parkSlot - 1].setPark(pID, false, StudDesc);
            System.out.println("Thanks");
            menu();
        } else {
            System.out.println("Sorry");
            menu();
        }
    } else if (parkLevel == 3) {
        ParkingLot[2][parkSlot - 1] = new ParkingDescription();
        if (ParkingLot[2][parkSlot - 1].getAvailable() == true) {
            System.out.println("Please Enter tp: ");
            int tp = input.nextInt();
            System.out.println("Please Enter First Name: ");
            String ft = input.next();
            System.out.println("Please Enter Last Name: ");
            String lt = input.next();
            System.out.println("Please Enter Contact Number: ");
            int cn = input.nextInt();
            System.out.println("Please Enter Email Address: ");
            String ea = input.next();
            System.out.println("Please Enter Car Number: ");
            String cnb = input.next();
            System.out.println("Please Enter Date Registered : ");
            int date = input.nextInt();

            StudDesc.setStudDesc(tp, ft, lt, cn, ea, cnb, date);

            int pID = (parkLevel * 1000) + parkSlot;
            ParkingLot[2][parkSlot - 1].setPark(pID, false, StudDesc);
            System.out.println("Thanks");
            menu();
        } else {
            System.out.println("Sorry");
            menu();
        }
    }
}

public void UpdatePark() {
    System.out.println();
    System.out.println("                                     **Parking Lot**                   ");
    System.out.println("    ________________________________________________________________________________");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 15; j++) {
            ParkingLot[i][j] = new ParkingDescription();
            System.out.print("   *   " + ParkingLot[i][j].getStatus());
        }
        System.out.println();
        System.out.println("    ********************************************************************************");
    }
    System.out.println("Please insert number 1-3 to choose the parking level");
    parkLevel = input.nextInt();
    System.out.println("Please insert number 1-15 to choose the parking slot");
    parkSlot = input.nextInt();

    //check available
    if (parkLevel == 1) {
        ParkingLot[0][parkSlot - 1] = new ParkingDescription();
        if (ParkingLot[0][parkSlot - 1].getAvailable() == false) {
            ParkingLot[0][parkSlot - 1].showDetails();
            menu();
        } else {
            System.out.println("Sorry");
            menu();
        }
    } else if (parkLevel == 2) {
        ParkingLot[1][parkSlot - 1] = new ParkingDescription();
        if (ParkingLot[1][parkSlot - 1].getAvailable() == true) {
            ParkingLot[1][parkSlot - 1].showDetails();
            menu();
        } else {
            System.out.println("Sorry");
            menu();
        }
    } else if (parkLevel == 3) {
        ParkingLot[2][parkSlot - 1] = new ParkingDescription();
        if (ParkingLot[2][parkSlot - 1].getAvailable() == true) {
            ParkingLot[2][parkSlot - 1].showDetails();
            menu();
        } else {
            System.out.println("Sorry");
            menu();
        }
    }
}
  }

//Class parking description
public class ParkingDescription {

public static int StudID, ParkSpaceID, DReg;
public static String Status, CNum;
 public static Boolean Available = true;

  public ParkingDescription() {
// TODO: Add your code here
 }

public void setPark(int parkSpaceID, Boolean available, StudentDescription StDe)
{
  this.StudID = StDe.getStudentID();
  this.CNum = StDe.getCarNumber();
  this.DReg = StDe.getDateReg();
  this.ParkSpaceID = parkSpaceID;
  this.Available = available;
}

public Boolean getAvailable()
{
  return Available;
}

public String getStatus()
{
  if(Available == false){
    return "1";
  } else {
    return "0";
  }
}

  public void showDetails()
{
  //generate report
  System.out.println("Student ID : TP"+StudID);
  System.out.println();
  System.out.println("Car Number : "+CNum);
  System.out.println();
  System.out.println("Parking Space : L"+ParkSpaceID);
  System.out.println();
  System.out.println("Date Register : "+DReg);
}
}
 //Student class

 public class StudentDescription {

 public int StudentID, CNumber, DateReg;
 public String FName, LName, EMail, CarNum;

 public StudentDescription() {
// TODO: Add your code here
 }
   public void setStudDesc(int studentID, String fName, String lName, int cNumber, String eMail, String carNum, int dateReg)
{
  this.StudentID = studentID;
  this.FName = fName;
  this.LName = lName;
  this.CNumber = cNumber;
  this.EMail = eMail;
  this.CarNum = carNum;
  this.DateReg = dateReg;
}

public int getStudentID()
{
  return this.StudentID;
}

public String getCarNumber()
{
  return this.CarNum;
}

public int getDateReg()
{
  return this.DateReg;
}
 }

我有这个问题。当我想将一个 parking 详细信息对象设置到 ParkingLot 数组中时,该数组会在所有数组上插入相同的数据信息。输出应该是这样的:

**Parking Lot**                   
*   1   *   0   *   0   *   0   *   0   *   0   *   0   *   0   *   0   *   0   *   0  *   0   *   0   *   0   *   0
*   0   *   0   *   0   *   0   *   0   *   0   *   0   *   0   *   0   *   0   *   0     *   0   *   0   *   0   *   0
*   0   *   0   *   0   *   0   *   0   *   0   *   0   *   0   *   0   *   0   *   0   *   0   *   0   *   0   *   0

但是我把所有的输出都设置为1。那么如何解决这个问题呢?谢谢..

最佳答案

还有很多代码尚未显示,例如 ParkingDescription 类的代码。但根据你所展示的内容......

在打印出所有状态的代码块中(由于某种原因,您已经编码了两次 - 一次在 Reg 中,一次在 UpdatePark 中),您正在用 45 个新的 ParkingDescription 对象填充数组,并打印每个对象的状态。也就是说,您将丢弃以前拥有的所有 ParkingDescription 对象,只打印全新的对象。您确实不想这样做,因为 ParkingDescription 对象存储了用户输入的所有数据。

我猜您得到的都是 1,因为 1 是新 ParkingDescription 的状态。

更新

既然您已经显示了 ParkingDescription 类,我发现问题在于您已将其字段声明为 static。这意味着每个字段只有一个副本,在此类的所有实例之间共享。从字段声明中删除单词 static,问题就应该得到解决。

下次您在 Stack Overflow 上发布问题时,我强烈建议您在第一次发布问题时发布所有代码。不要只发布您认为存在错误的部分,因为如果您错了,您会浪费每个人的时间。

关于Java:在对象数组上设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20452055/

相关文章:

java - 自动lombokify一个项目

java - 获取 jlist 的选定值

c# - 制作混合数组变量?

php - 从另一个对象向 stdClass 对象添加属性

javascript - 如何访问javaScript对象属性NAME而不是VALUE

java - Android - 禁用 ListView 选择突出显示但保持 OnClick 启用

java - 错误 : use 'application/json' Content-Type and raw POST with json data

c- 分配和释放二维数组

c - 在函数中通过指针修改 char* 会导致崩溃

javascript - 优化目标代码