views:

198

answers:

3

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

A: 

I'm not quite sure what you mean by 'logging in'. If (as I believe) you want to identify the user in your Java application, use:

System.getProperty("user.name");

which gives you the user name of the user currently running your application.

Brian Agnew
A: 

A GUI would be used to give the user something other than the command line to look at, but Swing or AWT don't provide the functionality you seem to associate them with - namely, connecting the Server and Client, allowing the user to log into the running instance of the Server. The GUI would make this process more user-friendly, but fundamentally you're talking about using sockets to let the Client connect to the Server (known IP address and open port set for the connection), sends some of the User's information (the names of shared files) and then receives a list of all possible shared files from the Server. The Client can then locally search through this list.

You can do this the other way around - the Server gets a request from the Client, does the searching, and just sends a result to the Client. Depends on where you want to put most of your workload. Either way, you're talking about sending Strings through a socket connection. At least, until you go from looking at lists of file names to actually sending the files from one Client to another.

So you probably want to start looking at a tutorial on sockets to look at how two separate programs on different computers can send information to one another.

char
I realized just now that Clients are equal to users and I understand that the GUI is there solely for the feeling of logging in. The real connection is created using sockets. I believe my Server class and Client class are nicely done. I've fixed RequestForFile and User classes, will ask another question shortly.
AmateurProgrammer
A: 

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.

djna
RequestForFile() method I have given you is incomplete, I though someone could explain to me how to complete it exactly. It is because I haven't made myself clear enough. I'll try to be more precise this time. I have a new code for RequestForFile() and User class.3) sure, every user is supposed to have its own shareFolder. Will deal with that later, doesn't seem like major problem.4) Don't know about Sets and Maps, but I'm pretty sure I HAVE TO use ArrayLists, not my choice anyway :(
AmateurProgrammer
It is pretty ambitious, but this assignment is inevitable for me, I have to finish this in the next couple days, even if I had to stay awake all night long.Thank you, by the way, you've been helpful. I hope you'll help me in my next question regarding the similar matter.
AmateurProgrammer