Question

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

Was it helpful?

Solution

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...

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top