Question

id appreciate anyones help here. Below is my class that contains all my setters and getters, in my main class, ive created 3 customers and in the value parameters, i have 3 different numbers. What i need to do is find the total value of all of those values, is there any way that i can create a method (See bookingValue below) that will calculate and add the the total of each customers value parameter? Bare in mind that 3 is not a fixed number, so the method should not be affected should i choose to add in more customers. This is probably really basic but if someone could get me on the right path, that'd be great, cheers

public class Customer 
{

    private int identity;
    private String name;
    private String address;
    private double value;

    public Customer()
    {
        identity = 0;
        name = "";
        address = "";
        value = 0.0;
    }

    public void setIdentity(int identityParam)
    {
        identity = identityParam;
    }

    public int getIdentity()
    {
        return identity;
    }

    public void setName(String nameParam)
    {
        name = nameParam;
    }

    public String getName()
    {
        return name;
    }

    public void setAddress(String addressParam)
    {
        address = addressParam;
    }

    public String getAddress()
    {
        return address;
    }

    public void setValue(double valueParam)
    {
        value = valueParam;
    }

    public double getCarCost()
    {
        return value;
    }

    public void printCustomerDetails()
    {
        System.out.println("The identity of the customer is: " + identity);
        System.out.println("The name of the customer is: " + name);
        System.out.println("The address of the customer is: " + address);
        System.out.println("The value of the customers car is: " + value + "\n");

    }

    public void bookingValue()
    {
        //Ive tried messing around with a for loop here but i cant seem to get it working   
    }


}
Was it helpful?

Solution

you can create an array of object of class customer and access the value in loop...

In the main function: customer cus[]=new customer[num];

where num can be any number as 3 in your case

then get the "value" for each customer.. and then

public double bookingValue(customer []cus, int length)
{
      double total=0.0;
    for(int i=0;i<length;i++)
        total+=a[i].value;
         return total;
}'

return total value wherever you want to use.....

OTHER TIPS

As in real life, one customer doesn't know anything about the other customers. If you would ask a customer in a store how much all customers spent, he will just look as confused as others reading this question. I'd suggest implementing some CustomerManager, or Bookkeeper which hold all the customers internally (in a List for example). This CustomerManager needs to have methods to add and remove customers, the getBookingValue() method which loops over all customers in the CustomerManager's customers List and returns the total value and, if you please, some other comfort methods. As an example:

public interface CustomerManager {
    public void addCustomer(Customer customer);
    public void removeCustomer(Customer customer);
    public List<Customer> getCustomersByDate(long from, long to);
    public double getBookingValue();
    public double getBookingValue(List<Customer> customerList);
    public List<Customer> getByAddress(String address);
    public List<Customer> getByName(String name);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top