package com.bwf.aClient;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner;
import com.bwf.bean.Request;
import com.bwf.bean.Response;
public class ClientFunction {
private Scanner sc = null;
private ObjectOutputStream oos;
public void setOos(ObjectOutputStream oos) {
this.oos = oos;
}
public ClientFunction(Scanner sc){
this.sc = sc;
}
/**登录身份选择界面*/
public void startMenu() throws IOException{
System.out.println(" 1-管理员登录 ,2-学生登录,0-退出");
int chose = sc.nextInt();
switch (chose) {
case 0:
cancle();
break;
case 1:
login();
break;
case 2:
sLogin();
break;
default:
startMenu();
break;
}
}
/**退出*/
public void cancle(){
Request request = new Request(Request.TYPE_CANCLE);
try {
oos.writeObject(request);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**注销*/
public void exit(){
Request request = new Request(Request.TYPE_EXIT);
try {
oos.writeObject(request);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void exitResult(Response response) throws IOException{
System.out.println(response.getContent());
if(response.getContent().equals("注销成功!")){
startMenu();
}
}
/**管理员登录界面*/
public void login() throws IOException{
System.out.println("请输入管理员账号:");
String aID = sc.next();
System.out.println("请输入管理员密码:");
String aPwd = sc.next();
// 包装成对象
Request request = new Request(Request.TYPE_ALOGIN);
request.setaId(aID);
request.setaPassword(aPwd);
// 发送给服务器
oos.writeObject(request);
oos.flush();
}
/** 打印管理员登录的结果*/
public void loginResult(Response response) throws IOException{
System.out.println(response.getContent());
if(response.getContent().equals("登录成功!")){
adminMenu();
}else{
startMenu();
}
}
/**学生登录界面*/
public void sLogin() throws IOException{
System.out.println("请输入学生账号:");
String sId = sc.next();
System.out.println("请输入学生密码:");
String sPwd = sc.next();
// 包装成对象
Request request = new Request(Request.TYPE_SLOGIN);
request.setsId(sId);
request.setsPassword(sPwd);
// 发送给服务器
oos.writeObject(request);
oos.flush();
}
/** 打印学生登录的结果*/
public void sLoginResult(Response response) throws IOException{
System.out.println(response.getContent());
if(response.getContent().equals("登录成功!")){
studentMenu();
}else{
startMenu();
}
}
/**管理员操作界面*/
public void adminMenu() throws IOException{
System.out.println("1-添加学生,2-查询学生,3-修改密码,4-修改班级,5-修改成绩,0-注销");
int chose = sc.nextInt();
switch (chose) {
case 0:
exit();
break;
case 1:
add();
break;
case 2:
look();
break;
case 3:
modifyPassword();
break;
case 4:
modifyClass();
break;
case 5:
modifyScore();
break;
default:
adminMenu();
break;
}
}
/**学生操作界面*/
public void studentMenu() throws IOException{
System.out.println("1-查询信息,2-修改密码,0-注销");
int chose = sc.nextInt();
switch (chose) {
case 0:
exit();
break;
case 1:
studentLook();
break;
case 2:
rePassword();
break;
default:
studentMenu();
break;
}
}
/**管理员请求添加*/
private void add() throws IOException{
System.out.println("请输入学号");
String sId = sc.next();
System.out.println("请输入密码");
String sPassword = sc.next();
System.out.println("请输入班级");
String sClass = sc.next();
System.out.println("请输入成绩");
Double sScore = sc.nextDouble();
//包装
Request request = new Request(Request.TYPE_ADD);
request.setsId(sId);
request.setsPassword(sPassword);
request.setsClass(sClass);
request.setsScore(sScore);
//发送给服务器
oos.writeObject(request);
oos.flush();
}
/** 打印添加的结果*/
public void addResult(Response response) throws IOException{
System.out.println(response.getContent());
adminMenu();
}
/**管理员查看学生信息*/
public void look(){
Request request = new Request(Request.TYPE_LOOK);
try {
oos.writeObject(request);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**打印查看结果 */
public void lookResult(Response response) throws IOException{
System.out.println(response.getContent());
adminMenu();
}
/**请求修改密码*/
private void modifyPassword() {
System.out.println("请输入要修改的学号");
String sId = sc.next();
System.out.println("请输入修改密码");
String sPassword = sc.next();
/**包装*/
Request request = new Request(Request.TYPE_MODIFYPS);
request.setsId(sId);
request.setsPassword(sPassword);
/**发送给服务器*/
try {
oos.writeObject(request);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/** 打印修改的结果*/
public void modifyPasswordResult(Response response) throws IOException{
System.out.println(response.getContent());
adminMenu();
}
/**请求修改班级*/
private void modifyClass() throws IOException{
System.out.println("请输入要修改的学号");
String sId = sc.next();
System.out.println("请输入修改班级");
String sClass = sc.next();
/**包装*/
Request request = new Request(Request.TYPE_MODIFYCLASS);
request.setsId(sId);
request.setsClass(sClass);
//发送给服务器
oos.writeObject(request);
oos.flush();
}
/** 打印添加的结果*/
public void modifyClassResult(Response response) throws IOException{
System.out.println(response.getContent());
adminMenu();
}
/**请求修改成绩*/
private void modifyScore() throws IOException{
System.out.println("请输入要修改的学号");
String sId = sc.next();
System.out.println("请输入修改成绩");
Double sScore = sc.nextDouble();
//包装请求
Request request = new Request(Request.TYPE_MODIFYSCORE);
request.setsId(sId);
request.setsScore(sScore);
//发送给服务器
oos.writeObject(request);
oos.flush();
}
/** 打印添加的结果*/
public void modifyScoreResult(Response response) throws IOException{
System.out.println(response.getContent());
adminMenu();
}
/**学生查看学生信息*/
public void studentLook(){
Request request = new Request(Request.TYPE_SLOOK);
try {
//发送给服务器
oos.writeObject(request);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**打印学生查看结果 */
public void studentLookResult(Response response) throws IOException{
System.out.println(response.getContent());
studentMenu();
}
/**学生请求修改密码*/
private void rePassword() {
System.out.println("请输入原始密码");
String sId = sc.next();
System.out.println("请输入修改密码");
String sPassword = sc.next();
/**包装*/
Request request = new Request(Request.TYPE_REPS);
request.setsId(sId);
request.setsPassword(sPassword);
/**发送给服务器*/
try {
oos.writeObject(request);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/** 打印学生修改的结果*/
public void rePasswordResult(Response response) throws IOException{
System.out.println(response.getContent());
studentMenu();
}
}