编写一个控制台应用程序,实现一个学生管理系统。每个学生有二个属性:学号与姓名。
要求此程序实现以下功能:
启动程序时从文件读取学生数据库。结束程序前保存学生的信息到文件。
程序运行时显示一个主菜单:
[0]Main Menu [1]search [2]input [3]delete [4]list all [5]exit
用户通过输入0-5之间的数字进行相应的操作:
选项0:回到主菜单
选项1:查找一个学生。提示输入学生的学号,查到则输出学生的姓名,未查到给出提示信息。执行完毕回到主菜单。
选项2:新增一个学生。先提示输入学生的学号,输入之后提示输入姓名,输入结束返回主菜单。
选项3:删除一个学生。提示输入学生的学号,通过学号来删除学生,执行完毕回到主菜单。
选项4:列出所有的学生信息,包括学号和姓名。
选项5:结束程序。
以下是我编了,有三个功能没实现,大家能不能帮我编下
import java.io.*;
public class xsglxt{
public static void main(String args[]){
Person p=new Person(1001,"叶龙务");
p.menu();
try{
FileOutputStream f1=new FileOutputStream("stu.txt");
ObjectOutputStream s1=new ObjectOutputStream(f1);
s1.writeObject(p);
s1.close();
}catch(IOException ioe){
System.out.println(ioe);
}
p=null;
try{
FileInputStream f2=new FileInputStream("stu.txt");
ObjectInputStream s2=new ObjectInputStream(f2);
p=(Person)(s2.readObject());
s2.close();
System.out.println(p.sno+" "+p.name);
}catch(IOException ioe){
System.out.println(ioe);
}
catch(ClassNotFoundException c){
System.out.println(c);
}
}
}
class Person implements Serializable{
int sno;
String name;
public Person(int sno,String name)
{
this.sno=sno;
this.name=name;
}
void menu()
{
System.out.println("------------------");
System.out.println(" 学生管理系统 ");
System.out.println("------------------");
System.out.println("[0]Main Menu");
System.out.println("[1]search");
System.out.println("[2]input");
System.out.println("[3]delete ");
System.out.println("[4]list all");
System.out.println("[5]exit");
System.out.println("------------------");
System.out.println("请输入你的选择:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try {
String str=br.readLine();
int num=Integer.parseInt(str);
switch(num)
{
case 0:
System.out.println("返回主菜单");
this.menu();
break;
case 1:
search();
break;
case 2:
input();
break;
case 3:
delete();
break;
case 4:
this.list();
break;
case 5:
this.exit();
break;
default:
this.defau();
}
}catch (Exception e) {
System.out.println("您输入的不是数字,请重新输入!");
}
}
void search()
{
}
void input()
{}
void delete()
{
}
void list()
{
try{
int i=0;
FileReader fr=new FileReader("stu.txt");
while((i=fr.read())!=-1){
System.out.print((char)i);
}
System.out.println();
fr.close();
}
catch(IOException e){
System.out.println(e);
}
}
void exit()
{
System.out.println("您己退出学生管理系统!");
System.exit(0);
}
void defau()
{
System.out.println("选择错误,请重新选择!");
this.menu();
}
}
要求此程序实现以下功能:
启动程序时从文件读取学生数据库。结束程序前保存学生的信息到文件。
程序运行时显示一个主菜单:
[0]Main Menu [1]search [2]input [3]delete [4]list all [5]exit
用户通过输入0-5之间的数字进行相应的操作:
选项0:回到主菜单
选项1:查找一个学生。提示输入学生的学号,查到则输出学生的姓名,未查到给出提示信息。执行完毕回到主菜单。
选项2:新增一个学生。先提示输入学生的学号,输入之后提示输入姓名,输入结束返回主菜单。
选项3:删除一个学生。提示输入学生的学号,通过学号来删除学生,执行完毕回到主菜单。
选项4:列出所有的学生信息,包括学号和姓名。
选项5:结束程序。
以下是我编了,有三个功能没实现,大家能不能帮我编下
import java.io.*;
public class xsglxt{
public static void main(String args[]){
Person p=new Person(1001,"叶龙务");
p.menu();
try{
FileOutputStream f1=new FileOutputStream("stu.txt");
ObjectOutputStream s1=new ObjectOutputStream(f1);
s1.writeObject(p);
s1.close();
}catch(IOException ioe){
System.out.println(ioe);
}
p=null;
try{
FileInputStream f2=new FileInputStream("stu.txt");
ObjectInputStream s2=new ObjectInputStream(f2);
p=(Person)(s2.readObject());
s2.close();
System.out.println(p.sno+" "+p.name);
}catch(IOException ioe){
System.out.println(ioe);
}
catch(ClassNotFoundException c){
System.out.println(c);
}
}
}
class Person implements Serializable{
int sno;
String name;
public Person(int sno,String name)
{
this.sno=sno;
this.name=name;
}
void menu()
{
System.out.println("------------------");
System.out.println(" 学生管理系统 ");
System.out.println("------------------");
System.out.println("[0]Main Menu");
System.out.println("[1]search");
System.out.println("[2]input");
System.out.println("[3]delete ");
System.out.println("[4]list all");
System.out.println("[5]exit");
System.out.println("------------------");
System.out.println("请输入你的选择:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try {
String str=br.readLine();
int num=Integer.parseInt(str);
switch(num)
{
case 0:
System.out.println("返回主菜单");
this.menu();
break;
case 1:
search();
break;
case 2:
input();
break;
case 3:
delete();
break;
case 4:
this.list();
break;
case 5:
this.exit();
break;
default:
this.defau();
}
}catch (Exception e) {
System.out.println("您输入的不是数字,请重新输入!");
}
}
void search()
{
}
void input()
{}
void delete()
{
}
void list()
{
try{
int i=0;
FileReader fr=new FileReader("stu.txt");
while((i=fr.read())!=-1){
System.out.print((char)i);
}
System.out.println();
fr.close();
}
catch(IOException e){
System.out.println(e);
}
}
void exit()
{
System.out.println("您己退出学生管理系统!");
System.exit(0);
}
void defau()
{
System.out.println("选择错误,请重新选择!");
this.menu();
}
}
import java.io.*;
class Student{
public void show(){
System.out.println("------------------");
System.out.println("学生管理系统");
System.out.println("------------------");
System.out.println("[0]Main Menu");
System.out.println("[1]search");
System.out.println("[2]input");
System.out.println("[3]delete ");
System.out.println("[4]list all");
System.out.println("[5]exit");
System.out.println("------------------");
System.out.print("请输入你的选择:");
}
public static Properties Student=new Properties();
InputStream in = null;
String filePath="stu.txt";
public Student(){}
public void Search(String StuNO){
try{
InputStream fis = new FileInputStream(filePath);
Student.load(fis);
if(Student.containsKey(StuNO)){
System.out.print(StuNO+" ");
System.out.println(Student.getProperty(StuNO));
}
else System.out.println("输入的学号不存在.");
}
catch(Exception e){}
}
public void Input(String StuNO,String StuName){
try{
InputStream fis = new FileInputStream(filePath);
Student.load(fis);
OutputStream fos = new FileOutputStream(filePath);
if(Student.containsKey(StuNO)) System.out.println("学 生信息已经存在.");
else Student.setProperty(StuNO, StuName);
Student.store(fos, "Update '" + StuNO + "' value " +StuName);
}catch(Exception e){
e.printStackTrace();
}
}
public void ListAll(){
try{
InputStream in = new BufferedInputStream(new FileInputStream(
filePath));
Student.load(in);
Enumeration en = Student.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = Student.getProperty(key);
System.out.println(key + " "+Property);
}
}catch(Exception e){}
}
public void Delete(String StuNO) {
try{
InputStream fis = new FileInputStream(filePath);
Student.load(fis);
if(Student.containsKey(StuNO)) {
Student.remove(StuNO);
System.out.println("已经删除");
}
else System.out.println("学号不存在.");
}
catch(Exception e){}
}
}
public class main {
public static void main(String args[])throws Exception{
int i=0;
while(i==0){
new Student().show();
System.out.println("");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
if(Integer.parseInt(s)<0||Integer.parseInt(s)>5) System.out.println("操作项输入有误.");
if(Integer.parseInt(s)==0){}
else if(Integer.parseInt(s)==1){
System.out.print("请输入查找学生的学号:");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
String s1=br1.readLine();
new Student().Search(s1);
}
else if(Integer.parseInt(s)==2){
System.out.print("请输入新加学生的学号:");
BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
String s2=br2.readLine();
System.out.print("请输入新加学生的姓名:");
BufferedReader br3=new BufferedReader(new InputStreamReader(System.in));
String s3=br3.readLine();
s3=s3.toString();
new Student().Input(s2, s3);
}
else if(Integer.parseInt(s)==3){
System.out.print("请输入要删除的学生的学号:");
BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
String s2=br2.readLine();
new Student().Delete(s2);
}
else if(Integer.parseInt(s)==4){
new Student().ListAll();
}
else if(Integer.parseInt(s)==5){
System.exit(0);
}
}
}
}我修改程序功能基本正常了,就是[3]delete删除后还在,这怎么改
你完全可以把数据放到数据库中,
先定义一个delete的SQL语句,
然后再通过判断条件来进行删除.
下面是我做的KTV项目的里面的一个方法,供LZ参考一下:
//删除包间类型语句
public boolean deleteRoomType(String roomType) {
boolean flag = false;
Connection con = null;
PreparedStatement stmt = null;
ResultSet set = null;
String sql = "delete from ktv_waiter where roomtype =?";
System.out.println(sql);
try {
con = new DbConnection().getConnection();
stmt = con.prepareStatement(sql);
stmt.setString(0, roomType);
stmt.executeUpdate();
flag = true;
} catch (Exception e) {
System.out.println("异常信息: " + e.getMessage());
}
return flag;
}
import java.util.*;
import java.io.*;
public class xsglxt {
public static void main(String args[])throws Exception{
Student s=new Student();
s.main();
}
}
class Student{
public void main()
{
System.out.println("------------------");
System.out.println(" 学生管理系统 ");
System.out.println("------------------");
System.out.println("[0]Main Menu");
System.out.println("[1]search");
System.out.println("[2]input");
System.out.println("[3]delete ");
System.out.println("[4]list all");
System.out.println("[5]exit");
System.out.println("------------------");
System.out.println("请输入您的选择:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); try {
String str=br.readLine();
int num=Integer.parseInt(str);
switch(num)
{
case 0:
System.out.println("返回主菜单");
this.main();
break;
case 1:
this.search();
break;
case 2:
this.input();
break;
case 3:
this.delete();
break;
case 4:
this.list();
break;
case 5:
this.exit();
break;
default:
this.defaut();
break;
}
}catch (Exception e) {
System.out.println("您输入的不是数字,请重新输入!");this.main(); }
}
public void search()
{
try{ System.out.print("请输入学号:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
this.find(s);
}catch(IOException e){
System.out.println(e); }
}
public void input()
{
try{ System.out.print("请输入学号:");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
String s1=br1.readLine();
System.out.print("请输入姓名:");
BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
String s2=br2.readLine();
s2=s2.toString();
this.shulu(s1, s2);
}catch(IOException e){System.out.println(e); }
}
public void delete()
{
try{ System.out.print("请输入学号:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
this.del(s);
}catch(IOException e){System.out.println(e); }
}
public void list()
{
try{
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
Student.load(in);
Enumeration en = Student.propertyNames();
if(Student.isEmpty())
{System.out.println("列表为空");this.main();}
else
{ System.out.println("列表中的记录为:");
while (en.hasMoreElements())
{ String key = (String) en.nextElement();
String Property = Student.getProperty(key);
System.out.println(key + " "+Property);
}this.main();}
}catch(Exception e){}
}
public void exit()
{
System.out.println("您己退出学生管理系统!");
System.exit(0);
}
public void defaut()
{
System.out.println("您的选择不在选项中!");
this.main();
}
public static Properties Student=new Properties();
InputStream in = null;
String filePath="stu.txt";
public Student(){}
public void find(String xh)
{
try{
InputStream fis = new FileInputStream(filePath);
Student.load(fis);
if(Student.containsKey(xh)){
System.out.println("查找结果为:");
System.out.print(xh+" ");
System.out.println(Student.getProperty(xh)); this.main();
}
else {System.out.println("该学号不存在."); this.main();}
}
catch(Exception e){}
}
public void shulu(String xh,String xm)
{
try{
InputStream fis = new FileInputStream(filePath);
Student.load(fis);
OutputStream fos = new FileOutputStream(filePath);
if(Student.containsKey(xh))
{ System.out.println("该学生已存在");this.main();}
else Student.setProperty(xh, xm);
Student.store(fos, "Update '" + xh + "' value " +xm);
this.main();
}catch(Exception e){ e.printStackTrace(); }
}
public void del(String xh)
{
try{
InputStream fis = new FileInputStream(filePath);
OutputStream fos = new FileOutputStream(filePath);
Student.load(fis);
if(Student.containsKey(xh))
{
Student.remove(xh);
Student.store(fos, filePath);
System.out.println("该记录已删除");
this.main();
}
else { System.out.println("该学号不存在."); this.main();}
} catch(Exception e){}
}
}