站内搜索: 请输入搜索关键词

当前页面: JAVA 编程资料牛鼻论坛Java & J2SE 技术区→JAVA控制台学生管理系统 高手进来回答

JAVA控制台学生管理系统 高手进来回答

发表新主题   回复此主题

第1楼 2007-05-30 23:37 翼仔 写道:

JAVA控制台学生管理系统 高手进来回答

一、实验目的:
1、综合掌握类的封装、输入输出流、对象串行化、容器类。
二、实验内容:
编写一个控制台应用程序,实现一个学生管理系统。每个学生有二个属性:学号与姓名。
三、实验要求:
启动程序时从文件读取学生数据库。结束程序前保存学生的信息到文件。
程序运行时显示一个主菜单:
[0]Main Menu [1]search [2]input [3]delete [4]list all [5]exit
用户通过输入0-5之间的数字进行相应的操作:
选项0:回到主菜单
选项1:查找一个学生。提示输入学生的学号,查到则输出学生的姓名,未查到给出提示信息。执行完毕回到主菜单。
选项2:新增一个学生。先提示输入学生的学号,输入之后提示输入姓名,输入结束返回主菜单。
选项3:删除一个学生。提示输入学生的学号,通过学号来删除学生,执行完毕回到主菜单。
选项4:列出所有的学生信息,包括学号和姓名。
选项5:结束程序。

四、实验学时:3学时
五、实验步骤:
1、创建学生类,实现Serializable接口。
2、创建主类,编写search方法用于查找学生,input 用于输入学生信息,delete 方法用于删除一个学生,list于列出所有学生,exit方法用于结束程序。


第2楼 2013-08-31 12:44 Robot :

JAVA控制台学生管理系统 高手进来回答 相关


第3楼 2007-06-04 18:22 Kimmy★Piggy 写道:

代码见附件。。每次都文字太短。。

命令行执行的时候,输入java q403.Test "/*存储文件路径*/" 即可
附件:q403.rar

第4楼 2007-06-08 03:38 游牧情人 写道:

import java.io.*;
import java.util.*;
class Treemap
{
static TreeMap tm=new TreeMap();
static Student s;
public static void main(String args[]){
try{
FileInputStream fis=new FileInputStream("students.dat");
ObjectInputStream ois=new ObjectInputStream(fis);

while((s=(Student)(ois.readObject()))!=null)
{
tm.put(s.stunum,s);
}
ois.close();
}catch(IOException ioe){
}catch(ClassNotFoundException c){
}
try{
while(true){
System.out.println("[0]Main Menu [1]search [2]input [3]delete [4]list all [5]exit"); //显示菜单
System.out.print("请选择操作:");
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
String ch=br.readLine();
int c=Integer.parseInt(ch);
switch(c){
case 0:
break;
case 1:
search();
break;
case 2:
input();
break;
case 3:
delete();
break;
case 4:
listAll();
break;
case 5:
tm.clear();
System.exit(0);
break;
default:
System.out.println("输入有误!");
}
}
}catch(IOException e){
}catch(NumberFormatException e){
System.out.println("输入有误!");
}
}
static void listAll()
{
tm.clear();
try{
FileInputStream fis=new FileInputStream("students.dat");
ObjectInputStream ois=new ObjectInputStream(fis);

while((s=(Student)(ois.readObject()))!=null)
{
tm.put(s.stunum,s);
System.out.println(s.stunum+" "+s.stuname);
}
ois.close();
}catch(IOException ioe){
}catch(ClassNotFoundException c){
}
}
static void input(){
String str1=null,str2=null;
try{
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
System.out.print("请输入学号:");
str1=br.readLine();
System.out.print("请输入姓名:");
str2=br.readLine();
}catch(IOException e){
System.out.println(e);
}
s=new Student(str1,str2);
tm.put(s.stunum,s);
//写文件
int n=1;
try{
FileOutputStream fos=new FileOutputStream("students.dat");
ObjectOutputStream oos=new ObjectOutputStream(fos);

for(Iterator it=tm.values().iterator();it.hasNext();)
{
s=(Student)it.next();

oos.writeObject(s);
}
oos.close();
}catch(IOException e){
System.out.println(e);
}
}
static void search()
{
String key=null;
try{
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
System.out.print("请输入一个学号:");
key=br.readLine();
}catch(IOException e){
System.out.println(e);
}

if(tm.containsKey(key))
{
s=(Student)tm.get(key);
System.out.println(s.stunum+" "+s.stuname);
}
else
System.out.println("没有");
}
static void delete()
{

String key=null;
try{
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
System.out.print("请输入学号:");
key=br.readLine();
}catch(IOException e){
}
if(tm.remove(key).equals(null))
{
System.out.println("没有");
}
try{
FileOutputStream fos=new FileOutputStream("students.dat");
ObjectOutputStream oos=new ObjectOutputStream(fos);
for(Iterator it=tm.values().iterator();it.hasNext();)
{
s=(Student)it.next();
oos.writeObject(s);
}
oos.close();
}catch(IOException e){
System.out.println(e);
}
}
}
class Student implements Serializable{
String stunum;
String stuname;
public Student(String stunum,String stuname){
this.stunum=stunum;
this.stuname=stuname;
}
}

发表新主题   回复此主题