okay so this is my server class
import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List; public class Server { private ServerSocket server; private static List<Accomodation> allAccommodations = new ArrayList<>(); public static void main(String[] args) { new Server().openServer(); } public synchronized static void addAccommodationToServer(Accomodation newAccommodation) { allAccommodations.add(newAccommodation); } public static List<Accomodation> getAllAccommodations() { return allAccommodations; } void openServer() { try { server = new ServerSocket(1234); while (true) { Socket providerSocket = server.accept(); Thread t = new ActionsForClients(providerSocket, null); t.start(); } } catch (IOException ioException) { ioException.printStackTrace(); } } }
this is my client class
and this is my client user classimport java.io.*; import java.net.*; import java.util.ArrayList; import java.util.List; import java.util.NoSuchElementException; import java.util.Scanner; public class Client extends Thread { private ObjectInputStream in; private ObjectOutputStream out; private Scanner scanner; private static List<Accomodation> accomodations; public Client() { scanner = new Scanner(System.in); accomodations = new ArrayList<>(); } public void run() { try (Socket requestSocket = new Socket("localhost", 1234)) { out = new ObjectOutputStream(requestSocket.getOutputStream()); in = new ObjectInputStream(requestSocket.getInputStream()); while (true) { displayMenu(); int choice = readChoice(); processChoice(choice); if (choice == 4) { System.out.println("Exiting program..."); break; } } } catch (UnknownHostException unknownHost) { System.err.println("You are trying to connect to an unknown host!"); } catch (IOException ioException) { System.err.println("Error communicating with server: " + ioException.getMessage()); ioException.printStackTrace(); } finally { closeStreams(); } } public synchronized static List<Accomodation> getAccomodations() { return accomodations; } private void displayMenu() { System.out.println("Welcome back Manager."); System.out.println("What would you like to do?"); System.out.println("Press 1 to add a new Accommodation."); System.out.println("Press 2 to add new dates for an existing accommodation."); System.out.println("Press 3 to see reservations for all accommodations."); System.out.println("Press 4 to exit the menu."); System.out.println("Enter a number:"); } private int readChoice() { while (!scanner.hasNextLine()) { try { Thread.sleep(100); // Sleep briefly to avoid busy-waiting } catch (InterruptedException e) { e.printStackTrace(); } } try { return Integer.parseInt(scanner.nextLine().trim()); } catch (NoSuchElementException | NumberFormatException e) { System.err.println("Invalid input. Please enter a number."); return -1; } } private synchronized void addAccommodation() { try (Scanner scanner = new Scanner(System.in)) { System.out.println("Enter Room name:"); String roomName = scanner.nextLine(); System.out.println("Enter Number of persons:"); int noOfPersons = Integer.parseInt(scanner.nextLine()); System.out.println("Enter Area:"); String area = scanner.nextLine(); System.out.println("Enter Stars:"); int stars = Integer.parseInt(scanner.nextLine()); System.out.println("Enter Number of reviews:"); int noOfReviews = Integer.parseInt(scanner.nextLine()); Accomodation newAccommodation = new Accomodation(roomName, noOfPersons, area, stars, noOfReviews, null, null); accomodations.add(newAccommodation); System.out.println("Accommodation added to your accommodation list successfully."); Server.addAccommodationToServer(newAccommodation); } catch (NumberFormatException e) { e.printStackTrace(); } } private void addAvailableDates() { if (accomodations.isEmpty()) { System.out.println("You have no accommodations in your list."); } else { System.out.println("\nYour Accommodation List:"); for (int i = 0; i < accomodations.size(); i++) { System.out.println((i + 1) + ". " + accomodations.get(i).getRoomName()); } System.out.println("Choose the accommodation that you want to add new available dates to"); try (Scanner scanner = new Scanner(System.in)) { int choiceacc=scanner.nextInt(); System.out.println("You chose "+accomodations.get(choiceacc).getRoomName()+"\nHow many dates do you want to add?"); int datescounter=scanner.nextInt(); for (int i = 0; i < datescounter; i++) { System.out.println("Type the date you want to add:"); String newDate = scanner.nextLine(); accomodations.get(choiceacc).addAvailableDate(newDate); } } } } private void SeeReservations() { for (Accomodation accomodation : accomodations) { System.out.println("Room: " + accomodation.getRoomName()); System.out.println("Taken Dates:"); if (accomodation.TakenDates != null) { for (String date : accomodation.TakenDates) { System.out.println(date); } } else { System.out.println("No reservations for this room."); } System.out.println("Available Dates:"); if (accomodation.AvailableDates != null) { for (String date : accomodation.AvailableDates) { System.out.println(date); } } else { System.out.println("All dates are reserved for this room."); } System.out.println("--------------------------------"); } } private void processChoice(int choice) { switch (choice) { case 1: System.out.println("You pressed 1"); addAccommodation(); break; case 2: System.out.println("You pressed 2"); addAvailableDates(); break; case 3: System.out.println("You pressed 3"); SeeReservations(); break; case 4: break; default: System.out.println("Invalid choice. Please enter a number between 1 and 4."); } } private void closeStreams() { try { if (in != null) in.close(); if (out != null) out.close(); scanner.close(); } catch (IOException e) { System.err.println("Error closing streams: " + e.getMessage()); } } public static void main(String[] args) { new Client().start(); } }
I know my code is bad and sloppy but i'm new to this and its my first time trying anything with a server so please don't be disrespectful.import java.io.*; import java.net.*; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class ClientUser extends Thread { private ObjectOutputStream out; private ObjectInputStream in; private Scanner scanner; public ClientUser() { scanner = new Scanner(System.in); } public void run() { try (Socket requestSocket = new Socket("localhost", 1234)) { out = new ObjectOutputStream(requestSocket.getOutputStream()); in = new ObjectInputStream(requestSocket.getInputStream()); while (true) { displayMenu(); int choice = readChoice(); processChoice(choice); if (choice == 4) { System.out.println("Exiting program..."); break; } } } catch (UnknownHostException unknownHost) { System.err.println("You are trying to connect to an unknown host!"); } catch (IOException ioException) { System.err.println("Error communicating with server: " + ioException.getMessage()); ioException.printStackTrace(); } finally { closeStreams(); } } private void displayMenu() { System.out.println("Welcome back Manager."); System.out.println("What would you like to do?"); System.out.println("Press 1 to search for an accommodation."); System.out.println("Press 2 to make a reservation."); System.out.println("Press 3 to review an accommodation."); System.out.println("Press 4 to exit the menu."); System.out.println("Enter a number:"); } private int readChoice() { try { return Integer.parseInt(scanner.nextLine().trim()); } catch (NumberFormatException e) { System.err.println("Invalid input. Please enter a number."); return -1; } } private void processChoice(int choice) { switch (choice) { case 1: System.out.println("You pressed 1"); List<Accomodation> testingloco = new ArrayList<>(); testingloco=Server.getAllAccommodations(); if(testingloco!=null) { System.out.println("NOT CRINGE"); for (Accomodation testingloc:testingloco) { System.out.println(testingloc.getRoomName()); } } else { System.out.println("CRINGE"); } break; case 2: System.out.println("You pressed 2"); // Handle making a reservation break; case 3: System.out.println("You pressed 3"); // Handle reviewing an accommodation break; case 4: break; default: System.out.println("Invalid choice. Please enter a number between 1 and 4."); } } private void closeStreams() { try { if (in != null) in.close(); if (out != null) out.close(); scanner.close(); } catch (IOException e) { System.err.println("Error closing streams: " + e.getMessage()); } } public static void main(String[] args) { new ClientUser().start(); } }
Okay so the problem i am facing is that when i run my server and connect from the client class i can add a new accommodation to his personal list but when i try to add it to the server's list of all the accommodation nothing happens im in the process of trying to add some options to the clientuser's menu like filtering all of the accommondations by certain criteria but i can't do it if this doesn't work.
If anyone takes the time to see this code and help me a bit that would be awesome.