# Problem with case - might need recursion

• 10-11-2010, 06:44 PM
Angelar
Problem with case - might need recursion
Hey this is my 1st thread

I have this class

Code:

class Vehicle {
private int Aukswn; //Number of cars entered the parking area
private String Pinakida; //The cars license plate number
private String Type; // Car type
private int Position; //The position the car is going to park

public Vehicle() {
Aukswn = 0;
Pinakida = new String();
Type = new String();
Position = 0;
}

public void Vehicle (int Aukswn, String Pinakida, String Type, int Position) {
this.Aukswn = Aukswn;
this.Pinakida = Pinakida;
this.Type = Type;
this.Position = Position;
}

public int getAukswn() {
return this.Aukswn;
}
public String getPinakida() {
return this.Pinakida;
}
public String getType() {
return this.Type;
}
public int getPosition() {
return this.Position;
}
}

Code:

public class Main {
public static void main (String args[]) {
int i, choice;
Vehicle array[] = new Vehicle[10];
do{
System.out.print("1. Arrival of Vehicle");
System.out.print("2. Vehicle Properties");
System.out.print("3. Free parking slots");
System.out.print("4. End");
System.out.print("Choice (1-4) :");
choice = UserInput.getInteger();
switch (choice) {
case (1):

}

}
while (choice == 4);
}
}

Now I find it a bit complicated to explain in English but I will try

For the 1st case, I will need to
Send a message to the user that there are any free parking slots (variable position = 0),and if there are, display the index of the first free slot to the user (else it will send him back to the main menu)

If there is a free parking slot, the user should enter the properties of the vehicle (I have my Insert class for that purpose, but for some reason I cant use it under a case)

Code:

public void Insert() {
System.out.print ("Give the number of cars entered the parking area");
this.Aukswn = UserInput.getInteger();
System.out.print ("Give the car's license plate number");
this.Pinakida = UserInput.getString();
System.out.print ("Give the parking slot which the car entered");
this.Position = UserInput.getInteger();
System.out.print ("Give the type of the car");
this.Type = UserInput.getString();
}

The position the user enters should be the one the program displayed at him
If he does not enter this position, or an invalid number (e.g. > 10) it should return him to the main menu
After the user succesfully enters the mentioned properties of the vehicle, the program returns him to the main menu. It should also return him if he presses the Esc. button during entering them
Any opinions?
• 10-12-2010, 12:58 PM
Ronin
Hello Angelar and welcome.

I presume this is assignment work so I will only point you in the right direction for now.
I have made a few assumptions about you code so correct me if I missunderstand. Even so, there are a several issues I can spot straight off.

Firstly, the variable "Aukswn" refers to the number of cars in the parking lot. If so, why is this variable in the Vehicle class? If you have to store it, then I would suggest placing it somewhere else, perhaps even create a CarPark class.

Secondly, you do while loop does the opposite of what you want. It will only loop if the user wants to exit the program.

Thirdly, I don't think you meant to refer to Insert as being a class, but if you did the method Insert does not contain any reference to the Vehicle class or so neither construct nor effect the properties of an instance of Vehicle.
If on the other hand you meant to refer to Insert as a method within the Vehicle class, then I am guessing that an instance of Vehcle was not created prior to the use of the method or you are not calling it correctly.

Once this lot is sorted, then I would like to hear you thoughts on how to solve the problems you have asked about prior to giving my own ;).

Regards.
• 10-12-2010, 03:14 PM
Angelar
Quote:

Originally Posted by Ronin
Hello Angelar and welcome.

I presume this is assignment work so I will only point you in the right direction for now.
I have made a few assumptions about you code so correct me if I missunderstand. Even so, there are a several issues I can spot straight off.

Firstly, the variable "Aukswn" refers to the number of cars in the parking lot. If so, why is this variable in the Vehicle class? If you have to store it, then I would suggest placing it somewhere else, perhaps even create a CarPark class.

Secondly, you do while loop does the opposite of what you want. It will only loop if the user wants to exit the program.

Thirdly, I don't think you meant to refer to Insert as being a class, but if you did the method Insert does not contain any reference to the Vehicle class or so neither construct nor effect the properties of an instance of Vehicle.
If on the other hand you meant to refer to Insert as a method within the Vehicle class, then I am guessing that an instance of Vehcle was not created prior to the use of the method or you are not calling it correctly.

Once this lot is sorted, then I would like to hear you thoughts on how to solve the problems you have asked about prior to giving my own ;).

Regards.

