temp001

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

// Define an interface for the tip strategies
interface TipStrategy {
    String getTip(String tipCode);
}

// Define concrete tip strategies for different languages
class EnglishTipStrategy implements TipStrategy {
    private static final Map<String, String> tipMap = new HashMap<>();
    static {
        tipMap.put("ENG1", "Always remember to write clean and readable code.");
        tipMap.put("ENG2", "Use meaningful variable and method names to improve code clarity.");
        tipMap.put("ENG3", "Document your code properly to make it easier for others to understand.");
        tipMap.put("ENG4", "Favor composition over inheritance to achieve better code maintainability.");
        tipMap.put("ENG5", "Don't repeat yourself (DRY principle) - refactor duplicated code into reusable methods or classes.");
    }

    @Override
    public String getTip(String tipCode) {
        return tipMap.getOrDefault(tipCode, "Tip not found for code: " + tipCode);
    }
}

class SpanishTipStrategy implements TipStrategy {
    private static final Map<String, String> tipMap = new HashMap<>();
    static {
        tipMap.put("ESP1", "Recuerda siempre escribir código limpio y legible.");
        tipMap.put("ESP2", "Usa nombres de variables y métodos significativos para mejorar la claridad del código.");
        tipMap.put("ESP3", "Documenta tu código adecuadamente para facilitar su comprensión por parte de los demás.");
        tipMap.put("ESP4", "Prefiere la composición sobre la herencia para lograr una mejor mantenibilidad del código.");
        tipMap.put("ESP5", "No te repitas (principio DRY) - refactoriza el código duplicado en métodos o clases reutilizables.");
    }

    @Override
    public String getTip(String tipCode) {
        return tipMap.getOrDefault(tipCode, "Tip not found for code: " + tipCode);
    }
}

// Tip context class
class TipContext {
    private TipStrategy tipStrategy;

    public TipContext(TipStrategy tipStrategy) {
        this.tipStrategy = tipStrategy;
    }

    public void setTipStrategy(TipStrategy tipStrategy) {
        this.tipStrategy = tipStrategy;
    }

    public String getTip(String tipCode) {
        return tipStrategy.getTip(tipCode);
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        // Create a map to store tip strategies for different languages
        Map<String, TipStrategy> tipStrategies = new HashMap<>();
        tipStrategies.put("English", new EnglishTipStrategy());
        tipStrategies.put("Spanish", new SpanishTipStrategy());

        // Example: Selecting a language
        String selectedLanguage = "English"; // Change this to select different languages

        // Example: Selecting a tip code
        String selectedTipCode = "ENG3"; // Change this to select different tip codes

        // Get the tip strategy based on the selected language
        TipStrategy tipStrategy = tipStrategies.get(selectedLanguage);

        // Create a tip context with the selected tip strategy
        TipContext tipContext = new TipContext(tipStrategy);

        // Get and display the tip
        System.out.println("Tip for " + selectedLanguage + " with code " + selectedTipCode + ": " + tipContext.getTip(selectedTipCode));
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值