编写一个控制台应用程序,实现一个学生管理系统。每个学生有二个属性:学号与姓名。
要求此程序实现以下功能:
启动程序时从文件读取学生数据库。结束程序前保存学生的信息到文件。
程序运行时显示一个主菜单:
[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 Main {
public static void main(String[] args) {
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:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
System.exit(0);
break;
default:
System.out.println("选择错误,请重新选择!");
System.out.println("\n按回车键继续....");
br.read();
}
}
catch (Exception e) {
System.out.println("您输入的不是数字,请重新输入!");
}
}
}
要求此程序实现以下功能:
启动程序时从文件读取学生数据库。结束程序前保存学生的信息到文件。
程序运行时显示一个主菜单:
[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 Main {
public static void main(String[] args) {
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:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
System.exit(0);
break;
default:
System.out.println("选择错误,请重新选择!");
System.out.println("\n按回车键继续....");
br.read();
}
}
catch (Exception e) {
System.out.println("您输入的不是数字,请重新输入!");
}
}
}
public class Main {
public static void main(String[] args) {
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: break;
case 1:
search();
break;
case 2:
input();
break;
case 3:
delete();
break;
case 4:
list();
break;
case 5:
System.exit(0);
break;
default:
System.out.println("选择错误,请重新选择!");
System.out.println("\n按回车键继续....");
br.read();
}
}
catch (Exception e) {
System.out.println("您输入的不是数字,请重新输入!");
}
}
}
public class Main {
public static void main(String[] args) throws Exception{
try{
InputStream f = new FileInputStream("stu.txt");
System.out.println("stu文本内容是:");
int n=50;
for(int i=0;i<n;i++)
System.out.print((char)f.read());
System.out.println(" ");
}
catch(FileNotFoundException fnfe){
System.out.println(fnfe);
}
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: break;
case 1:
//search();
break;
case 2:
//input();
break;
case 3:
//delete();
break;
case 4:
//list();
break;
case 5:
System.exit(0);
break;
default:
System.out.println("选择错误,请重新选择!");
System.out.println("\n按回车键继续....");
br.read();
}
}
catch (Exception e) {
System.out.println("您输入的不是数字,请重新输入!");
}
}
}
public class Main {
public static void main(String[] args) throws Exception{
InputStream f = new FileInputStream("stu.txt");
System.out.println("stu文本内容是:");
int n=100;
for(int i=0;i<n;i++){
System.out.print((char)f.read()); }
System.out.println(" ");
}
}stu.txt文本是:
071806164 ylw
071806164 叶
071806163 郑
071806165 俞
071806166 林
可是运行结果汉字显示不出来,这怎么弄呢?
转到 /usr/java/j2sdk1.4.0_03/jre/lib/ (我用的是j2sdk1.4.0_03)目录下,
用font.properties.zh.Turbo 代替 font.properties,
编辑font.properties,把 -tlc-song 替换为 -misc-simsun (即你所用的中文 truetype 字库)。
其他版本也可按照此法试一试,情况差不多。:)
public class Main {
public static void main(String[] args) throws Exception{
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("-------------------------");
break;
case 1:
System.out.println("-------------------------");
//search();
break;
case 2:
System.out.println("-------------------------");
//input();
break;
case 3:
System.out.println("-------------------------");
//delete();
break;
case 4:
System.out.println("-------------------------");
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);
}
System.out.println("-------------------------");
break;
case 5:
System.exit(0);
break;
default:
System.out.println("选择错误,请重新选择!");
System.out.println("\n按回车键继续....");
br.read();
}
}
catch (Exception e) {
System.out.println("您输入的不是数字,请重新输入!");
}
} }以上是我修改后代码,读取文本也可以了,list all功能也实现了,请帮我看看其它功能怎么实现
然后分别在case 中调用相应的方法
import java.util.*;
import java.io.*;
class Student implements Serializable {
public Hashtable Student=new Hashtable();
public Student(){}
public void display(){
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 void Search(double StuNO){
if(Student.containsKey(StuNO)){
System.out.print(StuNO+" ");
System.out.println(Student.get(StuNO));
}
else System.out.println("您输入的学号不存在");
}
public void Input(double StuNO,String StuName){
if(Student.containsKey(StuNO)) System.out.println("该学生已经存在");
else Student.put(StuNO, StuName);
}
public Hashtable ListAll(){
return Student;
}
public void Delete(double StuNO){
Student.remove(StuNO);
}
}public class student {
public static void main(String args[])throws Exception{
int i=0;
while(i==0){
new Student().display();
System.out.println("");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
if(Integer.valueOf(s).intValue()==1){
System.out.println("请输入学号");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
String s1=br1.readLine();
double number=Integer.valueOf(s1).doubleValue();
new Student().Search(number);
}
else if(Integer.valueOf(s).intValue()==2){
System.out.println("请输入学号");
BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
String s2=br2.readLine();
double number=Integer.valueOf(s2).doubleValue();
System.out.println("请输入姓名");
BufferedReader br3=new BufferedReader(new InputStreamReader(System.in));
String s3=br3.readLine();
new Student().Input(number, s3);
}
}
}
}
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){}
}
}