1 importjava.util.Scanner;2 importjava.util.LinkedList;3 importjava.util.Iterator;4 import java.io.*;5
6 importjavax.security.auth.kerberos.DelegationPermission;7 public classDiscManager {8 //创建一个disc类
9 public static classDisc{10 int discNum; //编号
11 String discName; //名称
12 String discStatus; //出租状态
13 publicDisc(){14 }15 public Disc(intnum,String name,String status){16 this.discNum=num;17 this.discName=name;18 this.discStatus=status;19 }20 public void setNum(intnum){21 this.discNum=num;22 }23 public voidsetName(String name){24 this.discName=name;25 }26 public voidsetStatus(String status){27 this.discStatus=status;28 }29 public voidprintDiscInfo(){30 System.out.println(this.discNum+" "+this.discName+" "+this.discStatus);31 }32 }33 //读取文件到列表
34 public static LinkedList fileToList(File file)throwsIOException{35 LinkedList myDisc=new LinkedList();36 if(!file.exists()){37 System.out.println("读取文件不存在,请检查!");38 }else{39 BufferedReader br=newBufferedReader(40 newInputStreamReader(41 newFileInputStream(file)));42 String line;43 while((line=br.readLine())!=null){44 Disc tempDisc=newDisc();45 tempDisc.setNum(Integer.parseInt(line));46 if((line=br.readLine())!=null){47 tempDisc.setName(line);48 }49 if((line=br.readLine())!=null){50 tempDisc.setStatus(line);51 }52 myDisc.add(tempDisc);53 }54 br.close();55 }56 returnmyDisc;57 }58
59
60 //输出列表
61 public static void printDiscList(LinkedListlist){62 Iterator it=list.iterator();63 while(it.hasNext()){64 it.next().printDiscInfo();65 System.out.println();66 }67 }68
69 //输出列表信息到文件
70 public static void listToFile(LinkedList list,File file)throwsIOException{71 if(!file.exists()){72 file.createNewFile();73 }74 PrintWriter pw=newPrintWriter(file);75 Iterator it=list.iterator();76 while(it.hasNext()){77 Disc tempDisc=it.next();78 pw.println(tempDisc.discNum+"");79 pw.println(tempDisc.discName);80 pw.println(tempDisc.discStatus);81 }82 pw.close();83 }84
85 //添加影碟信息
86 public static void addDiscToList(LinkedListlist){87 Scanner scan=newScanner(System.in);88 System.out.println("请输入你要添加影碟的名称:");89 String discName=scan.nextLine();90 String discStatus="未出租";91 int discNum=list.size()+1;92 Disc additionDisc=newDisc(discNum,discName,discStatus);93 list.add(additionDisc);94 //scan.close();
95 System.out.println("影碟信息添加完毕!");96 }97 //删除影碟信息
98 public static void delDiscToList(LinkedListlist){99 Scanner scan=newScanner(System.in);100 System.out.println("请输入要删除的影碟编号:");101 int number=scan.nextInt();102 int counter=-1;103 Iterator it=list.iterator();104 Disc tempDisc;105 while(it.hasNext()){106 tempDisc=it.next();107 counter++;108 if(tempDisc.discNum==number){109 break;110 }111 }112 if(counter>=0){113 list.remove(counter);114 }else{115 System.out.println("影碟为空!");116 }117 //scan.close();
118 }119
120 //修改影碟信息
121 public static void modifyDisc(LinkedListlist){122 Scanner scan=newScanner(System.in);123 System.out.println("请输入要修改的影碟编号:");124 int number=scan.nextInt();125 Iterator it=list.iterator();126 while(it.hasNext()){127 Disc tempDisc=it.next();128 if(tempDisc.discNum==number){129 System.out.println("你要修改的碟片信息如下:");130 System.out.println(tempDisc.discNum+" "+tempDisc.discName+" "+tempDisc.discStatus);131 System.out.println("如果修改编号请输入a,修改名字请输入b,修改出租状态请输入c.");132 String chooseChar=scan.nextLine();133 while(!(chooseChar.equals("a"))&&(!chooseChar.equals("b"))&&(!chooseChar.equals("c"))){134 System.out.println("输入有误,修改编号请输入a,修改名字请输入b,修改出租状态请输入c.");135 chooseChar=scan.nextLine();136 }137 if(chooseChar.equals("a")){138 System.out.println("请输入新编号:");139 tempDisc.setNum(scan.nextInt());140 System.out.println("编号修改完成!");141 }142 if(chooseChar.equals("b")){143 System.out.println("请输入新名称:");144 tempDisc.setName(scan.nextLine());145 System.out.println("名称修改完成!");146 }147 if(chooseChar.equals("c")){148 String newDiscStatus=(tempDisc.discStatus.equals("未出租")?"已出租":"未出租");149 tempDisc.setStatus(newDiscStatus);150 System.out.println("出租状态修改完成!");151 }152 System.out.println("修改后的碟片信息为:"+tempDisc.discNum+" "+tempDisc.discName+" "+tempDisc.discStatus);153 break;154 }155 }156 }157
158
159 //主界面显示
160 public static voidshowMainScreen(){161 System.out.println("文件读取完毕!");162 System.out.println("*********************************************************");163 System.out.println("*********************影片出租管理系统v1.0**********************");164 System.out.println("***********************Author:GTS************************");165 System.out.println("*********************************************************");166 System.out.println("请选择:");167 System.out.println("1:影片管理 2、退出系统");168 System.out.println();169 }170
171 //二级菜单界面
172 public static voidshowMenu(){173 System.out.println("*********************影片出租管理系统v1.0**********************");174 System.out.println("请选择操作选项");175 System.out.print("1、显示碟片信息");176 System.out.print(" 2、增加碟片信息");177 System.out.print(" 3、修改碟片信息");178 System.out.print(" 4、删除碟片信息");179 System.out.print(" 5、退出系统");180 System.out.println();181 }182
183 public static void main(String[] args)throwsIOException{184 File file=new File("F:\\霜\\STUDY\\lab\\java\\mydisc.txt");185 LinkedList discShop=fileToList(file);186 showMainScreen();187 Scanner scan=newScanner(System.in);188 intchooseNum;189 while((chooseNum=scan.nextInt())!=1&&(chooseNum!=2)){190 System.out.println("请重新输入选项序号(1或2)");191 }192 if(chooseNum==1){193 showMenu();194 while((chooseNum=scan.nextInt())!=5){195 switch(chooseNum){196 case 1:197 printDiscList(discShop);198 showMenu();199 break;200 case 2:201 addDiscToList(discShop);202 System.out.println("添加完成!");203 showMenu();204 break;205 case 3:206 modifyDisc(discShop);207 showMenu();208 break;209 case 4:210 delDiscToList(discShop);211 System.out.println("删除完成!");212 showMenu();213 break;214 default:215 break;216 }217 }218 System.out.println("*********************************************************");219 System.out.println("*********************影片出租管理系统v1.0**********************");220 System.out.println("***********************Author:GTS************************");221 System.out.println("************************已退出系统!**************************");222 }else{223 System.out.println("退出系统!");224 }225 listToFile(discShop, file);226 /*
227 addDiscToList(discShop);228 printDiscList(discShop);229 delDiscToList(discShop);230 printDiscList(discShop);231 listToFile(discShop, file);232 */
233 //printDiscList(discmg.fileToList(file));
234
235 }236
237 }