I am creating a JavaFX Seating Arrangement APP
@FXML
void addStudentClicked(ActionEvent event) {
String str = studentname.getText();
Random random = new Random();
int randomInt = random.nextInt(9);
if(randomInt == 0){
if(str.isEmpty()){
errorLabel.setText("ERROR");
}
else{
studentname1.setText(studentname.getText());
student1.setFill(studentcolour.getValue());
}
}
else if(randomInt == 1){
if(str.isEmpty()){
errorLabel.setText("ERROR");
}
This is my code that so far will take the user input and apply it to a random seat, it continues with else if's for each seat is there a cleaner way for me to do this then with all the else if's and how can I check to see if the seat is already filled so I am not filling the same seat twice?
Also is there a way to check if the colour has already been taken?
CodePudding user response:
An interesting alternative to using the random function is to put all your choices in a List
and then apply the Collection
method shuffle(). Then, you can iterate through the list. This ensures that each option is chosen once and only once.
I haven't looked closely enough at the code to tell if a switch/case construct would be a better choice, but that is always something to consider.
CodePudding user response:
Here is a similar (deliberately not the same) example just using the console.
I leave it to you to create a GUI.
Hints
- Writing GUIs is not the same as writing console apps because GUIs are event-driven, so you need to get rid of some loops and act on events.
- You can use ObservableLists to have list changes reflected in the UI.
- When you remove something from an ObservableList, it will no longer be in the list, so if it is bound to something in the UI, it will no longer be there
- e.g. if it is a color selection list and a color is removed from it, the color will no longer be available for selection.
Shuffle a list, then pick from the shuffled list
This is the same idea as physically shuffling a deck of cards, then grabbing the cards in order from top to bottom.
The basic idea is that one way to solve this problem is to apply:
You could also study:
But you don't need to write the shuffle algorithm yourself, you can just use the one already implemented in the java collection library.
Collections.shuffle(seatNumbers);
Example app
Console app-based example.
I'm sure the example uses some concepts you aren't familiar with, and if you use them, you should understand them or do them a different way.
Try running the sample application and look at the output to see what it did, then try to think about how you might apply similar concepts to your program.
import javafx.scene.paint.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class MusicalDragons {
public static final int NUM_ROUNDS = 3;
public static void main(String[] args) {
MusicalDragons game = new MusicalDragons();
for (int i = 0; i < NUM_ROUNDS; i ) {
System.out.println("Round " (i 1));
System.out.println("=========");
game.play();
System.out.println();
}
}
private final List<String> dragons;
private final List<Color> palette;
private final List<Integer> seatNumbers;
private final int numSeats;
public MusicalDragons() {
// generate a list of names.
dragons = Arrays.asList(
"Oyositatu (Old Dragon)",
"Tamasipionosi (Soul Master)",
"Timatamï (Crossroad Snake)",
"Tômiarôzi (Wealth Master)",
"Ararepemi (Hail Snake)",
"Umasumera (Horse Emperor)",
"Umitatu (Sea Dragon)",
"Akitatu (Autumn Dragon)",
"Tawayatatu (Weak Dragon)",
"Amoritatu (Descend From Heaven Dragon)"
);
numSeats = dragons.size() - 1;
// generate a list of colors.
palette = new ArrayList<>();
for (int i = 0; i < numSeats; i ) {
palette.add(
Color.RED.interpolate(
Color.VIOLET,
i * (1.0 / (numSeats - 1))
)
);
}
// generate a list of seat numbers.
seatNumbers =
IntStream.range(0, numSeats)
.boxed()
.collect(
Collectors.toCollection(
ArrayList::new
)
);
}
private void play() {
// randomize the order of everything.
Collections.shuffle(palette);
Collections.shuffle(seatNumbers);
Collections.shuffle(dragons);
// display the seat assignments.
for (int i = 0; i < numSeats; i ) {
System.out.println(
dragons.get(i) "\n"
" gets seat #" seatNumbers.get(i) "\n"
" colored " palette.get(i)
);
}
// hmm, one dragon missed out . . .
System.out.println();
System.out.println(
dragons.get(numSeats)
" does not get a seat and is very angry."
);
}
}
Output
Round 1
=========
Tawayatatu (Weak Dragon)
gets seat #1
colored 0xf64884ff
Tamasipionosi (Soul Master)
gets seat #4
colored 0xff0000ff
Oyositatu (Old Dragon)
gets seat #7
colored 0xfd0e1aff
Ararepemi (Hail Snake)
gets seat #0
colored 0xf074d4ff
Umasumera (Horse Emperor)
gets seat #8
colored 0xfb1d35ff
Amoritatu (Descend From Heaven Dragon)
gets seat #3
colored 0xf73a6aff
Timatamï (Crossroad Snake)
gets seat #2
colored 0xf4579fff
Umitatu (Sea Dragon)
gets seat #5
colored 0xf265b9ff
Tômiarôzi (Wealth Master)
gets seat #6
colored 0xf92b4fff
Akitatu (Autumn Dragon) does not get a seat and is very angry.