So I have a school project due in a week, and Ive done quite a lot of stuff. I am stuck on some parts tho. The project asks us to create a "parking interface" on the windows command prompt, and we need to do some stuff. Firstly, they want the user/person who runs the code to be able to create a garage object, that has 3 floors and an ID. The floors are created by the user, and they each have a car capacity, a truck capacity, and an ID. Then, the program should give the option for the user to create either a truck or a car. The car has an ID, an int value that takes the ID of the garage it is parked at, and an int value that takes the ID of the floor it is parked at. It should also have 2 functions, one to enter a garage, and one to enter a floor. Entering the garage requires at least 1 floor to have at least 1 empty spot, and entering a floor requires that floor to have at least 1 empty slot. Same thing for the trucks. The only difference for trucks and cars is that they cant park in each others' slot. So far, Ive done quite a lot, but I am stuck. First, how can the floors the user creates be distinct from one another, and same goes for the garage. A friend recommended I use ArrayLists, but I am not really sure how to use them. I can provide the code I have done so far, so that it is easier for you all to understand what I have done so far.
#Help with a school project.
1 messages ยท Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question ๐ค. Maybe it helps! In any case, a human is on the way ๐. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
w using Java. I have created the basic structure of the interface with buttons for entering and exiting the parking lot, as well as displaying the number of available parking spots.
One thing I am struggling with is implementing a method to check if a parking spot is available before allowing a car to enter. I need some guidance on how to create this functionality using Java code.
Another issue I am facing is how to keep track of the number of cars currently parked in the lot. I am not sure how to update this count each time a car enters or exits the parking lot.
Lastly, I would appreciate some advice on how to improve the overall user experience of the interface. Any suggestions on adding features or improving the design would be helpful.
Overall, I just need some guidance and tips on these specific aspects of my project. Any help or resources you can provide would be greatly appreciated. Thank you in advance for your assistance!
The project doesnt require a UI or anything, it is run through the compiler of the progam (In my case, IntelliJ, in the schools case, Apache NetBeans 15). I know that some stuff doesnt translate from one to another perfectly, but that isnt an issue. My project partner will import the project to NetBeans and fix any errors that might be there from the structure
Here is what I have done so far
I uploaded your attachments as Gist.
The main file is ergasiaparking.java, where it is run and the user gets to do stuff
It is not complete yet, but I have done progress
Please?
Could you break your initial post up a bit in paragraphs? And you can store your floors in a map where the key is the number, or in a list and give each element a unique ID.
Ok. So:
- Floors have an ID and a capacity for trucks and cars
- Garages have 3 floors, and an ID
- Cars and trucks each have an id, and 2 int values: the ID of the floor they are parked at, and the ID of the garage they are parked in
- The user, upon running the main file, should be able to: Create garages, floors, cars, trucks. Afterwards, they can park the cars and the trucks in floors (using functions)
- Cars and trucks cannot park in the same space
What I need help is:
- How can I allow the user to make an unspecified amount of floors/cars/garages/trucks, and have the program actually work
I was told to use ArrayLists from a friend, but I am not very familiar with them. I tried looking at w3schools, but it didnt really help me with what I needed
From what I have understood, the garages need to be in an arraylist, since the project doesnt require a specified amount of them. Same goes with the floors
There doesnt have to be any UI, the project is to be run through the program they use to compile code
So you can declare List<Floor> floors = new ArrayList<>(); which are collections, to which you can keep adding elements.
Will I be able to choose a specific element at any time?
They have getters, alternatively you can use Map<Int, Floor> floors = new HashMap<>(); where the first parameter is the ID.
I recommend taking a look at:
MOOC is a completely free introductory Java course created by the University of Helsinki, it is a great way to learn Java from the ground up.
It consists of two parts, one at beginner, and another at intermediate level. The end of the course is marked by creating your own Asteroids game clone!
Even though the instructions show how to configure and use NetBeans for the course, you can use IntelliJ. To use IntelliJ, simply install the TMC plugin by opening IntelliJ -> File -> Settings -> Plugins and searching for TMC. You will then be able to use IntelliJ to complete MOOC.
Visit MOOC here: https://java-programming.mooc.fi/
(the course is available in both English and Finnish)
About the course - Java Programming
Ive done a few online "classes" (or whatever they are called) on Codecademy for java
Ill take a look at that sometime.
Given they haven't covered collections I'd check out the linked resource.
Sure
aI read the part about the ArrayLists, and it has been a major help. I have one question tho, that I didnt see it mentioned anywhere there.
I saw that I can look for an object on the list by either its index or its value.
How can I look for an object by one of its properties? For example, how can I search for an object on the Floors arrayList using its ID?
For example, a user wants to delete a floor. The program asks for the ID of the floor they want to delete. Once the ID is entered, how can I look for the floor that has that ID?
Ok, probably the final question Ill need to ask here. Is it possible to alter the properties of objects in the list? I thought I could create a temporary object and insert there the properties of the object on the list, and then get any new value and replace the old value on the object. My question is how can I replace the object at the same spot the old one was at?
I read that removing an object from an arraylist "moves all indexes back one spot" (dont know how to explain it correctly)
So for example, the user selects the option to alter the properties of a floor. I have it set up so they enter the ID of the floor they want to alter, and a temporary Floor object is created, with the properties of the specified floor. Then the user enters what they want to alter.
I wanna know if it is possible to replace the object on the list, on the same index
you can do that with yourList.set(int index, E element) @vague wedge
it goes to the given index of the list and sets it to the given element
E is whatever type of variable the list can hold
np
Is it possible to add a blank object to an arraylist? As a like placeholder?
Because when an object gets removed from the arraylist, the indexes change, and that messes up everything Ive done so far
you could add null
floors.set(searchFor,null);Like so?
that would set it to an empty object
Hm
so if that's what you want then yes
It is what I wanted to do, but I am now left with many many problems
If you look at the last lines, I have set it so that every time 3 floors are created, it creates a garage
I uploaded your attachments as Gist.
Through a weird workaround I tinkered and made
How would that work with the removal/adding a null?
And if theres a better way to make the garages automatically, please let me know
so if you want to edit the properties of a floor, use the floors setters.
floors.get(2).setSomething("hi");. Don't create a new floor and replace the old object, you already have an object, just mutate its state
as for leaving the empty slots as null, its not a perfect idea, but not horrible, definitely at least comment that null means an empty slot. But if a floor always has a fixed size, you can just have a Vehicle[] slots
Also i would highly recommend updating the code such that int floorParked and int garageParked and instead int floorId and int garageId, much less confusing on what it represents
and currently Car and Truck have nothing in common other than the duplicate code. You'll need to employ some kind of polymorphism on them
Also I dont think you need a List<Floor> since you said each garage always has 3 floors?
Yeah it's better an array if the amount of items is the same always
I completely forgot about this, I should have done that
Floors dont have a fixed size, the size is given by the user when they run the code
And sadly, I know that car and truck are the same pretty much, but we have been specifically asked to have them as 2 different classes
And yes, each garage always has 3 floors
I used the arraylist on the floors because I need to store the floors, since a query on the project is being able to modify each floor individually
Yeah but car and truck need to have a common base class or interface if u want to store them in something besides an Object[]
But once a floor's size has been determined by the user, can it change? If so what happens to the existing cars
Otherwise you would have to do some wacky workaround and store an array/list of cars separately from the array/list of trucks so see if slots are available in both, rather than just checking a single list and updating a single list
I would also try to put the console interface logic in main ir elsewhere, and not in the data model methods
So after the UI creates a new car you can do something like currentFloor.addVehicle(userCreatedVehicle)
It can change, and Ill make it so that a changed size cannot be smaller than the current parked cars
So then i would just use an array because u get the null values for free on initialization. Then just replace the array with a new copy and manually adjusted values
But if u use an array and anything references it directly into another ref, like temp = thing.getVehiclesArray() then that temp variable would point to the old array when replacing. If u use lists u wouldnt have that issue, but would have to fill it with empty. Or you can use Arrays.asList(new Vehicle[5]) to initialize it. This is probably the best of both worlds
So tldr use a List<Vehicle> vehicles = Arrays.asList(new Vehicle[userDefinedSize]);
Otherwise when u say List<T> stuff = new ArrayList<>(userSize) u cant do stuff.set(5, t) as the 6th element doesnt exist
You would have to loop and manually call .add(null) userSize times
But with arrays.aslist it u can skip that