50,687
社区成员
发帖
与我相关
我的任务
分享
public class LineManager {
Scanner input = new Scanner(System.in);
static List<Line> lineList = null;
public static List<? extends Object> getAllEntity(File file) throws ParseException{
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
//读取的是Line.txt
try {
fis = new FileInputStream(file);
isr = new InputStreamReader(fis,"UTF-8");
br = new BufferedReader(isr);
if("Line.txt".equals(file.getName())){
lineList = new ArrayList<Line>();
String temp = "";
while((temp=br.readLine())!=null){
String [] lineTemp = temp.split(",");
Line line = new Line();
line.setTno(lineTemp[0]);
line.setXiname(lineTemp[1]);
line.setXldate(lineTemp[2]);
line.setTs(lineTemp[3]);
line.setStart(lineTemp[4]);
line.setJzd(lineTemp[5]);
line.setFc(lineTemp[6]);
line.setJz(lineTemp[7]);
line.setCdf(lineTemp[8]);
line.setDdd(lineTemp[9]);
line.setXingcheng(lineTemp[10]);
line.setXcbz(lineTemp[11]);
//字符型
line.setStatus(Integer.parseInt(lineTemp[12]));
lineList.add(line);
}
return lineList;
}else if("Line.txt".equals(file.getName())){
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
isr.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
//实体类的持久化
//参数:File写入的文件,Object
public static void insertEntity(File file,Object object){
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
try {
fos = new FileOutputStream(file,true);
osw = new OutputStreamWriter(fos,"UTF-8");
bw = new BufferedWriter(osw);
//以下要写入,写入之前,需要判断写入的实体类型
if(object instanceof Line){
Line line = (Line) object;
//以下按照写入Line的方法
String lineInsert =
String.format("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%d",line.getTno(),line.getXiname(),line.getXldate(),line.getTs()
,line.getStart(),line.getJzd(),line.getFc(),line.getJz(),line.getCdf(),line.getDdd(),line.getXingcheng()
,line.getXcbz(),line.getStatus());
bw.write("\r\n");
bw.write(lineInsert);
bw.flush();
}else if(object instanceof Line){
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bw.close();
osw.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 查询并显示所有线路信息
*/
@SuppressWarnings("unchecked")
public void printItemInFormation(){
try {
lineList = (List<Line>)getAllEntity(new File("entity_text\\Line.txt"));
} catch (ParseException e) {
e.printStackTrace();
}
for(Line i:lineList){
System.out.println(i);
}
}
/**
* 写入线路信息
*/
public void readin(Line line){
System.out.println("请输入线路编号:");
line.setTno(input.next());
System.out.println("请输入线路名称:");
line.setXiname(input.next());
System.out.println("请输入出团日期:(xxxx年x月x日)");
line.setXldate(input.next());
System.out.println("请输入天数:");
line.setTs(input.next());
System.out.println("请输入出发的交通工具:");
line.setStart(input.next());
System.out.println("请输入集合地点:");
line.setJzd(input.next());
System.out.println("请输入返程的交通工具:");
line.setFc(input.next());
System.out.println("请输入接站电话:");
line.setJz(input.next());
System.out.println("请输入出发地:");
line.setCdf(input.next());
System.out.println("请输入抵达地点:");
line.setDdd(input.next());
System.out.println("请输入行程备注:");
line.setXingcheng(input.next());
System.out.println("请输入线路简介:");
line.setXcbz(input.next());
System.out.println("请输入线路开关 1:线路开放 0:线路暂不开放");
line.setStatus(input.nextInt());
register(line);
System.out.println("添加成功\n");
}
/**
* 调用写入的地址
* @param l
*/
public void register(Line line){
File file = new File("entity_text\\Line.txt");
insertEntity(file, line);
}
/**
* 关闭线路操作
*/
public void Delete(Line line){
System.out.println("请输入要删除线路的编号:");
String num=input.next();
int j=-1;
for(int x=0;x<lineList.size();x++){
if(num.equals(lineList.get(x).getTno())){
j=x;
}
}
if(j==-1){
System.out.println("找不到该线路");
}else{
lineList.set(j, line).setStatus(0);
}
}
}