Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计算预订的总成本_Java_Loops_Setter_Getter - Fatal编程技术网

Java 计算预订的总成本

Java 计算预订的总成本,java,loops,setter,getter,Java,Loops,Setter,Getter,我感谢这里的任何人的帮助。下面是我的类,它包含我所有的setter和getter,在我的主类中,我创建了3个客户,在value参数中,我有3个不同的数字。我需要做的是找到所有这些值的总值,是否有任何方法可以创建一个方法(参见下面的bookingValue)来计算并添加每个客户值参数的总值?请记住,3不是一个固定的数字,因此,如果我选择添加更多客户,该方法不应受到影响。这可能真的很基本,但如果有人能让我走上正确的道路,那就太好了,干杯 public class Customer { p

我感谢这里的任何人的帮助。下面是我的类,它包含我所有的setter和getter,在我的主类中,我创建了3个客户,在value参数中,我有3个不同的数字。我需要做的是找到所有这些值的总值,是否有任何方法可以创建一个方法(参见下面的bookingValue)来计算并添加每个客户值参数的总值?请记住,3不是一个固定的数字,因此,如果我选择添加更多客户,该方法不应受到影响。这可能真的很基本,但如果有人能让我走上正确的道路,那就太好了,干杯

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   
    }


}

在现实生活中,一个客户对其他客户一无所知。如果你问商店里的一位顾客所有顾客花了多少钱,他看起来和其他阅读这个问题的人一样困惑。 我建议实现一些CustomerManager或簿记员,在内部保存所有客户(例如在列表中)。此CustomerManager需要具有添加和删除客户的方法,即getBookingValue()方法,该方法在CustomerManager的客户列表中的所有客户上循环,并返回总值,如果您愿意,还可以返回其他一些舒适方法。 例如:

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);
}
公共接口CustomerManager{
公共无效添加客户(客户);
公共作废删除客户(客户);
公共列表getCustomersByDate(长从,长到);
公共双getBookingValue();
公共双getBookingValue(列表customerList);
公共列表getByAddress(字符串地址);
公共列表getByName(字符串名称);
}

您可以创建customer类对象的数组并访问循环中的值

在主要功能中: 客户客户[]=新客户[num]

其中num可以是任何数字,在您的情况下为3

然后获取每个客户的“价值”。。然后

public double bookingValue(customer []cus, int length)
{
      double total=0.0;
    for(int i=0;i<length;i++)
        total+=a[i].value;
         return total;
}'
公共双预订值(客户[]客户,整数长度)
{
双倍合计=0.0;

对于(int i=0;i代替
//我试着在这里摆弄一个for循环,但我似乎无法让它工作
把不工作的代码放在这里,然后写下它的工作内容和你想要的内容。我们不会为你写表单0,这没有帮助,而是一个代码服务。你是对的,你需要一个
for
循环;告诉我们你已经尝试了什么。另外,
bookingValue()
应该是
静态的
(它将把
客户
实例的数组或
集合
作为输入,并且不操作特定的
客户
),或者它应该是“拥有”的某个
Booking
对象一群
Customer
s.@chrylis肯定会使用
Booking
对象,甚至是
列表
避免使用静态。哦,我们实际上还没有开始使用数组,直到周三,我用for循环所做的是错误的。在这种情况下,我只想读一点数组的内容,然后再给你回复,谢谢汉克兄弟,我还没有做数组,但在那里读到了一些关于数组的内容,我整个上午都在胡闹,终于让它工作了,谢谢你的帮助