0

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   
    }


}
user2757842
  • 651
  • 1
  • 11
  • 24
  • 1
    Instead of `//Ive tried messing around with a for loop here but i cant seem to get it working` put the code that does not work here, and write wht it does and what you want. We won't write it for you form 0, that is not helping, but a code service. – ppeterka Sep 15 '13 at 09:48
  • You're right that you need a `for` loop; show us what you've tried. Also, `bookingValue()` should be `static` (it is going to take an array or `Collection` of `Customer` instances as an input and doesn't operate on a specific `Customer`), or it should be on some `Booking` object that "owns" a group of `Customer`s. – chrylis -cautiouslyoptimistic- Sep 15 '13 at 09:49
  • @chrylis Definetly go with the `Booking` object or even a `List` avoid using statics. – Kevin Bowersox Sep 15 '13 at 09:50
  • 1
    Oh, well we haven't actually started arrays yet, not until wednesday, what i was doing with the for loop was wrong in that case, im just going to read up a bit on arrays and get back to you, thanks – user2757842 Sep 15 '13 at 09:55

2 Answers2

0

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);
}
Philipp Seeger
  • 140
  • 2
  • 6
0

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

  • Thanks man, i haven't done arrays yet but read a bit about them there, ive been messing around with this all morning and finally have it working, thanks for the help – user2757842 Sep 15 '13 at 11:00