0

ここで誰かの助けに感謝します。以下は、すべてのセッターとゲッターを含むクラスです。メイン クラスでは 3 つの顧客を作成し、値パラメーターでは 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   
    }


}
4

2 に答える 2

0

実生活と同様に、ある顧客は他の顧客について何も知りません。店内の顧客に、すべての顧客がいくら使ったかを尋ねた場合、彼はこの質問を読んでいる他の人たちと同じように混乱しているように見えるでしょう. すべての顧客を内部的に保持する CustomerManager または Bookkeeper を実装することをお勧めします (たとえば、リスト内)。この CustomerManager には、顧客を追加および削除するメソッド、CustomerManager の顧客リスト内のすべての顧客をループして合計値を返す getBookingValue() メソッド、および必要に応じてその他の快適なメソッドが必要です。例として:

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);
}
于 2013-09-15T10:06:52.023 に答える
0

クラス customer のオブジェクトの配列を作成し、ループ内の値にアクセスできます...

メイン関数: customer cus[]=new 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;
}'

使用したい場所ならどこでも合計値を返します.....

于 2013-09-15T10:39:36.490 に答える