1.策略模式 实现一个场景,输入两个数字和一个操作类型(加-add,减-subtract,乘-multi,除-devise),输出计算结果,要求不能使用if-else 或switch等条件控制语句 ,请使用java 实现 并且给出思路
// 1. 定义策略接口
interface CalculatorStrategy {
double calculate(double num1, double num2);
}
// 2. 实现具体策略类:加法策略
class AdditionStrategy implements CalculatorStrategy {
@Override
public double calculate(double num1, double num2) {
return num1 + num2;
}
}
// 减法策略
class SubtractionStrategy implements CalculatorStrategy {
@Override
public double calculate(double num1, double num2) {
return num1 - num2;
}
}
// 乘法策略
class MultiplicationStrategy implements CalculatorStrategy {
@Override
public double calculate(double num1, double num2) {
return num1 * num2;
}
}
// 除法策略
class DivisionStrategy implements CalculatorStrategy {
@Override
public double calculate(double num1, double num2) {
if (num2 == 0) {
throw new IllegalArgumentException("除数不能为0");
}
return num1 / num2;
}
}
// 3. 创建上下文类
class CalculatorContext {
private CalculatorStrategy strategy;
public void setStrategy(CalculatorStrategy strategy) {
this.strategy = strategy;
}
public double executeStrategy(double num1, double num2) {
return strategy.calculate(num1, num2);
}
}
public class Main {
public static void main(String[] args) {
// 示例操作数
double num1 = 10;
double num2 = 5;
// 示例操作类型
String operation = "multi";
CalculatorContext context = new CalculatorContext();
// 利用Map存储操作类型与对应策略对象的映射关系
java.util.Map<String, CalculatorStrategy> strategyMap = new java.util.HashMap<>();
strategyMap.put("add", new AdditionStrategy());
strategyMap.put("subtract", new SubtractionStrategy());
strategyMap.put("multi", new MultiplicationStrategy());
strategyMap.put("devise", new DivisionStrategy());
CalculatorStrategy selectedStrategy = strategyMap.get(operation);
if (selectedStrategy!= null) {
context.setStrategy(selectedStrategy);
double result = context.executeStrategy(num1, num2);
System.out.println("计算结果: " + result);
} else {
System.out.println("不支持的操作类型");
}
}
}
2. 测题目一: A系统提供的login服务,有外部“很多”系统在调用该服务实现其登录逻辑(比如B、C、D等等系统)
现在需要你实现以下逻辑:
1,在A系统上统计各个系统login服务的调用次数 2,把统计出来的每个系统的调用次数做一次升序排序 提示:*/ //登录伪代码如下,各个系统调用本方法登录 // systemName系统名称、userId 用户ID //在本方法中把每个系统调用次数进行统计并用集合存储 public void login(String systemName,String userId) {
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class LoginServiceStatistics {
// 使用HashMap来记录系统名称和对应的调用次数
private static Map<String, Integer> callCountMap = new HashMap<>();
public static void login(String systemName, String userId) {
// 获取当前系统名称对应的调用次数,如果不存在则默认为0,然后加1
callCountMap.put(systemName, callCountMap.getOrDefault(systemName, 0) + 1);
}
public static List<Map.Entry<String, Integer>> getSortedCallCounts() {
// 将HashMap的Entry集合转换为ArrayList,便于排序
List<Map.Entry<String, Integer>> entryList = new ArrayList<>(callCountMap.entrySet());
// 使用自定义比较器按照调用次数进行升序排序
entryList.sort((e1, e2) -> e1.getValue().compareTo(e2.getValue()));
return entryList;
}
public static void main(String[] args) {
// 模拟不同系统调用登录服务
login("B", "user1");
login("C", "user2");
login("B", "user3");
login("D", "user4");
login("C", "user5");
List<Map.Entry<String, Integer>> sortedList = getSortedCallCounts();
for (Map.Entry<String, Integer> entry : sortedList) {
System.out.println(entry.getKey() + "系统调用次数: " + entry.getValue());
}
}
}
3.使用java 三个线程循环打印ABC10次
import java.util.concurrent.Semaphore;
public class PrintABCWithSemaphore {
// 定义三个信号量,分别控制三个线程的执行顺序
private static Semaphore semaphoreA = new Semaphore(1);
private static Semaphore semaphoreB = new Semaphore(0);
private static Semaphore semaphoreC = new Semaphore(0);
static class ThreadA implements Runnable {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
semaphoreA.acquire();
System.out.print("A");
semaphoreB.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class ThreadB implements Runnable {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
semaphoreB.acquire();
System.out.print("B");
semaphoreC.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class ThreadC implements Runnable {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
semaphoreC.acquire();
System.out.println("C");
semaphoreA.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Thread(new ThreadA()).start();
new Thread(new ThreadB()).start();
new Thread(new ThreadC()).start();
}
}