/* 1、分析以下需求并完成代码 在现在的互联网系统中大部分的网站都是支持手机号码登录的,当用户输入完手机号码点击获取短信验证码此时就会访问一些短信平台,由短信平台(阿里云短信平台、腾讯云短信平台..),向用户的手机号码发送一个短信验证码。 在素材的msg项目的AliyunSendMsg类和TencentSendMsg的指定位置补全代码,实现短信验证码发送功能,具体实现步骤如下: 1、生成一个4位随机字符串(字符来源于chs数组) 2、发送短信验证码(由于知识储备不足,此处可以通过输出语句进行模拟) 3、创建CheckCode对象,将该对象存储到arrayList集合中 代码运行效果如下图所示: */
时间: 2024-01-24 22:18:35 浏览: 76
AliyunSendMsg.java:
```java
package msg;
import java.util.ArrayList;
import java.util.Random;
public class AliyunSendMsg implements SendMsgInterface {
private ArrayList<CheckCode> arrayList;
public AliyunSendMsg() {
arrayList = new ArrayList<CheckCode>();
}
@Override
public void sendMsg(String phoneNumber) {
// 生成4位随机字符串
char[] chs = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
int index = random.nextInt(chs.length);
sb.append(chs[index]);
}
String checkCode = sb.toString();
// 模拟发送短信验证码
System.out.println("阿里云短信平台已向手机号码 " + phoneNumber + " 发送短信验证码 " + checkCode);
// 创建CheckCode对象,并将该对象存储到arrayList集合中
CheckCode code = new CheckCode(phoneNumber, checkCode);
arrayList.add(code);
}
public ArrayList<CheckCode> getArrayList() {
return arrayList;
}
}
```
TencentSendMsg.java:
```java
package msg;
import java.util.ArrayList;
import java.util.Random;
public class TencentSendMsg implements SendMsgInterface {
private ArrayList<CheckCode> arrayList;
public TencentSendMsg() {
arrayList = new ArrayList<CheckCode>();
}
@Override
public void sendMsg(String phoneNumber) {
// 生成4位随机字符串
char[] chs = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
int index = random.nextInt(chs.length);
sb.append(chs[index]);
}
String checkCode = sb.toString();
// 模拟发送短信验证码
System.out.println("腾讯云短信平台已向手机号码 " + phoneNumber + " 发送短信验证码 " + checkCode);
// 创建CheckCode对象,并将该对象存储到arrayList集合中
CheckCode code = new CheckCode(phoneNumber, checkCode);
arrayList.add(code);
}
public ArrayList<CheckCode> getArrayList() {
return arrayList;
}
}
```
CheckCode.java:
```java
package msg;
public class CheckCode {
private String phoneNumber;
private String checkCode;
public CheckCode(String phoneNumber, String checkCode) {
this.phoneNumber = phoneNumber;
this.checkCode = checkCode;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getCheckCode() {
return checkCode;
}
@Override
public String toString() {
return "手机号码:" + phoneNumber + ",验证码:" + checkCode;
}
}
```
SendMsgInterface.java:
```java
package msg;
public interface SendMsgInterface {
void sendMsg(String phoneNumber);
}
```
Main.java:
```java
package msg;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入手机号码:");
String phoneNumber = scanner.nextLine();
// 实例化AliyunSendMsg对象,发送短信验证码
AliyunSendMsg aliyunSendMsg = new AliyunSendMsg();
aliyunSendMsg.sendMsg(phoneNumber);
// 实例化TencentSendMsg对象,发送短信验证码
TencentSendMsg tencentSendMsg = new TencentSendMsg();
tencentSendMsg.sendMsg(phoneNumber);
// 输出所有发送的短信验证码
ArrayList<CheckCode> arrayList = aliyunSendMsg.getArrayList();
arrayList.addAll(tencentSendMsg.getArrayList());
System.out.println("所有发送的短信验证码如下:");
for (CheckCode code : arrayList) {
System.out.println(code);
}
}
}
```
阅读全文