You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在User类中实现searchStore()方法跨类访问CandyStore属性

如何正确实现User类的searchStore()方法并访问CandyStore的cost属性

问题背景

需在User类中实现searchStore()方法,满足以下需求:

  • 用户搜索时,输出其到访地标附近的糖果店铺(例如用户去过lm4和lm1时,需输出Lolli's和Guccini's店铺)
  • 访问对应店铺的糖果cost属性,判断用户money是否足够购买:余额充足则输出可购买提示,否则提示余额不足

遇到的问题:

  • 在方法内创建新CandyStore实例,只能获取新实例的cost,无法访问目标店铺的真实值
  • 尝试添加getter方法时误写为static导致编译错误,但不同店铺cost不同,不能设置为static

修改后的完整代码

Main类

package testing;

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello World");

        User user1 = new User("Mylah", 1, 20f);
        User user2 = new User("Meg", 2, 50f);
        User user3 = new User("Marga", 3, 25f);

        Landmark lm1 = new Landmark("Vista Mall", 1);
        Landmark lm2 = new Landmark("Vista Residences", 2);
        Landmark lm3 = new Landmark("Vista Park", 3);

        Landmark lm4 = new Landmark("Crystal Mall", 4);
        Landmark lm5 = new Landmark("Crystal Residences", 5);
        Landmark lm6 = new Landmark("Crystal Park", 6);

        CandyStore c1 = new CandyStore("Lolli's Store", 1, 15);
        CandyStore c2 = new CandyStore("Mary's Store", 2, 25);
        CandyStore c3 = new CandyStore("Guccini's Store", 3, 10);

        c1.addLandmark(lm4);
        c1.addLandmark(lm6);
        c2.addLandmark(lm2);
        c2.addLandmark(lm3);
        c3.addLandmark(lm1);
        c3.addLandmark(lm5);

        // 将所有店铺列表传递给用户
        user1.setCandyStores(new CandyStore[]{c1, c2, c3});
        
        user1.visits(lm4);
        user1.visits(lm1);

        user1.searchStore();
        user1.viewState();
    }

}

User类(核心修改)

package testing;

public class User {
    String name;
    int StudentId;
    float money;
    Landmark[] lm;
    int lmCounter;
    static int MAX_LM = 3;
    // 新增存储所有糖果店铺的属性
    private CandyStore[] candyStores;

    User(String n, int id, float m) {
        this.name = n;
        this.StudentId = id;
        this.money = m;
        this.lm = new Landmark[User.MAX_LM];
    }

    // 新增设置店铺列表的方法
    public void setCandyStores(CandyStore[] stores) {
        this.candyStores = stores;
    }

    void visits(Landmark l) {
        if(this.lmCounter < MAX_LM) {
            this.lm[this.lmCounter++] = l;
        } else {
            System.out.println("Please go home, you visited too many places already!\n");
        }
    }

    void searchStore() {
        if (candyStores == null || candyStores.length == 0) {
            System.out.println("No candy stores available to search.");
            return;
        }
        if (lmCounter == 0) {
            System.out.println("You haven't visited any landmarks yet.");
            return;
        }

        System.out.println("\n=== Nearby Candy Stores ===");
        // 遍历用户去过的每个地标
        for (int i = 0; i < lmCounter; i++) {
            Landmark visitedLm = lm[i];
            // 遍历所有店铺,检查是否关联当前地标
            for (CandyStore store : candyStores) {
                boolean isNearby = false;
                for (int j = 0; j < store.lmCounter; j++) {
                    // 通过LMid匹配地标,确保是同一个地点
                    if (store.LM[j].LMid == visitedLm.LMid) {
                        isNearby = true;
                        break;
                    }
                }
                if (isNearby) {
                    // 输出店铺信息并判断余额
                    System.out.println("Found store: " + store.name);
                    float candyCost = store.getCost();
                    if (money >= candyCost) {
                        System.out.printf("You have enough money to buy candies here (Cost: %.1f, Your Money: %.1f)\n", candyCost, money);
                    } else {
                        System.out.printf("You don't have enough money to buy candies here (Cost: %.1f, Your Money: %.1f)\n", candyCost, money);
                    }
                    System.out.println("--------------------------");
                }
            }
        }
    }

    void viewState() {
        System.out.println("\n======== VIEW STATE ========");
        System.out.println("Student ID: " + this.StudentId);
        System.out.println("Student Name: " + this.name);
        System.out.println("Student's Money: " + this.money);

        if(this.lmCounter == 0) {
            System.out.println(this.name + " is still in school!\n");
        } else {
            System.out.println(this.name + " visited :");
            for (int i = 0; i < lmCounter; i++) {
                Landmark L = lm[i];
                int j = i+1;
                System.out.println(j + ". " + L.name);
            }

        }
    }
}

CandyStore类(新增getter)

package testing;

public class CandyStore {
    String name;
    int StoreId;
    float cost; // cost of candies inside the store
    int sales;
    Landmark[] LM;
    int lmCounter;
    static int MAX_LM = 3;

    CandyStore(String n, int id, float c) {
        this.name = n;
        this.StoreId = id;
        this.cost = c;
        this.sales = 0;
        this.LM = new Landmark[CandyStore.MAX_LM];
    }

    void addLandmark(Landmark lm) {
        if(this.lmCounter < MAX_LM) {
            this.LM[this.lmCounter++] = lm;
        } else {
            System.out.println("Can't add landmark\n");
        }
    }

    // 新增非静态getter方法,返回当前店铺的cost值
    public float getCost() {
        return this.cost;
    }

    void ViewState() {
        System.out.println("\n======== VIEW STATE ========");
        System.out.println("Store ID: " + this.StoreId);
        System.out.println("Store Name: " + this.name);
        System.out.println("Store Sales: " + this.sales);

        System.out.println("Landmark nearby");
        if(lmCounter == 0) {
            System.out.println("The store doesn't have any recognizable landmark\n");
        } else {
            for(int i = 0; i < lmCounter; i++) {
                Landmark l = LM[i];
                int j = i+1;
                System.out.println(j + ". " + l.name);
            }
        }
    }
}

Landmark类(无需修改)

package testing;

public class Landmark {
    int LMid; // landmark number
    String name;

    Landmark (String n, int id) {
        this.name = n;
        this.LMid = id;
    }

    void viewState() {
        System.out.println("\n======== VIEW STATE ========");
        System.out.println("Landmark Number: " + this.LMid);
        System.out.println("Landmark Name: " + this.name);
    }
}

关键修改说明

  1. 给CandyStore添加非静态getter方法
    新增public float getCost()实例方法,每个店铺调用时会返回自己的cost值,完全不需要设置为static,解决之前的编译错误(错误原因是误将getter写成static)。

  2. 让User类获取所有CandyStore实例
    在User类中添加CandyStore[] candyStores属性和setCandyStores方法,在Main类中创建完所有店铺后,将店铺列表传递给用户,确保User能遍历所有店铺进行匹配。

  3. 实现searchStore核心逻辑

    • 先判断是否有店铺数据、用户是否去过地标
    • 遍历用户去过的每个地标,再遍历所有店铺,通过LMid匹配地标是否关联
    • 找到匹配店铺后,调用store.getCost()获取糖果价格,与用户余额对比输出提示

内容的提问来源于stack exchange,提问作者User617290653251457439

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 12:50:26