Search code examples
javasearchauthenticationfile-sharing

java file sharing application, user logging


OK I have so much questions regarding my file sharing application that I don't know where to start. My Java knowledge is very limited and I will be satisfied with any help you provide me.

That being said, here come the questions.

Firstly, I'm working on a user login method that needs to look sort of like this:

import java.io.File;
import java.util.ArrayList;


public class User {

    String username;
    String IPAdresa;

    public User(String username, String IPAdresa) {

         this.username = username.toLowerCase();
         this.IPAdresa = IPAdresa;

    }

    public void fileList() {

        ArrayList<String> list = new ArrayList<String>();

        File folder = new File("C:\\userfolder");

           File[] files = folder.listFiles();

           for (int i = 0; i < files.length; i++) {

                list.add(i, files[i].toString());

           }

    }
}

As you can see I have user class that contains parameters regarding user, such as username and IPAddress, and also fileList method that lists files from a certain folder and creates the arraylist containing those file names as strings.

The next thing I have to do is make a class or a method that provides search function to clients/users. For example, when users logs on to the application, he will want to search for a certain file and also he will provide the list of files from his shared folder to other users. The way I understood my menthor, the Request class needs to contain for each loops that are able to search within the users respective file lists. I'm not sure how to pull that off and I have a lot of problems regarding working with array lists.

This how it's supposed to look like approximately: (I'm using sort of pseudo-code for this one so far)

public class RequestForFile {

    ArrayList list = new ArrayList();
    User user = new User("Slavisha","123.23.34.45");

    public RequestForFile() {
        list.add(user);
            foreach (User user in userlist) {
              foreach (String str in User.fileList()) {
                  if (str == request)
                  ...
              }
            }

    }

}

Next question: How do users log in to Java application? I've been thinking about it all day and tried to get around it but I just failed. I don't have GUI/Swing yet, hope to do it in the end.

I have 3 more classes that represent Client, Server and HandleClient.

As I said any contribution is welcome. I will be back with more questions for sure. Thanks


Solution

  • You're asking too many questions in one question. One question concerns how your RequestForFile object knows which user it's dealing with. That's the log-in question. Suggest you raise that separately.

    Let's assume for the moment that we know it's Slavisha on that ipaddress who is asking. There's quite a few problems here:

    1). What's being asked for? RequestForFile() doesn't take any parameter to say which file is being requested. So what's the resposbility of RequestForFile()? is it suppsed to represent one request? Is it responsible for actually finding the file? Returning it?

    2). Your User.fileList() method doesn't actually do anything useful. It doesn't return anything, so on completion everything it has figured out it lost.

    3). Anyway, every user seems to be looking in the same folder: "C:\userfolder" did you intend to have a separate directory for each user.

    4). Looking through lists is probably not efficient, you need to read about Sets and Maps.

    I think you need a pretty detailed redesign. Your user class needs to answer questions such as "does this user have that file". The REquest class needs to identify a particular User and ask it for the file. I'm not clear how you then intend to transfer the file from client to server.

    I should say that doing Client/Server programming as an early education exercise is pretty ambitious.