这是我初学java以来写的第一个大一点的小程序,在这里就跟大家一起来分享一下。

首先给大家说一下做这个小程序的需求:  

    每个班有一个老师,每个班可以有多个学生。

    学生的增删改查

    老师的增删改查

    学生可以转班

思路:1、我们实现这一个功能因为还没学习数据库话,首先应该想到要用到数组的方式来实现;

          2、构思:结构要安排合理,什么部分该实现什么功能,以及应该先实现什么再做什么,一步一步的写;

          3、先不要管代码量有多少,以实现功能为主要目的;

          4、实现功能后,再考虑代码的优化问题。

下面就是代码实现的部分:

第一步:我们要有一个主程序来运行,代码如下

import java.util.*;
import java.util.List;
import java.util.Scanner;

import org.lanqiao.service.ClassService;
import org.lanqiao.service.StudentService;
import org.lanqiao.service.TeacherService;

public class Main { 
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		ClassService cs = new ClassService();
		StudentService ss = new StudentService();
		TeacherService ts = new TeacherService();
		a:while(true) {
			System.out.println("=======班级管理======");
			System.out.println("1、班级信息管理");
			System.out.println("2、老师信息管理");
			System.out.println("3、学生信息管理");
			System.out.println("4、退出程序");
			int input = sc.nextInt();
			switch (input) {
			case 1:
				cs.checkAll(sc);
				break;
			case 2:
				ts.teacherAll(sc);
				break;
			case 3:
				ss.studentAll(sc);
				break;
			case 4:
				System.out.println("欢迎下次使用!");
				System.exit(0);
				break a;
			default:
				System.out.println("没有此功能!!!");
				break;
			}
		}
	}
}

第二步:服务类

这里要说的就是我把实现功能都在写在Service类里面:

1、班级的Service类如下:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.lanqiao.school.Clazz;
import org.lanqiao.school.Student;
import org.lanqiao.school.Teacher;

public class ClassService {
	// 创建班级容器
	static List<Clazz> clazz = new ArrayList<>();

	static {
		Teacher	t1 = new Teacher(1, "AA", "AA@lanqiao.org", "英语");
		Teacher t2 = new Teacher(2, "BB", "BB@lanqiao.org", "语文");

		List<Student> student1 = new ArrayList<>();
		student1.add(new Student(1, "aa", "aa@lanqiao.org", 85));
		student1.add(new Student(2, "bb", "bb@lanqiao.org", 90));
		
		List<Student> student2 = new ArrayList<>();
		student2.add(new Student(3, "cc", "cc@lanqiao.org", 95));
		student2.add(new Student(4, "dd", "dd@lanqiao.org", 100));
		student2.add(new Student(5, "ee", "ee@lanqiao.org", 75));
		
		clazz.add(new Clazz(t1, student1, 1));
		clazz.add(new Clazz(t2, student2, 2));
		
	}
	public static void checkAll(Scanner sc) {
		d: while (true) {
			System.out.println("=============");
			System.out.println("1、查看所有班级信息");
			System.out.println("2、查看指定班级信息");
			System.out.println("3、增加班级");
			System.out.println("4、删除班级");
			System.out.println("5、返回上级菜单");
			int input = sc.nextInt();
			switch (input) {
			case 1:
				check(sc);
				break;
			case 2:
				check1(sc);
				break;
			case 3:
				add(sc);
				break;
			case 4:
				delete(sc);
				break;
			case 5:
				break d;
			default:
				System.out.println("没有该功能!");
				break;
			}
		}

	}

	/**
	 * 查看所有班级信息
	 */
	public static void check(Scanner sc) {
		int sum=0;
		System.out.println("本校共有" + clazz.size() + "个班,信息如下");
		for (Clazz a : clazz) {
			System.out.println(a.getId() + "班有" + a.getStulist().size() + "个学生");
			for (int i = 0; i < a.getStulist().size(); i++) {
				System.out.println(a.getStulist().get(i).toString());
			}
			sum+=a.getStulist().size();
		}
		System.out.println("本校共有"+sum+"个学生");
		System.out.println("===========");
	}

