在类之间传递带有getter和setter的userInput

时间:2019-04-20 19:10:23

标签: java

我的getter返回null。我正在尝试收集用户输入,并将其发送到另一个类,通过使用getter和setter进行显示。在ClientInput类中,我正在收集用户输入,并在设置器中为实例变量分配Client类中的用户输入值。然后从那里我希望从ClientCalculations类访问实例变量的值。当我尝试使用它时,由于我正在创建新实例,因此给我一个空值。如何从另一个类获取用userinput设置的值?希望我觉得我是Java新手。

public class Client{
    //Instance variables for client information
    private String ClientName;
    private String ClientLawnWidth;
    private String ClientLawnLength;
    private String ClientPaymentMethod;
    private String ClientQuestion;

    /*
    *Getter and setter for Client name
    *
    *
    */

    public void setName(String ClientName) {
        this.ClientName = ClientName;
    }
    public String getName() {
        return ClientName;
    }

    /*
    *Getter and setter for Client name
    *
    *
    */

    public void setClientLawnWidth(String ClientLawnWidth) {
        this.ClientLawnWidth = ClientLawnWidth;
    }
    public String getClientLawnWidth() {

        return ClientLawnWidth;
    }
    /*
    *Getter and setter for Client name
    *
    *
    */

    public void setClientLawnLength(String ClientLawnLength) {
        this.ClientLawnLength = ClientLawnLength;
    }

    public String getClientLawnLength() {
        return ClientLawnLength;

    }

    /*
    *Getter and setter for Client Payment Method
    *
    *
    */

    public void setClientPaymentMethod(String ClientPaymentMethod) {
        this.ClientPaymentMethod = ClientPaymentMethod;
    }

    public String getClientPaymentMethod() {
        return ClientPaymentMethod;
    }       

    /*
    *Getter and setter for Client Question
    *
    *
    */

    public void setClientQuestion(String ClientQuestion) {
        this.ClientQuestion = ClientQuestion;
    }

    public String getClientQuestion() {
        return ClientQuestion;
    }       

    public String display() {
        return "Client Name: " + getName()
                + "\n Client Lawn Width: " + getClientLawnWidth()
                + "\n Client Payment Method: " + getClientPaymentMethod();
    }
}

public class ClientInput {
    private  ArrayList<Client>    Clients;
    private  InputHelper  input;

    public void run() {
    clientDataEntry();
    displayClients();
    }

    public void clientDataEntry() { 
    String ClientName = ""; 
    String ClientLawnWidth = "";    
    String ClientLawnLength = "";   
    String ClientPaymentMethod = "";    
    String ClientQuestion = ""; 
    String  more          = "";
    Client     newClient        = null;
    Clients = new ArrayList();

    while (true) {
            input = new InputHelper();
            ClientName = input.getUserInput(
                    "Enter the name of The Client");
            ClientLawnWidth = input.getUserInput(
                    "Enter the width of clients lawn in yards");
            ClientLawnLength = input.getUserInput(
                    "Enter the length of clients lawn in yards");
            ClientPaymentMethod = input.getUserInput(
                    "Enter your payment method, 1,2,or 22 payments");
            newClient = new Client();
            newClient.setName(ClientName);
            newClient.setClientLawnWidth(ClientLawnWidth);
            newClient.setClientLawnLength(ClientLawnLength);
            newClient.setClientPaymentMethod(ClientPaymentMethod);
            Clients.add(newClient);
            more = input.getUserInput(
                    "Would you like to enter another?");
            if (!more.equals("y")) {
                break;
            }
        }
    }
        public void displayClients() {
        Client  client  = null;
        for (int i = 0; i < Clients.size(); i++) {
            client = (Client) Clients.get(i);
            System.out.println(client.display());
            System.out.println();
        }
    }
 }

public class ClientCalculations {
    private String name; 
    private int servicChargeFee;
    public void display() {
    Client client = new Client();
    System.out.print(client.getName());
    }
}

1 个答案:

答案 0 :(得分:0)

您要在哪里初始化即时变量? 您可以在构造方法的帮助下初始化实例变量,然后您的getter将开始工作。 示例:

 class Client {
        private String name;

        // constructor 
        Client(String name) {
            this.name = name;
        }

        // getter
        public String getName() {
            return this.name;
        }
        // setter
        public void setName(String name) {
            this.name = name;   
        }
}

public class Main {
        public static void main(String[] args) {
            // instance of client class
            Client client = new Client("Client Name");
            System.out.println(client.getName());               
        }
}