package demo01;
import java.io.IOException;
import java.util.Scanner;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class Oklogin {
public static void main(String[] args) {
boolean flag=false;
Scanner sc=new Scanner(System.in);
System.out.println(" 欢迎进入学生管理系统的登录页面!");
System.out.println(" 请输入用户名:");
String name=sc.nextLine();
System.out.println(" 请输入密码:");
String pwd=sc.nextLine();
String type="login";
OkHttpClient client=new OkHttpClient();
RequestBody body=new FormBody.Builder().add("name",name).add("pwd",pwd).add("type",type).build();
Request request=new Request.Builder().url("http://localhost:8080/week/login.do").post(body).build();
try {
Response response=client.newCall(request).execute();
if(response.isSuccessful()){
System.out.println(response.body().string());
flag=true;
}
} catch (Exception e) {
e.printStackTrace();
}
if(flag){
load();
}
}
private static void load() {
while(true){
System.out.println(" 欢迎来到学生账号信息管理系统:");
System.out.println("--------------------------");
System.out.println("1: 增加用户信息");
System.out.println("2: 删除用户信息");
System.out.println("3: 修改用户信息");
System.out.println("4: 查看用户信息");
System.out.println("5: 退出系统!");
System.out.println("----------------------------");
System.out.println(" 请选择:");
Scanner sc=new Scanner(System.in);
int choose=0;
while(true){
try {
choose = sc.nextInt();
break;
} catch (Exception e) {
sc.next();// 清除上次输入的信息
System.out.println(" 请输入合法信息:");
}
}
switch (choose) {
case 1:
addUser();
break;
case 2:
deleteUser();
break;
case 3:
modifyUser();
break;
case 4:
findAllUser();
break;
case 5:
System.out.println(" 退出成功!");
return;
}
System.out.println(" 按任意键回到首页:");
String name=sc.next();
}//while
}
private static void deleteUser() {
Scanner sc=new Scanner(System.in);
System.out.println(" 请输入您删除的用户名:");
String name=sc.nextLine();
String type="D";
OkHttpClient client=new OkHttpClient();
RequestBody body=new FormBody.Builder().add("name",name).add("type",type).build();
Request request=new Request.Builder().url("http://localhost:8080/week/login.do").post(body).build();
try {
Response response=client.newCall(request).execute();
if(response.isSuccessful()){
System.out.println(response.body().string());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void modifyUser() {
Scanner sc=new Scanner(System.in);
System.out.println(" 请输入您修改的用户名:");
String name=sc.nextLine();
System.out.println(" 请输入您修改的密码:");
String pwd=sc.nextLine();
String type="C";
OkHttpClient client=new OkHttpClient();
RequestBody body=new FormBody.Builder().add("name",name).add("pwd",pwd).add("type",type).build();
Request request=new Request.Builder().url("http://localhost:8080/week/login.do").post(body).build();
try {
Response response=client.newCall(request).execute();
if(response.isSuccessful()){
System.out.println(response.body().string());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void addUser() {
Scanner sc=new Scanner(System.in);
System.out.println(" 请输入添加的用户名:");
String name=sc.nextLine();
System.out.println(" 请输入添加的密码:");
String pwd=sc.nextLine();
String type="B";
OkHttpClient client=new OkHttpClient();
RequestBody body=new FormBody.Builder().add("name",name).add("pwd",pwd).add("type",type).build();
Request request=new Request.Builder().url("http://localhost:8080/week/login.do").post(body).build();
try {
Response response=client.newCall(request).execute();
if(response.isSuccessful()){
System.out.println(response.body().string());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void findAllUser() {
String type="A";
OkHttpClient client=new OkHttpClient();
RequestBody body=new FormBody.Builder().add("type",type).build();
Request request=new Request.Builder().url("http://localhost:8080/week/login.do").post(body).build();
try {
Response response=client.newCall(request).execute();
if(response.isSuccessful()){
System.out.println(response.body().string());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
|