	/**
	 * 查看指定班级信息
	 */
	public static void check1(Scanner sc) {
		System.out.println("请输入要查看的班级id");
		int id = sc.nextInt();
		int index = -1;
		for (Clazz c : clazz) {
			if (id == c.getId()) {
				System.out.println(c.getId() + "班有" + c.getStulist().size() + "个学生");
				for (int i = 0; i <= c.getStulist().size() - 1; i++) {
					System.out.println(c.getStulist().get(i).toString());
					index = i;
				}
			}
		}
		if (index == -1) {
			System.out.println("没有该班级");
		}

	}

	/**
	 * 增加班级
	 */

	public static void add(Scanner sc) {
		//判断输入班级id是否存在
		System.out.println("请输入增加的班级id");
		int cID = sc.nextInt();
		for(Clazz c:clazz) {
			if(c.getId()==cID) {
				System.out.println("此班级id已经存在!!!");
				return;
			}
		}

		//用户输入一个老师对象
		System.out.println("请输入一个老师信息,格式为:3-CC-CC@lanqiao.org-数学");
		String s = sc.next();
		String[] infos = s.split("-");
		Teacher t3 = new Teacher(Integer.parseInt(infos[0]),infos[1],infos[2],infos[3]);
		//判断输入的老师id是否存在
		for(Clazz c:clazz) {
			if(t3.getId()==c.getT().getId()) {
				System.out.println("此id已经存在");
				return;
			}
		}
		//添加学生
		System.out.println("请输入要添加的学生个数");
		int n = sc.nextInt();
		//因为是新班级,所以要重新创建一个学生容器
		List<Student> stul = new ArrayList<>();
		System.out.println("请输入要添加的学生信息,格式为:6-ff-ff@lanqiao.org-77");
		//定义一个统计学生的变量
		int num=0;
		//当新增学生数量大于输入的就结束循环
		a:while(num<n) {
			String s1 = sc.next();
			String[] info = s1.split("-");
			//判断输入格式是否正确
			if(info.length<4) {
				System.out.println("输入信息错误");
				continue a;
			}
			
			//创建学生对象
			Student newStu = new Student(Integer.parseInt(info[0]),info[1],info[2],Double.parseDouble(info[3]));
			//判断输入的学生id是否存在
			for(Clazz c :clazz) {
				for(int i = 0;i<c.getStulist().size();i++) {
					if(newStu.getId()==c.getStulist().get(i).getId()) {
						System.out.println("该学号已经存在!!!");
						continue a;
					}
				}
			}
			stul.add(newStu);
			num++;
		}
		clazz.add(new Clazz(t3, stul, cID));
		System.out.println("添加成功!");
	}

	/**
	 * 删除班级
	 */
	public static void delete(Scanner sc) {
		System.out.println("请输入要删除的班级id");
		int id = sc.nextInt();
		int index =-1;
		for(int i =0;i<=clazz.size();i++) {
			if(id==clazz.get(i).getId()) {
				clazz.remove(id-1);
				index=i;
				System.out.println("删除成功!!");
				break;
			}
		}
		if(index==-1) {
			System.out.println("没有该班级!!!");
		}
		System.out.println("=======");
	}
}

2、学生的服务类如下:

import java.util.Scanner;

import org.lanqiao.school.Clazz;
import org.lanqiao.school.Student;

public class StudentService extends ClassService {
	public static void studentAll(Scanner sc) {
		c: while (true) {
			System.out.println("===学生信息管理===");
			System.out.println("1、增加一个学生(先输入一个班级)");
			System.out.println("2、删除一个学生");
			System.out.println("3、更新一个学生信息");
			System.out.println("4、学生转班");
			System.out.println("5、返回上一级菜单");
			int input = sc.nextInt();
			switch (input) {
			case 1:
				stuAdd(sc);
				break;
			case 2:
				stuDelete(sc);
				break;
			case 3:
				stuUpdate(sc);
				break;
			case 4:
				stuZb(sc);
				break;
			case 5:
				break c;
				default:
					System.out.println("没有此功能!");
					break;
			}
		}
	}