About the variable Aukswn, it is given from the assignment along with the other attributes of the Vehicle class, thats why I put it there. It did not seem too reasonable to me as well.
About the insert method, I guess I can do it without using a method, just applying UserInput straight away inside my case.
About the loop, I sorted that out
• 10-12-2010, 04:19 PM
Ronin
You could declare the variable Aukswn as static, in which case it will be shared between all of the instances. This is something I generally try to avoid.

I would still like to hear your thoughts on how to solve your other problems before assisting further.

Regards.
• 10-12-2010, 05:22 PM
Angelar
Code:

public class Main {
public static void main (String args[]) {
int i;
int choice = 0;
Vehicle array[] = new Vehicle[10];
while (choice <> 4) {
System.out.print("Αρχικό Μενού");
System.out.print("1. Προσέλευση Οχήματος");
System.out.print("2. Εμφάνιση Στοιχείων Οχήματος");
System.out.print("3. Λίστα Θέσεων Parking");
System.out.print("4. Τέλος");
System.out.print("Δώστε Επιλογή (1-4) :");
choice = UserInput.getInteger();
switch (choice) {
case (1):
if (array.Position[i] = 0) {
System.out.print(" Η πρωτη κενη θεση ειναι η: " + i);
System.out.print("Δωσε αριθμο πινακιδων");
array.Pinakida = UserInput.getString();
System.out.print("Δωσε θεση παρκινγκ (Χρησιμοποιησε την πρωτη ελευθερη)");
array.Position = UserInput.getInteger();
System.out.print ("Δωσε τυπο οχηματος");
array.Type = UserInput.getString();

else return;
}
}
}
}
}

This is what I used now. I left Aukswn out of this (will figure out about it later on). I used a while loop, it seems to be working better with it and used the UserInput without creating the Insert method. Now what I can't say for sure is how to make the program recognize if the entered command is correct or if you pressed the Esc. button so it will return you to the main menu. Opinions so far?
• 10-12-2010, 07:37 PM
Zack
Here's a few things I can suggest:
1) Make sure your code compiles before posting it. Your {}s don't line up, and this prevents us from being able to compile it and run it without figuring out which {}s should be removed.
2) Check your = vs ==, particularly just after your first "case" statement.
3) <> is not a valid Java operator (this is obsolete and only present as legacy code in a few languages, e.g. Python). Use != instead.
4) You declare the variable i, and use it later, but its value is never set.
5) UserInput is never declared.

Sorry if this seems a big harsh to just point them all out like that, but they're all issues that need addressing.

Best of luck. :)
• 10-13-2010, 02:25 PM
Angelar
Quote:

Originally Posted by Zack
Here's a few things I can suggest:
1) Make sure your code compiles before posting it. Your {}s don't line up, and this prevents us from being able to compile it and run it without figuring out which {}s should be removed.
2) Check your = vs ==, particularly just after your first "case" statement.
3) <> is not a valid Java operator (this is obsolete and only present as legacy code in a few languages, e.g. Python). Use != instead.
4) You declare the variable i, and use it later, but its value is never set.
5) UserInput is never declared.

Sorry if this seems a big harsh to just point them all out like that, but they're all issues that need addressing.

Best of luck. :)

About the {}s, NetBeans pops out many mistakes, I try to write the code in notepad trying to line them up, but they pop up again when i paste it in NetBeans
2 and 3 are solved
UserInput is declared in a separate file
And i should be 0

Code:

public class Main {
public static void main (String args[]) {
int i = 0;
int choice = 0;
Vehicle array[] = new Vehicle[10];
while (choice != 4) {
System.out.print("Αρχικό Μενού");
System.out.print("1. Προσέλευση Οχήματος");
System.out.print("2. Εμφάνιση Στοιχείων Οχήματος");
System.out.print("3. Λίστα Θέσεων Parking");
System.out.print("4. Τέλος");
System.out.print("Δώστε Επιλογή (1-4) :");
choice = UserInput.getInteger();
switch (choice) {
case (1):
if (array.Position[i] == 0) {
System.out.print(" Η πρωτη κενη θεση ειναι η: " + i);
System.out.print("Δωσε αριθμο πινακιδων");
array.Pinakida = UserInput.getString();
System.out.print("Δωσε θεση παρκινγκ (Χρησιμοποιησε την πρωτη ελευθερη)");
array.Position = UserInput.getInteger();
System.out.print ("Δωσε τυπο οχηματος");
array.Type = UserInput.getString();

else return;
}
}
}
}
}