【问题标题】:Accessing a classes values from inside another class and mutating them从另一个类内部访问一个类的值并改变它们
【发布时间】:2020-07-09 07:35:55
【问题描述】:

我的public Customer rob 方法有问题。我需要这种方法能够抓住任何客户c 并将他们的钱设置为 0 并为此返回整个客户,即他们的姓名、年龄和金钱。这是代码,感谢任何帮助。

此外,代码的快速摘要只是一个名为 walmart 的商店,其中包含 26 名客户,包括他们的姓名、年龄(int)和金钱(float)

客户类别:

class Customer {
    String name;
    int age;
    float money;

    public Customer(String n, int a, float m) {
        name = n;
        age = a;
        money = m;
    }

    public int getAge() {
        return age;
    }
    public float getMoney() {
        return money;
    }
    public String toString() {
        return "Customer " + name + ": a " + age + " year old with $" + money;
    }
}

包含rob方法的Store类:

public class Store {
    public static final int MAX_CUSTOMERS = 500;
    String name;
    Customer[] customers;
    int customerCount;
    int main;
    public Store(String n) {
        name = n;
        customers = new Customer[MAX_CUSTOMERS];
        customerCount = 0;
    }
    public void addCustomer(Customer c) {
        if (customerCount < MAX_CUSTOMERS)
            customers[customerCount++] = c;
    }
    public void listCustomers() {
        for (int i=0; i<customerCount; i++)
            System.out.println(customers[i]);
    }

    public int averageCustomerAge() {
        //should return int of average age
        int i;
        for(i=0; i<customerCount;i++) {
            main += customers[i].getAge();
        }
        main = main/customerCount;
        return main;
    }
    public Customer richestCustomer() {
        int i;
        float total = 0;
        Customer mainCustomer = null;
        for(i=0; i<customerCount;i++) {
            if (customers[i].getMoney() > total) {
                mainCustomer = customers[i];
                total = customers[i].getMoney();
            }
        }
        return mainCustomer;
    }
    public Customer rob(Customer c) {
        Customer mainCustomer = null;
        mainCustomer = customers[c];

    }
}

测试类:

public class StoreTestProgram {
    public static void main(String args[]) {
        Customer[] result;
        Store walmart;
        walmart = new Store("Walmart off Innes");
        walmart.addCustomer(new Customer("Amie", 14, 100));
        walmart.addCustomer(new Customer("Brad", 15, 0));
        walmart.addCustomer(new Customer("Cory", 10, 100));
        walmart.addCustomer(new Customer("Dave", 5, 48));
        walmart.addCustomer(new Customer("Earl", 21, 500));
        walmart.addCustomer(new Customer("Flem", 18, 1));
        walmart.addCustomer(new Customer("Gary", 8, 20));
        walmart.addCustomer(new Customer("Hugh", 65, 30));
        walmart.addCustomer(new Customer("Iggy", 43, 74));
        walmart.addCustomer(new Customer("Joan", 55, 32));
        walmart.addCustomer(new Customer("Kyle", 16, 88));
        walmart.addCustomer(new Customer("Smaug", 12, 1000));
        walmart.addCustomer(new Customer("Mary", 17, 6));
        walmart.addCustomer(new Customer("Nick", 13, 2));
        walmart.addCustomer(new Customer("Omar", 18, 24));
        walmart.addCustomer(new Customer("Patt", 24, 45));
        walmart.addCustomer(new Customer("Quin", 42, 355));
        walmart.addCustomer(new Customer("Ruth", 45, 119));
        walmart.addCustomer(new Customer("Snow", 74, 20));
        walmart.addCustomer(new Customer("Tamy", 88, 25));
        walmart.addCustomer(new Customer("Ulsa", 2, 75));
        walmart.addCustomer(new Customer("Vern", 9, 90));
        walmart.addCustomer(new Customer("Will", 11, 220));
        walmart.addCustomer(new Customer("Xeon", 17, 453));
        walmart.addCustomer(new Customer("Ying", 19, 76));
        walmart.addCustomer(new Customer("Zack", 22, 35));
        //System.out.println("Here are the customers:\n");
        //walmart.listCustomers();

        System.out.println("\nAverage age of customers: " + walmart.averageCustomerAge());

        // Find richest customer
        System.out.println("\nRichest customer is: " + walmart.richestCustomer());

        //Make Smaug’s walmart experience quite miserable
        walmart.rob(walmart.customers[11]);
        System.out.println("\n Smaug is now: " + walmart.customers[11]);
    }
}

【问题讨论】:

    标签: java


    【解决方案1】:

    您可以更改 rob 以接受客户的int indexString name

    public Customer rob(int customerIndex) {
      Customer customer = customers[customerIndex];
      customer.setMoney(0f);
      return customer;
    }
    

    通过名称查找可能如下所示:

    Arrays.stream(customers)
        .filter(customer -> customer.name.equals(customerName))
        .findFirst();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 2023-04-07
      • 2020-11-24
      • 1970-01-01
      相关资源
      最近更新 更多