0

I'm trying to check does inputed room number exist in array list:

private void initAction(RoomsWindow rw, RoomAddUpdateWindow raw) {
            btnConfirm.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    ArrayList<Room> rooms = RoomsWindow.loadRoom();
                    for (Room r : rooms) {
                        r.getNumber();
                    }
                    if (rooms.equals(txtNumber.getText())) {
                        JOptionPane.showMessageDialog(null, "Room number already exists!", "Add",
                                JOptionPane.PLAIN_MESSAGE);
                    } else {
                        File fileRooms = new File("src/txt/rooms");
                        try {
                            FileWriter fw = new FileWriter(fileRooms, true);
                            BufferedWriter bw = new BufferedWriter(fw);
                            PrintWriter pw = new PrintWriter(bw, true);
                            String tv = (String) cbTV.getSelectedItem();
                            if (cbTV.getSelectedItem().equals("Yes")) {
                                tv = "true";
                            } else {
                                tv = "false";
                            }
                            String miniBar = (String) cbMiniBar.getSelectedItem();
                            if (cbMiniBar.getSelectedItem().equals("Yes")) {
                                miniBar = "true";
                            } else {
                                miniBar = "false";
                            }
                            pw.println(txtNumber.getText() + "|" + txtType.getText() + "|" + txtName.getText() + "|"
                                    + txtBeds.getText() + "|" + tv + "|" + miniBar + "|" + "false" + "|" + "false");
                            pw.close();
                            JOptionPane.showMessageDialog(null, "Succesfully", "Done", JOptionPane.PLAIN_MESSAGE);
                            raw.setVisible(false);
                            rw.setVisible(false);
                            RoomsWindow newRoomWindow = new RoomsWindow();
                            newRoomWindow.setVisible(true);

                        } catch (IOException exc) {
                            exc.printStackTrace();
                        }
                    }
                }
            });
        }

If it exist then i don't want to write in file, just to print a message in

dialog: OptionPane.showMessageDialog(null, "Room number already exists!", "Add",
                                    JOptionPane.PLAIN_MESSAGE);

EDIT Here is my Room class: package entities;

public class Room {

    int number;
    String type;
    String name;
    int beds;
    Boolean tv;
    Boolean miniBar;
    Boolean ocupied;
    Boolean deleted;
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getBeds() {
        return beds;
    }
    public void setBeds(int beds) {
        this.beds = beds;
    }
    public Boolean getTv() {
        return tv;
    }
    public void setTv(Boolean tv) {
        this.tv = tv;
    }
    public Boolean getMiniBar() {
        return miniBar;
    }
    public void setMiniBar(Boolean miniBar) {
        this.miniBar = miniBar;
    }
    public Boolean getOcupied() {
        return ocupied;
    }
    public void setOcupied(Boolean ocupied) {
        this.ocupied = ocupied;
    }
    public Boolean getDeleted() {
        return deleted;
    }
    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }
    public Room(int number, String type, String name, int beds, Boolean tv, Boolean miniBar, Boolean ocupied,
            Boolean deleted) {
        super();
        this.number = number;
        this.type = type;
        this.name = name;
        this.beds = beds;
        this.tv = tv;
        this.miniBar = miniBar;
        this.ocupied = ocupied;
        this.deleted = deleted;
    }

}

Else, ofcourse, I want to write in file succesfuly. Where am I wrong?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Neo Cortex
  • 47
  • 1
  • 11

2 Answers2

2

The "simplest" route would require you to loop over each Room in the rooms List and compare the roomNumber with the Room's room number

Something like...

List<Room> rooms = RoomsWindow.loadRoom();
// This may throw an exception, so you'll want to deal with that
String roomNumber = Integer.parseInt(txtNumber.getText());
boolean found = false;
for (Room room : rooms) {
    if (room.getNumber() == roomNumber) {
        found = true;
        break;
    }
}

nb: I don't have your Room class so I had to "guess" at it's functionality

A "sleeker" way might be to make use of the stream's API

Room match = rooms.stream().filter(room -> room.getNumber() == roomNumber).orElse(null);
if (match != null) {
    // Found room
} else {
    // Did not find foom
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • @NeoCortex Updated – MadProgrammer Jun 07 '18 at 04:21
  • Now it found if number exist, but when number does not exist I get this error: `Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException: No value present` on line: `Room match = rooms.stream().filter(room -> room.getNumber() == roomNumber).findFirst().get();` Sleeker way is pretty much cool, but I never work on that way, so don't know what to change. I suppose the this error is in detail. :D Btw, I don't know why I can't mention your username. :D – Neo Cortex Jun 07 '18 at 04:36
  • First time dealing with optionals in Java, had hopped it might return a `null`, but no, that would be to easy. I've tested and update it – MadProgrammer Jun 07 '18 at 04:39
  • I did it. :D Jus instead of `.get()` write `.orElse(null)` Update that, so I could acept answer. :P – Neo Cortex Jun 07 '18 at 04:43
  • @NeoCortex Sweet, nice find – MadProgrammer Jun 07 '18 at 04:45
0

Hope This Will Help

In this way you can check the existing value in your list.

if(rooms.contains(txtNumber.getText())){
    ///Do Not add
    }else{
    //add in List
    }

All the Best.

Aman Deep
  • 381
  • 4
  • 16