注:本文使用New Bing(GPT4.0)演示
I want you to act as a Java software developer.
I will provide you with a list of commands and you will implement them.
My first request is "I need help creating a Java application."
public int calculateUsingIfElse(int a, int b, String operator) {
int result = 0;
if (operator.equals("add")) {
result = a + b;
} else if (operator.equals("subtract")) {
result = a - b;
} else if (operator.equals("multiply")) {
result = a * b;
} else if (operator.equals("divide")) {
if (b == 0) {
throw new IllegalArgumentException("Cannot divide by zero");
}
result = a / b;
} else if (operator.equals("modulus")) {
if (b == 0) {
throw new IllegalArgumentException("Cannot divide by zero");
}
result = a % b;
} else if (operator.equals("power")) {
result = (int) Math.pow(a, b);
} else if (operator.equals("square root")) {
if (a < 0) {
throw new IllegalArgumentException("Cannot take square root of negative number");
}
result = (int) Math.sqrt(a);
} else if (operator.equals("absolute value")) {
result = Math.abs(a);
} else if (operator.equals("negate")) {
result = -a;
} else {
throw new IllegalArgumentException("Invalid operator");
}
return result;
}
并且要求「整理成比较优雅的代码结构,比如自动拆分子函数、降低if和循环嵌套,甚至实现部分设计模式。」
// 定义一个接口,表示不同的运算策略
public interface OperationStrategy {
int apply(int a, int b);
}
// 定义一个枚举,表示不同的运算符和对应的策略
public enum Operator {
ADD((a, b) -> a + b),
SUBTRACT((a, b) -> a - b),
MULTIPLY((a, b) -> a * b),
DIVIDE((a, b) -> {
if (b == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
return a / b;
}),
MODULUS((a, b) -> {
if (b == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
return a % b;
}),
POWER((a, b) -> (int) Math.pow(a, b)),
SQUARE_ROOT((a, b) -> {
if (a < 0) {
throw new ArithmeticException("Cannot take square root of negative number");
}
return (int) Math.sqrt(a);
}),
ABSOLUTE_VALUE(Math::abs),
NEGATE(a -> -a);
private final OperationStrategy strategy;
Operator(OperationStrategy strategy) {
this.strategy = strategy;
}
public int apply(int a, int b) {
return strategy.apply(a, b);
}
}
// 定义一个方法,根据运算符和操作数来计算结果
public int calculateUsingStrategy(int a, int b, String operatorName) {
// 根据运算符名称找到对应的枚举常量,如果没有找到则抛出异常
Operator operator = Operator.valueOf(operatorName.toUpperCase());
if (operator == null) {
throw new IllegalArgumentException("Invalid operator");
}
// 调用枚举常量的apply方法来执行相应的策略
return operator.apply(a,b);
}
import org.junit.Test;
import static org.junit.Assert.*;
// 定义一个测试类,用@Test注解标记要执行的测试方法
public class CalculatorTest {
// 定义一个测试方法,用assertEquals方法来验证计算结果是否正确
@Test
public void testAdd() {
int result = calculateUsingStrategy(3, 5, "add");
assertEquals(8, result);
}
// 定义其他的测试方法,分别测试不同的运算符和操作数
@Test
public void testSubtract() {
int result = calculateUsingStrategy(10, 4, "subtract");
assertEquals(6, result);
}
@Test
public void testMultiply() {
int result = calculateUsingStrategy(7, 9, "multiply");
assertEquals(63, result);
}
@Test
public void testDivide() {
int result = calculateUsingStrategy(12, 3, "divide");
assertEquals(4, result);
}
// 定义一个测试方法,用assertThrows方法来验证抛出了预期的异常
@Test
public void testDivideByZero() {
assertThrows(ArithmeticException.class,
() -> calculateUsingStrategy(10,0,"divide"));
}
// 定义其他的测试方法,分别测试不同的异常情况
@Test
public void testModulusByZero() {
assertThrows(ArithmeticException.class,
() -> calculateUsingStrategy(10,0,"modulus"));
}
@Test
public void testSquareRootOfNegativeNumber() {
assertThrows(ArithmeticException.class,
() -> calculateUsingStrategy(-4,-2,"square root"));
}
// 定义一个测试方法,用assertThrows方法来验证抛出了预期的异常
@Test
public void testInvalidOperator() {
assertThrows(IllegalArgumentException.class,
() -> calculateUsingStrategy(10,-2,"foo"));
}
}
线下活动推荐:
时间:4月9日(周日)14:00-18:00
主题:得物技术沙龙-安全专场
地点:上海市杨浦区黄兴路221号互联宝地C2栋5楼 培训教室
活动亮点:本次沙龙聚焦于行业安全前沿最佳实践,将通过得物安全白皮书分享、企企业安全体系建设经验、零信任安全介绍、数据安全治理手段等多个维度,来讲述安全管理在当前企业中遇到的挑战和解决方案。
报名方式:点击阅读原文或下图
*文/ 展昭
关注得物技术,每周一三五晚18:30更新技术干货