数据结构实用教程(JAVA版)
看完第一章 集合 结合书中代码
稍微修改做个小练习:E盘根目录table.txt内容:C001 程序设计基础 4 蒋建设C002 微机原理与应用 5
张钢C003 高等数学 6 李明 C004 离散数学 5 赵学会C005 世界近代史 2
陈晓C006 数据结构(JAVA版) 8 王凯
课程表类:package com.chujianyun.com;public class Table
{ private
String key;
private String
rest;
public
Table()
{
} public
Table(String key, String
rest)
{
super();
this.key =
key;
this.rest =
rest;
}
public
String
getKey()
{
return
key;
} public void
setKey(String
key)
{
this.key =
key;
} public
String getRest()
{
return
rest;
} public void
setRest(String
rest)
{
this.rest =
rest;
}
@Override
public boolean equals(Object
obj)
{
return
key.equals(((Table)obj).key);
}
@Override
public String toString()
{
return
"Table [key=" + key + ", rest=" + rest +
"]";
}
}文件操作类:package com.chujianyun.com;import
java.io.BufferedReader;import java.io.BufferedWriter;import
java.io.File;import java.io.FileInputStream;import
java.io.FileNotFoundException;import
java.io.FileOutputStream;import java.io.FileReader;import
java.io.FileWriter;import java.io.IOException;import
java.io.InputStreamReader;import java.io.OutputStreamWriter;import
java.util.HashSet;import java.util.LinkedHashSet;import
java.util.Scanner;import java.util.Set;public class FileOperator
{ private
static String filePath =
"E:\\table.txt";
public
static Set
readFile( )
{
Setset = new
LinkedHashSet();
File file = new
File(filePath);
BufferedReader bf =
null;
try
{
bf = new
BufferedReader(new InputStreamReader(new
FileInputStream(file),"GBK"));
String
str,key,rest;
Table tbl
=null;
while((str=bf.readLine())!=null)
{
key =
str.substring(0,4);
rest =
str.substring(5);
tbl = new
Table(key,rest);
set.add(tbl);
}
} catch
(FileNotFoundException e)
{
System.out.println("没有找到文件!");
//e.printStackTrace();
}catch(IOException e)
{
System.out.println("打开或者访问文件异常"+e.getMessage());
//e.printStackTrace();
}finally
{
try
{
bf.close();
} catch
(IOException e)
{
// TODO
Auto-generated catch
block
e.printStackTrace();
}
}
return
set;
} public
static void
writeFile(Setset)
{
File file =
new
File(filePath);
BufferedWriter bw =
null;
//Table tbl
= null;
try
{
bw = new
BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),
"GBK"));
for(Table
tbl:set)
{
bw.write(tbl.getKey()+"
"+tbl.getRest()+"\r\n");
}
bw.flush();
} catch
(IOException e)
{
System.out.println("打开或者访问文件异常"+e.getMessage());
}finally
{
try
{
bw.close();
} catch
(IOException e)
{
System.out.println("写缓冲流关闭异常"+e.getMessage());
}
}
}
public
static void
select()
{
System.out.println("\n-----------------------------------------");
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("-1:退出");
System.out.println("-----------------------------------------");
System.out.print("功能选择:");
}
//根据 key 和
SET查找 public
static Table findTable(String
key,Settbls)
{
for(Table
tbl : tbls)
{
if(tbl.getKey().equals(key))
{
return
tbl;
}
}
return
null;
} public
static void findTableInput(Settbls,Scanner
in)
{
String input
="";
System.out.print("请输入要查找的课程表:");
input
=in.next();
Table tbl =
findTable(input,
tbls);
if(tbl==null)
{
System.out.println("没有找到该课程号对应的课程表");
}else
{
System.out.println("对应的课程表为:"+tbl);
}
}
public
static void editTableInput(Settbls,String
input)
{
String key =
input.substring(0,4);
String rest
=
input.substring(5);
for(Table
tbl:tbls)
{
if(tbl.getKey().equals(key))
{
tbl.setRest(rest);
System.out.println("修改编号为["+key+"]的课程成功!");
}
}
} public
static void editTableInput(Settbls,Scanner
in)
{
String input
="";
System.out.print("请输入要修改的课程表:");
input=in.nextLine();
input=in.nextLine();
editTableInput(tbls,input);
}
public
static void deleteTable(String
key,Settbls)
{
Table
tblRemove = findTable(key,
tbls);
if(tblRemove!=null)
{
tbls.remove(tblRemove);
writeFile(tbls);
System.out.println("删除编号为:["+key+"]的课程成功!");
}else
{
System.out.println("未检索到,课程编号:["+key+"]对应的课程,删除失败!");
}
}
public
static void deleteTableInput(Settbls,Scanner
in)
{
String input
="";
System.out.print("请输入要删除的课程表:");
input
=in.next();
deleteTable(input,
tbls);
}
public
static void addTable(Settbls,Scanner
in)
{
String
input="";
System.out.print("请输入要添加的课程表:");
input=in.nextLine();
input=in.nextLine();
String key =
input.substring(0,4);
String rest
=
input.substring(5);
tbls.add(new
Table(key,rest));
writeFile(tbls);
System.out.print("添加课程表:"+key+"成功!");
}
public
static void
printTables(Settbls)
{
for(Table
tbl:tbls)
{
System.out.println(tbl);
}
}
}测试类:package
com.chujianyun.com;import java.util.Scanner;import
java.util.Set;public class SetDemo
{ public
static void main(String[]
args)
{
Settbls =
FileOperator.readFile();
Scanner in =
new
Scanner(System.in);
FileOperator.select();
int choice
=0;
while((choice
=in.nextInt())!=-1)
{
switch(choice)
{
case -1: System.exit(0);
break;
case 1:
FileOperator.findTableInput(tbls,in);
break;
case
2:
FileOperator.deleteTableInput(tbls,in);break;
case
3:
FileOperator.addTable(tbls,in);break;
case
4:
FileOperator.printTables(tbls);
break;
case
5:
FileOperator.editTableInput(tbls, in);
break;
default:
System.out.println("输入的数字错误请重新输入");
}
FileOperator.select();
}
}}效果:
-----------------------------------------
输入编号进行功能选择:
1:根据课程号查找课程;
2:根据课程号删除课程:
3:向课程表里面追加一个课程记录:
4:显示课程表里面所有课程记录
5:修改课程表里面的课程记录
-1:退出
-----------------------------------------
功能选择:4
Table [key=C001, rest=程序设计基础 4 蒋建设]
Table [key=C002, rest=微机原理与应用 5 张钢]
Table [key=C003, rest=高等数学 6 李明 ]
Table [key=C004, rest=离散数学 5 赵学会]
Table [key=C005, rest=世界近代史 2 陈晓]
Table [key=C006, rest=数据结构(JAVA版) 8 王凯]
-----------------------------------------
输入编号进行功能选择:
1:根据课程号查找课程;
2:根据课程号删除课程:
3:向课程表里面追加一个课程记录:
4:显示课程表里面所有课程记录
5:修改课程表里面的课程记录
-1:退出
-----------------------------------------
功能选择:2
请输入要删除的课程表:C006
删除编号为:[C006]的课程成功!
-----------------------------------------
输入编号进行功能选择:
1:根据课程号查找课程;
2:根据课程号删除课程:
3:向课程表里面追加一个课程记录:
4:显示课程表里面所有课程记录
5:修改课程表里面的课程记录
-1:退出
-----------------------------------------
功能选择:1
请输入要查找的课程表:C005
对应的课程表为:Table [key=C005, rest=世界近代史 2 陈晓]
-----------------------------------------
输入编号进行功能选择:
1:根据课程号查找课程;
2:根据课程号删除课程:
3:向课程表里面追加一个课程记录:
4:显示课程表里面所有课程记录
5:修改课程表里面的课程记录
-1:退出
-----------------------------------------
功能选择:3
请输入要添加的课程表:C006 中国近代史 1 林冲
添加课程表:C006成功!
-----------------------------------------
输入编号进行功能选择:
1:根据课程号查找课程;
2:根据课程号删除课程:
3:向课程表里面追加一个课程记录:
4:显示课程表里面所有课程记录
5:修改课程表里面的课程记录
-1:退出
-----------------------------------------
功能选择:4
Table [key=C001, rest=程序设计基础 4 蒋建设]
Table [key=C002, rest=微机原理与应用 5 张钢]
Table [key=C003, rest=高等数学 6 李明 ]
Table [key=C004, rest=离散数学 5 赵学会]
Table [key=C005, rest=世界近代史 2 陈晓]
Table [key=C006, rest=中国近代史 1 林冲]
-----------------------------------------
输入编号进行功能选择:
1:根据课程号查找课程;
2:根据课程号删除课程:
3:向课程表里面追加一个课程记录:
4:显示课程表里面所有课程记录
5:修改课程表里面的课程记录
-1:退出
-----------------------------------------
功能选择:5
请输入要修改的课程表:C006 JAVA程序设计 8 林冲
修改编号为[C006]的课程成功!
-----------------------------------------
输入编号进行功能选择:
1:根据课程号查找课程;
2:根据课程号删除课程:
3:向课程表里面追加一个课程记录:
4:显示课程表里面所有课程记录
5:修改课程表里面的课程记录
-1:退出
-----------------------------------------
功能选择:4
Table [key=C001, rest=程序设计基础 4 蒋建设]
Table [key=C002, rest=微机原理与应用 5 张钢]
Table [key=C003, rest=高等数学 6 李明 ]
Table [key=C004, rest=离散数学 5 赵学会]
Table [key=C005, rest=世界近代史 2 陈晓]
Table [key=C006, rest=JAVA程序设计 8 林冲]
-----------------------------------------
输入编号进行功能选择:
1:根据课程号查找课程;
2:根据课程号删除课程:
3:向课程表里面追加一个课程记录:
4:显示课程表里面所有课程记录
5:修改课程表里面的课程记录
-1:退出
-----------------------------------------
功能选择:-1