基于IO的图书管理系统

具体要求如下:
1、注册功能,第一位注册的管理员,需要创建管理员信息文件user.txt;管理员信息至少包含账号及密码;后续注册的管理员只需往该文件中添加数据即可;

2、登录功能,控制台提示输入账号及密码,验证账号密码准确性用于确定登录成功与否。

3、登录成功后,进入图书管理界面,管理员选择要进行的操作(可直接在合适位置手动创建book.txt文件,用于保存图书信息)。

4、图书信息至少包含图书编号,图书名称,图书价格,图书作者等信息.

5、根据管理员的选择进行相关操作(查看图书,新增图书,修改图书,删除图书)

public class Login {public static void main(String[] args) throws IOException, ClassNotFoundException {Login login = new Login();login.choose();}public void choose() throws IOException, ClassNotFoundException {System.out.println("-------欢迎来到图书管理系统---------");System.out.println("请选择:1,登录 ");System.out.println("请选择:2,注册 ");Scanner sc = new Scanner(System.in);int clc = sc.nextInt();switch (clc) {case 1:System.out.println("-------登录-------");Login login = new Login();login.login();Management management = new Management();management.mainMenu();break;case 2:System.out.println("-------注册-------");Registration registration = new Registration();registration.registration();break;default:System.out.println("选择有误,退出系统");System.exit(0);}}//登录public void login() throws IOException, ClassNotFoundException {//用户输入账号及密码Scanner sc = new Scanner(System.in);System.out.println("请输入用户名:");String admin = sc.next();System.out.println("请输入密码:");String password = sc.next();String s = admin + ":" + password;Reader reader = new FileReader("user.txt");BufferedReader reader1 = new BufferedReader(reader);String str = "";int t = -1;while ((str = reader1.readLine()) != null) {if (str.equals(s)) {System.out.println("登录成功!!!");t = 1;break;}}if (t == -1) {System.out.println("用户名或密码有误!");Login login = new Login();login.choose();}reader1.close();}
}

public class Registration {Scanner sc = new Scanner(System.in);File file = new File("user.txt");//注册方法public void registration() throws IOException, ClassNotFoundException {if (file.exists()) {file.createNewFile();}System.out.println("请输入账号:");String admin = sc.next();Reader reader = new FileReader(file);BufferedReader bufferedReader = new BufferedReader(reader);String str = "";while ((str = bufferedReader.readLine()) != null) {String[] split = str.split(":");//System.out.println(Arrays.toString(split));if (split[0].equals(admin) ) {System.out.println("此账号已存在!!!");Login login = new Login();login.choose();}}System.out.println("请输入密码:");String password = sc.next();String s = admin + ":" + password;Writer out = new FileWriter(file, true);BufferedWriter bufferedWriter = new BufferedWriter(out);bufferedWriter.newLine();bufferedWriter.write(s);System.out.println("注册成功!!!");bufferedReader.close();bufferedWriter.close();}
}

public class Management {Scanner sc = new Scanner(System.in);private List list = Io.read();public void mainMenu() throws IOException, ClassNotFoundException {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("-------6:退出--------");System.out.println("请选择:");Scanner sc = new Scanner(System.in);int clc = sc.nextInt();switch (clc) {case 1:System.out.println("查看图书");display();mainMenu();break;case 2:System.out.println("新增图书");add();mainMenu();break;case 3:System.out.println("修改图书");altered();mainMenu();break;case 4:System.out.println("删除图书");delete();mainMenu();break;case 5:System.out.println("数据保存");Io io = new Io();io.writer(list);mainMenu();break;case 6:System.out.println("退出");System.exit(0);break;default:System.out.println("输入有误!退出系统");}}//查看图书public void display() {list.forEach(System.out::println);}//新增图书public void add() throws IOException, ClassNotFoundException {System.out.println("请输入要新增图书的ID:");int newId = sc.nextInt();Iterator iterator = list.iterator();while (iterator.hasNext()) {if (iterator.next().getId() == newId) {System.out.println("该图书ID已存在!!!");mainMenu();break;}}System.out.println("请输入要新增图书的名称:");String name = sc.next();System.out.println("请输入要新增图书的价格:");double pre = sc.nextDouble();System.out.println("请输入要新增图书的作者:");String aut = sc.next();Book book = new Book(name, newId, pre, aut);list.add(book);display();}//修改图书public void altered() {System.out.println("请选择你要修改的图书id");int id = sc.nextInt();int index = -1;Iterator iterator = list.iterator();while (iterator.hasNext()) {Book book = iterator.next();if (book.getId() == id) {index = 1;System.out.println("原名称:" + book.getName());String name = sc.next();System.out.println("原价格:" + book.getPrice());double pre = sc.nextDouble();System.out.println("原作者:" + book.getAuthor());String aut = sc.next();book.setId(id);book.setName(name);book.setPrice(pre);book.setAuthor(aut);list.add(book);display();}}if (index == -1) {System.out.println("无此图书!");}}//删除图书public void delete() {System.out.println("请选择要删除图书的id");int id = sc.nextInt();int index = -1;Iterator iterator = list.iterator();while (iterator.hasNext()) {Book book = iterator.next();if (book.getId() == id) {list.remove(book);System.out.println("删除成功");index = 1;}}if (index == -1) {System.out.println("无此图书!");}}
}

public class Io {static File file = new File("book.txt");//读取文件public static List read() {InputStream in = null;ObjectInputStream objectIn = null;List listBook = null;try {in = new FileInputStream(file);objectIn = new ObjectInputStream(in);listBook = (List) objectIn.readObject();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} finally {try {if (objectIn != null)objectIn.close();} catch (IOException e) {e.printStackTrace();}}return listBook;}//写入文件public void writer(List listBook) throws IOException {OutputStream out = new FileOutputStream(file);ObjectOutputStream objectOutputStream = new ObjectOutputStream(out);objectOutputStream.writeObject(listBook);objectOutputStream.close();}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部