参数校验

This commit is contained in:
2025-05-03 10:20:50 +08:00
parent d84021f3db
commit bce338ee19
12 changed files with 688 additions and 7 deletions

View File

@ -0,0 +1,138 @@
package com.greenorange.promotion;
import java.util.Stack;
// Command 接口
interface Command {
void execute(); // 执行命令
void undo(); // 撤销命令
}
// Receiver 类,厨师执行具体的菜品制作命令
class Chef {
public void makeBurger() {
System.out.println("制作汉堡...");
}
public void makeFriedRice() {
System.out.println("制作炒饭...");
}
public void makeFriedChicken() {
System.out.println("制作炸鸡...");
}
}
// 具体命令类
class MakeBurgerCommand implements Command {
private Chef chef;
public MakeBurgerCommand(Chef chef) {
this.chef = chef;
}
@Override
public void execute() {
chef.makeBurger(); // 执行命令,制作汉堡
}
@Override
public void undo() {
System.out.println("撤销汉堡制作命令...");
}
}
class MakeFriedRiceCommand implements Command {
private Chef chef;
public MakeFriedRiceCommand(Chef chef) {
this.chef = chef;
}
@Override
public void execute() {
chef.makeFriedRice(); // 执行命令,制作炒饭
}
@Override
public void undo() {
System.out.println("撤销炒饭制作命令...");
}
}
class MakeFriedChickenCommand implements Command {
private Chef chef;
public MakeFriedChickenCommand(Chef chef) {
this.chef = chef;
}
@Override
public void execute() {
chef.makeFriedChicken(); // 执行命令,制作炸鸡
}
@Override
public void undo() {
System.out.println("撤销炸鸡制作命令...");
}
}
// 服务员类,接收并执行命令
class Waiter {
private Stack<Command> commandHistory = new Stack<>(); // 用栈来保存历史命令,方便撤销
public void takeOrder(Command command) {
command.execute(); // 执行命令
commandHistory.push(command); // 将命令存入历史记录
}
public void undoLastOrder() {
if (!commandHistory.isEmpty()) {
Command lastCommand = commandHistory.pop(); // 获取并移除栈顶命令
lastCommand.undo(); // 执行撤销操作
} else {
System.out.println("没有可撤销的命令!");
}
}
// 宏命令:一次性下多个菜品
public void placeMultipleOrders(Command[] commands) {
for (Command command : commands) {
takeOrder(command);
}
}
}
// 客户端模拟点餐过程
public class Client {
public static void main(String[] args) {
// 创建厨师
Chef chef = new Chef();
// 创建具体命令
Command makeBurger = new MakeBurgerCommand(chef);
Command makeFriedRice = new MakeFriedRiceCommand(chef);
Command makeFriedChicken = new MakeFriedChickenCommand(chef);
// 创建服务员
Waiter waiter = new Waiter();
// 用户下单
waiter.takeOrder(makeBurger);
waiter.takeOrder(makeFriedRice);
// 用户撤销上一条订单(炒饭)
waiter.undoLastOrder();
// 用户一次性下多个菜品(宏命令)
Command[] orders = {makeBurger, makeFriedChicken};
waiter.placeMultipleOrders(orders);
}
}

View File

@ -0,0 +1,48 @@
package com.greenorange.promotion;
import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.Test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class IpDemo {
public static void main(String[] args) {
getIpLocation("123.167.57.119");
}
public static String getIpLocation(String ip) {
try {
// 发送请求到ip-api服务
String url = "http://ip-api.com/json/" + ip + "?lang=zh-CN"; // 获取中文结果
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 解析JSON返回结果
JSONObject jsonResponse = JSONObject.parseObject(response.toString());
String country = jsonResponse.getString("country"); // 国家
String region = jsonResponse.getString("regionName"); // 省
String city = jsonResponse.getString("city"); // 城市
System.out.println(jsonResponse);
System.out.println(country + "-" + region + "" + "-" + city);
return country + "-" + region + "" + "-" + city; // 返回位置
} catch (Exception e) {
e.printStackTrace();
}
return "未知";
}
}