第一次在CSDN上发文章,大家看看我的Java期末作业

本文展示了一个使用Java编写的简单学生信息管理系统,实现了学生数据的增、删、改、查以及导入导出功能,运用了面向对象编程思想,包含主菜单、基本信息管理和成绩管理等模块。
摘要由CSDN通过智能技术生成

本人目前处于一个双非二本大学学习软件工程,进入大学之前没有任何计算机语言基础,刚刚浅学一部分C语言基础和Java语言基础,下面是我完成一部分的Java程序。

面向对象综合设计

应用面向对象程序设计的基本思想,模拟课堂讲授的“学生信息管理系统”,完成一个简单的系统设计,实现数据的增、删、改、查功能及数据的导入导出、统计汇总功能。

Menu

import java.util.Scanner;
import java.io.IOException;



public class Menu {
StudentView sv=new StudentView();


public static void main(String[] args)throws IOException{
Menu menu = new Menu();
menu.processMain();
}

//主菜单
public static void mainMenu() {
System.out.println("*********学生信息管理系统*********");
System.out.println(" 1.基本信息管理");
System.out.println(" 2.成绩管理");
System.out.println(" 3.退出系统");
System.out.println("*******************************");
}

//基本信息管理菜单
public void infoMenu() {
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(" 6.导入学生信息");
System.out.println(" 7.导出学生信息");
System.out.println(" 8.返回主菜单");
System.out.println("*******************************");
}

//成绩管理菜单
public void scoreMenu() {
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("*******************************");
}

//主菜单控制逻辑
public void processMain() throws IOException{
mainMenu();
while (true) {
Scanner input = new Scanner(System.in);
System.out.print("<学生信息管理系统>");
int choice = input.nextInt();
switch (choice) {
case 1:
processInfo();
break;
case 2:
processScore();
break;
case 3:
System.out.println("系统结束运行,您已退出");
System.exit(1);
break;
default:
System.out.println("输入错误!");

}
}
}

//基本信息管理菜单控制逻辑
public void processInfo() throws IOException{
infoMenu();
while (true) {
Scanner input = new Scanner(System.in);
System.out.print("<学生基本信息管理>");
int choice = input.nextInt();
switch (choice) {
case 1:
// System.out.println("添加学生信息");
sv.add();
break;
case 2:
System.out.println("显示学生信息");
break;
case 3:
System.out.println("修改学生信息");
break;
case 4:
System.out.println("删除学生信息");
break;
case 5:
System.out.println("查找学生信息");
break;
case 6:
System.out.println("导入学生信息");
//sv.
break;
case 7:
System.out.println("导出学生信息");
//sv.
break;
case 8:
processMain();
default:
System.out.println("输入错误!");

}
}
}

//成绩管理菜单控制逻辑
public void processScore() throws IOException {
scoreMenu();
while (true) {
Scanner input = new Scanner(System.in);
System.out.print("<成绩管理模块>");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("课程平均分");
break;
case 2:
System.out.println("课程最高分");
break;
case 3:
System.out.println("课程最低分");
break;
case 4:
System.out.println("不及格人数");
break;
case 5:
processMain();
default:
System.out.println("输入错误!");
}
}
}

}

Student

//模型类
public class Student {
private String id; //学号
private String name; //成绩
private int math; //数学成绩
private int chinese; //语文成绩
private int english; //英语成绩
public Student(String id, String name, int chinese, int english){
}

public Student(String id, String name, int math, int chinese, int english) {
this.id = id;
this.name = name;
this.math = math;
this.chinese = chinese;
this.english = english;
}

public String getId() {
return id;
}

public String getName() {
return name;
}

public int getMath() {
return math;
}

public int getChinese() {
return chinese;
}

public int getEnglish() {
return english;
}

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

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

public void setMath(int math) {
this.math = math;
}

public void setChinese(int chinese) {
this.chinese = chinese;
}

public void setEnglish(int english) {
this.english = english;
}
public String toString(){
return id+"."+name+"."+math+"."+chinese+"."+english;
}
}

StudentList



import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

