1

I am trying to create a new object and initialize it with the parameters which are passed by the following command :

java -jar JAR-FILE.jar store Information.dat ClientName "Address" City Country HomePhone OfficePhone CellPhone

where ClientName is the name of the client , "Address" contains the client's address and etc. The client can have a HomePhone or a OfficePhone or a Cellphone or all of these and even more phone numbers.

here is the class which I tried to initialize it with parameters :

private static void SaveClient(String[] args) throws Exception 
{
    Client SaveClient = new Client(...);
    ....
    out.writeObject(SaveClient);
    out.close();

}

here is the Client constructor :

public class Client{

private String ClientName;
private Address address;
private List<String> PhoneNumbers;

public Client() {
    this.PhoneNumbers = new ArrayList<String>();
}

public Client(String ClientName, Address address) {
    this();
    this.name = ClientName;
    this.address = address;
}

public void AddPhoneNumber(String number) {
    this.PhoneNumbers.add(number);
}
.... 
}

but I don't know how to initialize it with the passing parameters. We can assume the information is valid, also I don't want to make any changes to the "Client" constructor just modifying the SaveClient

user3390377
  • 13
  • 1
  • 4

3 Answers3

0

Use the String[] args, convert these arguments as needed
(e.g. to int, long, String, or to whatever you need), and
then pass them to the Client constructor.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
0

Assuming your client Class has all the needed properties and the args array elements positioning is known you can go for something like this

Public Client (String[] args){
    this.address = args[6]; // Based on your edit this will need to be an Address object. may look something like: this.address = new Address(args[6]);  assuming Address object has a constructor that accepts String
    ....
}

This can get very ugly if the order of the string array is not correct or things are in the wrong place or not there at all. You will also have to do some casting (converting value types) if the Client property you are trying to save is an int or double or what have you, since the args element is coming as Strings only

Marquis Blount
  • 7,585
  • 8
  • 43
  • 67
0

First, you need to determine if the number of supplied arguments meet your expected needs...

// This assumes that address, city, country, home phone, 
// office and cell phone are optional
if (args.length >= 3) {...

Or

// This assumes that all the values are required...
if (args.length >= 9) {...

If either of these cases are untrue (based on your needs), then you should display some kind of error message and probably exit...

You then need to extract the values from args...

// This assumes that some of the arguments are optional...
String clientName = args[2];
String address = args.length > 3 ? args[3] : null;
String city = args.length > 4 ? args[4] : null;
String country = args.length > 5 ? args[5] : null;
String homePhone = args.length > 6 ? args[6] : null;
String officePhone = args.length > 7 ? args[7] : null;
String cellPhone = args.length > 8 ? args[8] : null;

Or

// This assumes that all the parameters are mandatory
String clientName = args[2];
String address = args[3];
String city = args[4];
String country = args[5];
String homePhone = args[6];
String officePhone = args[7];
String cellPhone = args[8];

Then create your Client object...

Client client = new Client(
    clientName,
    address,
    city,
    country,
    homePhone,
    officePhone,
    cellPhone);

This of course will mean that your Client object will require a constructor capable of accepting the information your providing. Your example Client won't work, apart from the fact that it has a Person constructor which is invalid, it has no means to accept all this information...

public class Client{

    private String ClientName;
    private Address address;
    private List<String> PhoneNumbers;

    public Client() {
        this.PhoneNumbers = new ArrayList<String>();
    }

    public Client(String clientName, String address, String city, String country, String homePhone, String officePhone, String cellPhone) {
        this();
        ClientName = clientName;
        address = new Address(...); // No idea of the parameters for this...
        AddPhoneNumber(homePhone);
        AddPhoneNumber(officePhone);
        AddPhoneNumber(cellPhone);
    }

For example...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • sorry I am lost, we can assume the user enters valid information – user3390377 Mar 06 '14 at 23:51
  • So all the fields are mandatory? Then follow the mandatory branch of comments... – MadProgrammer Mar 06 '14 at 23:52
  • can we create a loop to extract the values? – user3390377 Mar 06 '14 at 23:54
  • Sure, but that will change the way that you construct the parameters for the `Client` object and the possible the constructor itself – MadProgrammer Mar 06 '14 at 23:57
  • I was reading this example [link] (http://stackoverflow.com/questions/18446509/args-length-and-command-line-arguments) and I though we could use `for(int i = 0;i – user3390377 Mar 07 '14 at 00:03
  • The issue is determining which elements map to which property of the `Client`, this is a dangerous approach as developers could send you anything. A better approach is to request the caller pass each variable you expect as separate parameter – MadProgrammer Mar 07 '14 at 00:05
  • but the thing is that the user can enter 1 phone number, 2 phone numbers or 2+ phone numbers - so I thought using loop would be a better approach – user3390377 Mar 07 '14 at 00:10
  • That's a fair point, but how do you know which one is a phone number and which isn't? A better solution might be to use `SaveClient(String clientName, String address, String city, String country, String... phoneNumbers) {...`, which allows for a variable number of phone numbers, which you can access as simple array from within the method – MadProgrammer Mar 07 '14 at 00:12