这是我初学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类里面:
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 = new ArrayList<>();
static {
Teacher t1 = new Teacher(1, "AA", "[email protected]", "英语");
Teacher t2 = new Teacher(2, "BB", "[email protected]", "语文");
List student1 = new ArrayList<>();
student1.add(new Student(1, "aa", "[email protected]", 85));
student1.add(new Student(2, "bb", "[email protected]", 90));
List student2 = new ArrayList<>();
student2.add(new Student(3, "cc", "[email protected]", 95));
student2.add(new Student(4, "dd", "[email protected]", 100));
student2.add(new Student(5, "ee", "[email protected]", 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("请输入一个老师信息,格式为:[email protected]数学");
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 stul = new ArrayList<>();
System.out.println("请输入要添加的学生信息,格式为:[email protected]");
//定义一个统计学生的变量
int num=0;
//当新增学生数量大于输入的就结束循环
a:while(num
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("请输入你要增加的学生信息,例如([email protected])");
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("请输入修改的信息:[email protected] 学号不能修改");
String s = sc.next();
try {
String[] infos = s.split("-");
for(Clazz c:clazz) {
for(int i =0;i
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
服务类就写完了,下面就是班级,学生,老师的封装类了,代码如下:
import java.util.ArrayList;
import java.util.List;
public class Clazz {
private Teacher t;
private List stulist = new ArrayList<>();
private int id;
public Teacher getT() {
return t;
}
public void setT(Teacher t) {
this.t = t;
}
public List getStulist() {
return stulist;
}
public void setStulist(List stulist) {
this.stulist = stulist;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Clazz(Teacher t, List stulist, int id) {
super();
this.t = t;
this.stulist = stulist;
this.id = id;
}
public Clazz() {
super();
}
}
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();
}
}
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的经历。也希望有更多的朋友为我评论,说说我的不足之处,谢谢!!!