有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java计算预订的总成本

我感谢这里的任何人的帮助。下面是我的类,包含我所有的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   
    }


}

共 (2) 个答案

  1. # 1 楼答案

    您可以创建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;
    }'
    

    无论在何处使用,都返回总值

  2. # 2 楼答案

    在现实生活中,一个客户对其他客户一无所知。如果你问商店里的一位顾客所有顾客花了多少钱,他看起来和其他阅读这个问题的人一样困惑。 我建议实现一些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);
    }