//控制器类
public class StudentList implements Iterable<Student>{
@Override
public Iterator<Student> iterator() {
return stuList.iterator();
}
private ArrayList<Student> stuList=new ArrayList<>();

//+
private ArrayList<Student> studentList;

public StudentList() {
studentList = new ArrayList<>();
}

public int size() {
return studentList.size();
}
//+

private String id;
//判断学号是否已存在
private boolean idExists(String id){
for (int i=0;i< stuList.size();i++){
Student st=stuList.get(i);
if(st.getId().equals(id))
return true;
}
return false;
}
private Student[] stus; //Student数组存放学生对象
private int total=0; //实际学生人数(数组实际元素个数)
public StudentList(int maxLength){
stus=new Student[maxLength];
}



//添加学生信息
public boolean addStudent(Student stu){
if(total>stus.length){
return false;
}
stus[total++]=stu;
return true;
}
public Student[] getAllStudent(){
Student[] allStu=new Student[total];
for(int i=0;i<total;i++){
allStu[i]=stus[i];
}
return allStu;
}
public void add(){
Scanner input=new Scanner(System.in);
System.out.print("学号:");
if(idExists(id)){
System.out.println("该生已存在,不能执行添加操作");
}else{
System.out.println("姓名:");
String name=input.next();
String id= String.valueOf(enterScore("学号"));
int math=enterScore("数学");
int chinese=enterScore("语文");
int english=enterScore("英语");
Student stu=new Student(id,name,math,chinese,english);
}
}
//学生信息显示
public void show(){
System.out.println("----------学生信息----------");
System.out.println("学号\t姓名\t数学\t语文\t英语");
Student[] stuList = new Student[0];
for(Student st:stuList){
System.out.println(st.getId()+"\t"+st.getName()+"\t\t"+st.getMath()+"\t\t"+st.getChinese()+"\t\t"+st.getEnglish());
}
System.out.println("---------------------------");
}
//成绩的异常处理
private int enterScore(String msg) {
int score=0;
while(true) {
try {
System.out.print(msg);
Scanner input = new Scanner(System.in);
score = input.nextInt();
if(score>=0&&score<=100)
break;
else
System.out.println("输入错误,成绩应该为0——100");
} catch (Exception e) {
System.out.println("输入错误,成绩应该为0——100");
}
}
return score;
}
}

StudentView

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

import static java.nio.file.Files.find;
public class StudentView {
private StudentList stuList=new StudentList(200);


//判断学号是否存在
private boolean idExists(String id) {
for (Student stu : stuList) {
if (stu.getId().equals(id)) {
return true;
}
}
return false;
}

//添加学生信息
public void add(){
Scanner input=new Scanner(System.in);
System.out.println("学号");
String id= input.next();
int index=find(id); //查找该学号是否存在(-1:不存在;其他值:已存在),已存在则不能添加
if(index!=-1){
System.out.println("该生已存在,不能执行添加操作");
}else{
System.out.println("姓名:");
String name = input.next();
System.out.println("数学:");
int math=input.nextInt();
System.out.println("语文:");
int Chinese=input.nextInt();
System.out.println("英语:");
int English=input.nextInt();
Student stu=new Student(id,name,math,Chinese,English);
boolean added=stuList.addStudent(stu);
if(added){
System.out.println("添加成功!");
}else{
System.out.println("添加失败!");
}
}
}
//查找学生:按学号查找其所在位置
public int find(String id){
int index=-1;
Student[] allStu= stuList.getAllStudent();;
for(int i=0;i< allStu.length;i++){
if(id.equals(allStu[i].getId())){
index=i;
}
}
return index;
}

//成绩异常处理
private int enterScore(String msg){
int score;
while(true){
try{
System.out.println(msg);
Scanner input=new Scanner(System.in);
score=input.nextInt();
if(score>=0&&score<=100)
break;
else
System.out.println("输入错误,成绩应为0——100!");
}catch (Exception e){
System.out.println("输入错误,成绩应为0——100!");
}
}
return score;
}

//数据导入
public void load() {
Scanner input = new Scanner(System.in);
System.out.println("请输入要导入的文件名:");
String fileName = input.next();
File file = new File(fileName);
if (file.exists()) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String str;
while ((str = br.readLine()) != null) {
String[] stuInfo = str.split(",");
String id = stuInfo[0];
String name = stuInfo[1];
int math = Integer.parseInt(stuInfo[2]);
int chinese = Integer.parseInt(stuInfo[3]);
int english = Integer.parseInt(stuInfo[4]);
Student stu = new Student(id, name, chinese, english);
if (idExists(id)) {
System.out.println("该学生已经存在!");
} else {
stuList.addStudent(stu);
}
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally{
if (br!=null){
try {
br.close();
}catch (IOException e) {
throw new RuntimeException(e);
}

}
}
System.out.println("导入成功!");
} else {
System.out.println("要导入的文件不存在!");
}
}

//数据导出
public void save(){
Scanner input = new Scanner(System.in);
System.out.println("请输入要导出的文件名:");
String fileName = input.next();
try{
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));

for(Student stu : stuList) {
bw.write(stu.toString());
bw.newLine();
}
bw.close();
System.out.println("导出成功!");
}catch(IOException e){
throw new RuntimeException(e);
}
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值