insert into an array from an arraylist - java

i am doing a hotel room booking system.
i created two array list which is days and rooms.
i created buttons to add value into array list.
example: if monday clicked, then ("monday") inserted into days's array list.
if monday and tuesday clicked, then ("monday") and ("tuesday) inserted into days's array list.
now after all the information is done how do i insert the array list which is days and rooms
into a array, which this array is to function is to store these information as a booking, then use this array to compare with another booking array weather the room being booked or not

As per my understanding of the question you are trying to insert two different ArrayLists into one Array ??
If you are trying to do so.. that is not a good way to do it.. instead create a class having variables as day and room get values in object of that class.. than store it in a array..
hope this helps..

jvaa.util.Map would be a good choice, put the room as the KEY, put a List which contains all reservation date as VALUE.
Reservations:
{
room-1 = [Monday, Tuesday], room-2 = [Sunday], room-3 = []
}
In case you want to check whether a room is booked or not, just retrieve the corresponding List and check size of the List, if the List is empty then the room is not reserved at all.
Exampe for checking reservation of room_1
Map.get(room_1).isEmputy()
For you second comment
Q1: Map.get(room_1).add(DAY-1), for reservation ROOM-1 in DAY-1
Q2:Depends on how do you want to model it. In case you have 3 floors, it is able to index FLOOR/ROOMS in another KEY-VALUE data type , so that you can firstly find all rooms in specific floor by the floor key, then try to add a new room in that floor.
{ FLOOR-1 : [ROOM-1, ROOM-2, ROOM-3, ...], FLOOR-2 : [ROOM-4, ROOM-5, ...]}

Related

How do I assign names to variables in an arraylist?

I am doing a project for my AP Computer Science class. The current project asks us to create a rip off version of excel that prints to the console. I am wondering if theres a way for me to create an arraylist of empty values that I can assign cell names to (A1, A2, F6, etc.) and then use those names to call values from their place in the arraylist. For example, I'd give the cell D4 a value of 6, store that 6 in an arraylist, then call it back using "D6".
A link to the directions of the project: https://issaquahwednet-my.sharepoint.com/:w:/g/personal/stutlerk_issaquah_wednet_edu/EQOW8BFzHIhIsdvXGP-qKDsBbN7BFa-kUCiMeeq9BZbbwg?e=Nc1Jcs
Maybe I'm not making sense, for which I am very sorry.
//what i would like to be able to do:
arraylist "cells" = (A1 through L20)
user input = "A1 = hi"
set index "A1" of "cells" to "hi"
repeat that stuff until user input = quit
is this possible? or would I just have to create two arraylists, one for cell numbers and one for values? I guess I could do that and then if a user wants to see what a cell says then they could Say "A1" and I could search my cell arraylist for a value of A1 then compare that index with the other arraylist.
I'm completely lost one this and, again, sorry if I'm not making any sense.
To do what you want, you'd need a map rather than a list. Lists always have integer numbers as the indices and there's no way to change that. (You could technically do this with two arrayLists, but it would be a pain to maintain it so that "A1" and "hi" stayed in the same index on two different lists if you ever had to delete/move anything.)
The HashMap documentation is here: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
And you'd do something like this:
Map<String, String> cells = new HashMap<String,String>();
cells.put("A1", "hi");
That would create a key-value pair with key "A1" and value "hi".
You can use structure map where for every key you have associated a value.
https://docs.oracle.com/javase/8/docs/api/java/util/Map.html

How to read/update values stored inside a map which is then inside of an array in Cloud Firestore?

To begin with I'm a newbie to Firestore and Android programming.
I have a collection which stores documents, each document contains an array of semesters, inside the semester array there will be an array of maps representing each semester. Inside the individual semester maps, there will be a number field identifying the semester and also another array called Students where it will contain an array of maps where each map will represent each student and store three different fields: ID of the student, DaysPresent, Total.
I'm trying to find a way to read the data for each individual student represented by ID in such a way where the semester number is taken into account.
For example, if the user selects Semester 1, it will go inside the array Semester and then load up the values from the first array which represents the Semester 1, and it will then display all the maps that represent each student where the days present and the total can be modified and read by the user.
One crucial key point is that the total number will always be the same for every individual map which means every individual student.
Edit: Mar 26th, 2020:
Actually it's possible using the solution in the following article:
How to map an array of objects from Cloud Firestore to a List of objects?
If you intend to get a List<Semester> or a List<Student> when querying your Cloud Firestore database, please note that this is currently not possible. Even if those objects are of type Semester and Student, there is no way you can map them into a List of Semester/Student objects. To solve this, you need to write code to iterate those objects so you can convert them from a List<Map> to a List<Semester>and List<Student>. So you need to apply your own logic for that.
Edit:
The type of object that is returned when calling:
document.getData();
Is a Map<String Object>, so you need to iterate through the map according to your logic and get the data as needed.

How to remove item from ArrayList, then add back to place of removal?

How do you remove from an ArrayList at a particular index then add back to that same index without the removal causing the ArrayList to compensate for the loss of them item and moving the empty space to the end of the array list?
I've tried:
public void dischargePatient(int bedNumber) {
if (bedNumber < beds.size()) {
beds.remove(bedNumber);
}
}
But this moves the bed at bedNumber to the end of the ArrayList after removing the patient from the bed. How do I keep it at bedNumber?
You want to use a Map object instead. I'm a little rusty at java, but you can define a map of beds like this:
HashMap<Integer,Boolean> occupiedBeds;
and then you can check if an entry is true to see if a bed is occupied, and then set it to false when you discharge a patient. You can initialize the occupiedBeds to a range of false values, or you can just assume an "unset" state means that nobody is in the bed.
You just aspecify the index you want - beds.add(bedNumber, patient);
This pushes the patients that were prior to the addition in locations bedNumber and later to bedNumber+1 ...., thus bringing your list back to the original order.
public void add(int index,
E element)
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
You can use beds.get(bedNumber) and beds.set(bedNumber, patient).
If you want to keep on using an ArrayList, then you can use the method "set(index, null)" instead of invoking the method "remove(index)". This will ensure that the your order does not change.
Example:
0 1 2 3
ArrayList items: [ A, B, C, D ]
items.set(2, null);
0 1 2 3
items: [ A, B, null, D ]
I know this doesn't answer the question asked, but using a list in such a way, expecting the index to reflect the bed number while also expecting the list to define if a bed is taken is a bit broken, or at least highly troublesome. The examples below show how it is easier to manipulate sets and maps and they provide extra information such as total occupied beds without having to code that up yourself.
I suggest you use a Map of Patients rather than a list.
Map<Integer,Patient> patientBeds = new HashMap<Integer,Patient>();
patientBeds.add(bedNumber, patient);
patientBeds.remove(bedNumber);
patientBeds.contains(bedNumber); // is this bed taken?
patientBeds.size(); // total occupied beds, regardless of their numbers
If you don't need the Patient objects, but simply to know which beds are occupied, use a simple Set of Integers
Set<Integer> set = new HashSet<Integer>();
set.add(bedNumber); // help me nurse
set.remove(bedNumber); // I'm going home!
set.contains(bedNumber); // is this bed taken?
set.size(); // total occupied beds, regardless of their numbers
With either of these solutions, your class which holds the Map or Set might have a member which specifies the total number of available beds in the ward. I wouldn't try to build this concept into the Map such as populating the Map with "null" patients.
int totalBeds = 10;
if (bedNumber > totalBeds) {
// send them to the next ward
}

Working with data from table

I am just wondering if I could get some advice on implementing an algorithm for creating a JFreeChart. Basically, I am extracting some data from a JTable which contains information about patients. There are age categories for all patients such as 20-26, 26-30, 31-35, 35-40, 50-55, 55-60 etc. There are about 30 of them and every patient belongs to their corresponding age category. There is a button on top of the JTable which opens a frame containing the age distribution graph. What I am thinking of doing:
Create an integer variable for every category
Loop through the age details for all patients in the JTable
Compare variables with the JTable data and increment by 1 if there is a match (lots of if statements to go in the loop)
Store the categories names and the amount of people registered under every category in a HashMap
Pass the map to the ChartFrame
I suppose this might be a relatively good way of doing this but I was wondering if somebody could possibly suggest a way of avoiding having to create a variable for every category and then having to do all those comparisons using about 30 if statements.
EDIT: I do not have the patient's exact age - only the category they belong to.
I've assumed you've got your own class AgeRange which stores a range of ages. Then what you can do is store the age ranges in a TreeMap<Integer,AgeRange>, where the key is the first number of the range and the value is the range itself.
When you need to find which age range contains a particular age, use
theMap.lowerEntry(age + 1)
to find it.
Check the TreeMap Javadoc at http://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html
Use the SimpleHistogramDataset class in JFreeChart. Add one SimpleHistogramBin for each of your age ranges, and then call the addObservation() method for each person's age. Once you are done, you have a working dataset - it implements the IntervalXYDataset interface so you can, for example, pass it to the createXYBarChart() method to create a histogram.

What data structure to choose for an ELO rating system with 20k+ entries?

I have some Objects (currently 20 000 +) that have the following Attributes
Object
----------
String name
int rating
I want to create an ELO rating for all these Objects. This implies the following:
To adjust the ELO of 2 Objects matched against each other I need to find those Objects in the list by name.
To Display the List I need to get every Object ordered by its rating.
The whole program will be implemented in Java, but I think its independent from the programming language.
Currently I am unsure which data model to choose for this Project. A friend advised me to use a 2-4 tree to insert the Objects ordered by name so I can change the rating of each object fast.
However the Objects are printed in order of their rating rather than by name and I don't want to sort so many Objects every time I output the list.
As I am pretty new to data structures: What is the usual way to solve this problem?
Creating another tree ordered by rating?
Having a list of ratings and each rating linked to each Object currently having that rating?
Edit: ELO rating is a mapping from the set of objects to the integers. Each object only gets one rating but a rating can have multiple Objects associated with it.
Creating another tree ordered by rating? Having a list of ratings and each rating linked to each Object currently having that rating?
Well , this is one way to do so , but will take huge space also since you have 20K+ entries .
The best way i can think of now is :
Use datastructure like multimap with key=name , and value = ratings.
This way , everytime you insert a new object in multimap , it will take O(logN) time .
To find all ratings with same name use equal_range , which is also an O(logN) operation .
Hope this helps !
A HashMap with name as the key will give you O(1) performance when matching the elements, keep a TreeSet with a rating Comparator and you'll have the items in order. Although you'll need to reinsert an element if the rating changes.

Categories