	/**
	 * 增加一个学生 先输入班级
	 */
	public static void stuAdd(Scanner sc) {
		System.out.println("请先输入要增加到哪一个班级");
		int id = sc.nextInt();
		//定义一个下标,判断是否存在这个班级
		int index = -1;
		for (Clazz c : clazz) {
			if (id == c.getId()) {
				System.out.println(c.getId() + "班共有" + c.getStulist().size() + "个学生");
				for (int i = 0; i <= c.getStulist().size() - 1; i++) {
					System.out.println(c.getStulist().get(i).toString());
					index = i;
				}
			}
		}
		if (index == -1) {
			System.out.println("没有该班级");
			return;
		}
		System.out.println("请输入你要增加的学生信息,例如(5-ff-ff@lanqiao.org-80)");
		String s = sc.next();
		try {
			String[] infos = s.split("-");
			// 创建学生对象
			Student newStu = new Student(Integer.parseInt(infos[0]), infos[1], infos[2], Double.parseDouble(infos[3]));
			for (Clazz c : clazz) {
				for (int i = 0; i < c.getStulist().size(); i++) {
					if (newStu.getId() == c.getStulist().get(i).getId()) {
						System.out.println("输入有误,学号不能一样!");
						return;
					}
				}
				// c.getStulist().add(newStu);
			}
			for (int i = 0; i < clazz.size(); i++) {
				if (id == clazz.get(i).getId()) {
					clazz.get(i).getStulist().add(newStu);
				}

			}
			System.out.println("添加成功!!");
		} catch (Exception e) {
			System.out.println("输入有误!");
			// TODO: handle exception
		}
	}

	/**
	 * 删除
	 */
	public static void stuDelete(Scanner sc) {
		System.out.println("请输入要删除的学生id");
		int id = sc.nextInt();
		int index = -1;
		for (Clazz c : clazz) {
			for (int i = 0; i < c.getStulist().size(); i++) {
				if (id == c.getStulist().get(i).getId()) {
					index = i;
					c.getStulist().remove(c.getStulist().get(i));
				}
			}
		}
		if (index == -1) {
			System.out.println("没有该学号的学生!");
			return;
		}
		System.out.println("删除成功!");
	}

	/**
	 * 更新
	 */
	public static void stuUpdate(Scanner sc) {
		System.out.println("请输入要更新的学生学号");
		int id = sc.nextInt();
		int index =-1;
		for (Clazz c : clazz) {
			for (int i = 0; i < c.getStulist().size(); i++) {
				if (id == c.getStulist().get(i).getId()) {
					System.out.println(c.getStulist().get(i).toString());
					index=i;
				}
			}
		}
		
		if (index == -1) {
			System.out.println("没有该学号的学生!");
			return;
		}

		System.out.println("请输入修改的信息:vv-vv@lanqiao.org-99  学号不能修改");
		String s = sc.next();
		try {
			String[] infos = s.split("-");
			for(Clazz c:clazz) {
				for(int i =0;i<c.getStulist().size();i++) {
					if(id==c.getStulist().get(i).getId()) {
						c.getStulist().get(i).setName(infos[0]);
						c.getStulist().get(i).setEmail(infos[1]);
						c.getStulist().get(i).setScore(Double.parseDouble(infos[2]));
					}
				}
			}
			System.out.println("更新成功!");
		} catch (Exception e) {
			System.out.println("输入有误,请按要求重新输入");
			// TODO: handle exception
		}
	}

	/**
	 * 转班
	 */
	public static void stuZb(Scanner sc) {
		System.out.println("请输入要转班的学生学号");
		int id1 = sc.nextInt();
		//定义一个下标,判断该学生是否存在
		int index = -1;
		for (Clazz c : clazz) {
			for (int i = 0; i < c.getStulist().size(); i++) {
				if (id1 == c.getStulist().get(i).getId()) {
					System.out.println("该学生原班级为:"+c.getId()+"班");
					System.out.println(c.getStulist().get(i).toString());
					index = i;
				}
			}
		}
		if (index == -1) {
			System.out.println("学号不存在!");
			return;
		}
		System.out.println("请输入要转入的班级的编号");
		int id2 = sc.nextInt();
		int index1 = -1;
		
		//新建一个学生空的学生对象,为了后面把要转班的学生信息给自己
		Student newStu = null;
		for (Clazz c : clazz) {
			for (int i = 0; i < c.getStulist().size(); i++) {
				if (id1 == c.getStulist().get(i).getId()) {
					// 把这个学生信息放入新的学生对象里面
					 newStu=c.getStulist().get(i);
					// 找到这个学生把学生信息在这个班级删除
					c.getStulist().remove(i);
					index1 = i;
				}
			}
		}
		//找到转入的班级,再把学生对象放入
		for (int i = 0; i < clazz.size(); i++) {
			if (clazz.get(i).getId() == id2) {
				clazz.get(i).getStulist().add(newStu);
			}

		}

		if (index1 == -1) {
			System.out.println("该班级不存在");
			return;
		}
		System.out.println("转班成功");
	}
}

