update:demo12

This commit is contained in:
dichgrem
2026-01-16 16:19:52 +08:00
parent 1bbfe0e546
commit a7334f14dc
6 changed files with 84 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
package com.study.demo12;
public interface CodeGenerator {
String generate();
}

View File

@@ -0,0 +1,8 @@
package com.study.demo12;
public class LoginCodeGenerator implements CodeGenerator {
@Override
public String generate() {
return "LOGIN" + System.currentTimeMillis();
}
}

View File

@@ -0,0 +1,8 @@
package com.study.demo12;
public class PaymentCodeGenerator implements CodeGenerator {
@Override
public String generate() {
return "PAY" + System.currentTimeMillis();
}
}

View File

@@ -0,0 +1,11 @@
package com.study.demo12;
public class TestCodeGenerator {
public static void main(String[] args) {
CodeGenerator loginGenerator = new LoginCodeGenerator();
CodeGenerator paymentGenerator = new PaymentCodeGenerator();
System.out.println("登录验证码: " + loginGenerator.generate());
System.out.println("支付校验码: " + paymentGenerator.generate());
}
}

View File

@@ -0,0 +1,14 @@
package com.study.demo12;
public class TestVerifyCodeLogic {
public static void main(String[] args) {
String timestamp = String.valueOf(System.currentTimeMillis());
String last4Digits = timestamp.substring(timestamp.length() - 4);
String code = "CODE" + last4Digits;
System.out.println("完整时间戳: " + timestamp);
System.out.println("时间戳后4位: " + last4Digits);
System.out.println("生成的验证码: " + code);
System.out.println("验证码格式正确: " + (code.startsWith("CODE") && code.length() == 8));
}
}

View File

@@ -0,0 +1,38 @@
package com.study.demo12;
import java.awt.*;
import java.awt.event.*;
public class VerifyCodeFrame extends Frame {
private Label codeLabel;
public VerifyCodeFrame() {
setTitle("验证码生成窗口");
setSize(300, 150);
setLayout(new FlowLayout());
Button codeButton = new Button("获取验证码");
codeLabel = new Label("点击按钮获取验证码");
codeButton.addActionListener(e -> {
String timestamp = String.valueOf(System.currentTimeMillis());
String last4Digits = timestamp.substring(timestamp.length() - 4);
codeLabel.setText("CODE" + last4Digits);
});
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
add(codeButton);
add(codeLabel);
setVisible(true);
}
public static void main(String[] args) {
new VerifyCodeFrame();
}
}