user3079530
user3079530

Reputation: 13

Java: setting variable on object array

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;
}
 }

I have this problem. when i want to set one parking detail object into ParkingLot array, the array is insert same data information on all of array. the output suppose to be like this:

**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

but I got all the output being set to 1. So how to solve this problem? thanks..

Upvotes: 0

Views: 796

Answers (1)

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79838

There's still lots of code that you haven't shown, for example, the code for the ParkingDescription class. But based on what you HAVE shown ...

In the chunk of code that prints out all the statuses, (which for some reason, you've coded twice - once in Reg and once in UpdatePark), you are populating your array with 45 new ParkingDescription objects, and printing the status of each one. That is, you're throwing away all the ParkingDescription objects that you had previously, and only printing brand new objects. You really don't want to do that, because it's the ParkingDescription objects that store all the data that the user has entered.

I guess you're getting all 1s because 1 is the status of a new ParkingDescription.

UPDATE

Now that you've shown your ParkingDescription class, I see that the problem is that you've declared its fields as static. That means that there's only one copy of each field, shared between all the instances of this class. Remove the word static from the field declarations, and the problem should be fixed.

And the next time you post a question on Stack Overflow, I strongly recommend posting ALL the code when you first post the question. Don't just post the part where YOU think the bug is, because you waste everyone's time if you're wrong.

Upvotes: 1

Related Questions