3、老师的服务类:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.lanqiao.school.Clazz;
import org.lanqiao.school.Teacher;


public class TeacherService extends ClassService{
	
	public static void teacherAll(Scanner sc) {
		b:while(true) {
			System.out.println("===老师信息管理===");
			System.out.println("1、查看所有老师信息");
			System.out.println("2、修改老师信息");
			System.out.println("3、返回上一级菜单");
			int input = sc.nextInt();
			switch(input) {
			case 1:
				tcAll(sc);
				break;
			case 2:
				tcUpdate(sc);
				break;
			case 3:
				break b;
				default:
					System.out.println("没有该功能!");
					break;
			}
		}
	}
	/**
	 * 查看
	 */
	public static void tcAll(Scanner sc) {
		System.out.println("本校共有"+clazz.size()+"个老师,老师信息如下");
		for(int i = 0;i<clazz.size();i++) {
			System.out.println(clazz.get(i).getT().toString());
		}
		
	}
	/**
	 * 修改
	 */
	public static void tcUpdate(Scanner sc) {
		System.out.println("请输入要修改老师的id");
		int id = sc.nextInt();
		int index = -1;
		for (int i = 0; i < clazz.size(); i++) {
			if (id == clazz.get(i).getId()) {
				System.out.println(clazz.get(i).getT().toString());
				index=i;
			}
		}
		if (index==-1) {
			System.out.println("查无此人!");
			return;
		}
		System.out.println("请输入修改的老师信息,例如(DD-DD@lanqiao.con-英语) 编号不能修改");
		String s = sc.next();
		try {
			String[] infos = s.split("-");

			for(int i =0;i<clazz.size();i++) {
				if(id==clazz.get(i).getId()) {
					clazz.get(i).getT().setName(infos[0]);
					clazz.get(i).getT().setEmail(infos[1]);
					clazz.get(i).getT().setJob(infos[2]);
				}
			}
			
			System.out.println("修改成功!!");
		} catch (Exception e) {
			System.out.println("输入有误,请按要求重新输入!");
			// TODO: handle exception
		}
	}
}

服务类就写完了,下面就是班级,学生,老师的封装类了,代码如下:

1、班级:

import java.util.ArrayList;
import java.util.List;

public class Clazz {
	private Teacher t;
	private List<Student> stulist = new ArrayList<>();
	private int id;

	public Teacher getT() {
		return t;
	}

	public void setT(Teacher t) {
		this.t = t;
	}

	public List<Student> getStulist() {
		return stulist;
	}

	public void setStulist(List<Student> stulist) {
		this.stulist = stulist;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Clazz(Teacher t, List<Student> stulist, int id) {
		super();
		this.t = t;
		this.stulist = stulist;
		this.id = id;
	}

	public Clazz() {
		super();
	}

}

2、学生:

public class Student {
	private int id;
	private String name;
	private String email;
	private double score;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}

	public Student(int id, String name, String email, double score) {
		super();
		this.id = id;
		this.name = name;
		this.email = email;
		this.score = score;
	}

	
	
	@Override
	public String toString() {
		return "学号:" + id + "姓名:" + name+ "邮箱:" + email + "分数:" + score;
	}

	public Student() {
		super();
	}

}

3、老师:

public class Teacher {
	private int id;
	private String name;
	private String email;
	private String job;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	
	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}
	
	public Teacher(int id, String name, String email, String job) {
		super();
		this.id = id;
		this.name = name;
		this.email = email;
		this.job = job;
	}
	


	@Override
	public String toString() {
		return "编号:" + id + ",姓名"+ name+",邮箱:"+ email + ",科目:"+job;
	}

	public Teacher() {
		super();
	}

}

以上就是我的代码部分,在这里说明以下,我也是一个初学者,代码的结构和处理以及代码的优化可能不是很好,对于新手来说还是以实现功能为目的,等你练得多了自己也就会去修改代码,后面我也会更多的给大家说说自己学习java的经历。也希望有更多的朋友为我评论,说说我的不足之处,谢谢!!!