mvc模式 学生管理系统

/**主界面

*/

public class Client {

public static void main(String[] args) {
System.out.println("--------------------");
System.out.println("欢迎进入学生管理系统");
System.out.println("--------------------");
Control.getInstance().addView(new MainView());
Control.getInstance().showView();
}

}

//model

import java.util.HashSet;
import java.util.Iterator;
import com.czh.entity.Student;

public class StudentManager  {

public static HashSetstudentList = new HashSet();

public static void add(Student student) {
studentList.add(student);
System.out.println("插入成功!");
}

public static void del() {

}

public static void modify() {

}

public static void show(int id) {
boolean isHave = false;
Iterator iterator =  StudentManager.studentList.iterator();
while(iterator.hasNext()){
Student student  =  iterator.next();
if(student.getId() == id){
System.out.println(student);
isHave = true;
break;
}
}
if(!isHave){
System.out.println("该学号不存在");
}
}

public static void allShow() {
System.out.println(StudentManager.studentList);
}
}

//定义一个接口

public interface View {
public abstract void showView();
}

import java.util.Scanner;
import com.czh.control.Control;
import com.czh.entity.Commond;
import com.czh.model.StudentManager;
import com.czh.util.Util;

public class ShowStudentView implements View {
@Override
public void showView() {
System.out.println("请输入学号,查询学生,输入all查询所有学生,back返回上一级");
while (true) {
Scanner in = new Scanner(System.in);
String value = in.next();
if("all".equals(value)){//查询所有
Commond commond  = new Commond(0, value);
Control.getInstance().operateCommond(commond);
}else if("back".equals(value)){
Control.getInstance().bakcView();
Control.getInstance().showView();
return;
}else{//单个查询
Commond commond  = new Commond(0, value);
Control.getInstance().operateCommond(commond);
}
}
}
}

import java.util.Scanner;
import com.czh.control.Control;
import com.czh.entity.Commond;

public class MainView implements View {
@Override
public void showView() {
while (true) {
System.out.println("请输入操作:0是查询,1是添加,2是修改,3是删除,back是返回");
 Scanner in = new Scanner(System.in);
 String name = in.next();
 switch(name){
  case "0":
  Control.getInstance().addView(new ShowStudentView());
Control.getInstance().showView();
  return;
  case "1":
  Control.getInstance().addView(new AddStudentView());
Control.getInstance().showView();
  return;
  case "2":
 
  break;
  case "3":
 
  break;
  case "back":
  Control.getInstance().bakcView();
  Control.getInstance().showView();
  return;
default:
System.out.println("您输入的命令错误");
break;
 }
}
}
}

import java.util.Scanner;
import com.czh.control.Control;
import com.czh.entity.Commond;
import com.czh.entity.Student;

public class AddStudentView implements View {
        @Override
public void showView() {
System.out.println("请输入学生姓名,添加学生,back返回上一级");
while (true) {
Scanner in = new Scanner(System.in);
String name = in.next();
if("back".equals(name)){
Control.getInstance().bakcView();
Control.getInstance().showView();
return;
}else{
Student student  = new Student();
student.setName(name);
Commond commond = new Commond(1, student);
Control.getInstance().operateCommond(commond);
}
}
}
}

import java.util.Stack;


import com.czh.entity.Commond;
import com.czh.entity.Student;
import com.czh.model.StudentManager;
import com.czh.util.Util;
import com.czh.view.View;
/**
 * 连接  view 和  studentManger 的桥梁,
 * 接受view 的命令  ,去操作studentManger,将处理的结果,交给view显示
 */
public class Control {

private static  Control  control = new Control();
private Stack viewStack ;
private Control(){
viewStack = new Stack<>();
}
public static  Control getInstance(){
return control;
}
/**
* 添加界面
* @param view
*/
public void addView(View view){
viewStack.push(view);
}

public void showView(){
if(!viewStack.isEmpty()){
viewStack.get(viewStack.size()-1).showView();
}else{
System.out.println("退出学生管理系统!");
}
}

public void bakcView(){
viewStack.pop();
}
//0是查询,1是添加,2是修改,3是删除
public void operateCommond(Commond commond){
switch(commond.getFlag()){
case 0://查询
String strShow = (String) commond.getObj();
if(strShow.equals("all")){
StudentManager.allShow();
}else{
int id = Util.strToInt(strShow);
StudentManager.show(id);
}
break;
case 1://添加
Student student = (Student)commond.getObj();
student.setId(StudentManager.studentList.size());
StudentManager.add(student);
break;
case 2://修改

break;
case 3://删除

break;
default:
System.out.println("命令错误!");
break;
}
}
}

package com.czh.util;
/**
 * 工具类
 * @author czh
 *
 */
public class Util {
public static int strToInt(String num){
int numInt = 0;
try {
numInt = Integer.valueOf(num);
} catch (Exception e) {
numInt = 0;
}
return numInt;
}
}

public class Commond {
//标记是什么命令:0是查询,1是添加,2是修改,3是删除
private int flag ;
private Object obj;
public Commond(int flag , Object obj){
this.flag =  flag;
this.obj = obj;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
}

public class Student {
private String name;
private int id;


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Student) {
Student student = (Student) obj;
if ((this.name.equals(student.getName())) && (this.id == student.getId())) {


}
}
return super.equals(obj);
}
@Override
public int hashCode() {


return name.hashCode() + id;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "学号:" + this.id + " 姓名:" + this.name;
}
}

你可能感兴趣的:(mvc模式 学生管理系统)