重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.io.BufferedReader;
创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于网站设计制作、做网站、兰西网络推广、重庆小程序开发公司、兰西网络营销、兰西企业策划、兰西品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供兰西建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BookWork {
static ListBook data;
private static Scanner input;
public static void main(String[] args) {
if (!initBook("d:/book.txt")) {
System.out.println("初始图书列表失败 ..");
return;
}
input = new Scanner(System.in);
while (true) {
try {
System.out.println("请输入操作:");
System.out.println("1.找书 2.删除图书 3.退出");
int number = Integer.parseInt(input.next());
if (number == 1) {
findBook();
} else if (number == 2) {
delBook();
} else if (number == 3) {
System.out.println("退出");
break;
} else {
System.out.println("这个不是我要的...重来...");
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("这个不是我要的...重来...");
System.out.println();
}
}
}
private static void delBook() {
System.out.println("请输入要删除的书名或编号:");
String key = input.next();
if (key != null !key.equals("")) {
for (Book book : data) {
if (book.number.equals(key) || book.name.contains(key)) {
data.remove(book);
System.out.println(" 图书 " + book.toString() + " 已删除");
return;
}
}
}
System.out.println("没有您要删除的");
}
private static void findBook() {
System.out.println("请输入要查找的书名或编号:");
String key = input.next();
if (key != null !key.equals("")) {
for (Book book : data) {
if (book.number.equals(key) || book.name.contains(key)) {
System.out.println("找到了 图书 " + book.toString());
return;
}
}
}
System.out.println("没有您要找的");
}
private static boolean initBook(String string) {
try {
System.out.println("图书导入中...");
System.out.println("列表文件 -- " + string);
File file = new File(string);
if (!file.exists()) {
return false;
}
data = new ArrayListBook();
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
String[] strings = line.split(",");
Book b = new Book(strings[0], strings[1]);
data.add(b);
System.out.println("导入" + b.toString());
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static class Book {
String number;
String name;
public Book(String number, String name) {
super();
this.number = number;
this.name = name;
}
@Override
public String toString() {
return "Book [编码:" + number + ", 名称:" + name + "]";
}
}
}
001,金瓶梅
002,杂事秘辛
003,飞燕外传
004,控鹤监秘记
005,汉宫春色
void del_book(){ //我会一种比较笨的办法: //建立一个临时文件,然后从原文件读取图书信息,把要删除的项不写入临时文件,其他全部写入。 //全部读写完成后,改变两个文件的读写状态,把临时文件的内容全部覆盖写入原文件 ofstream outData("temp.txt", ios::out); ifstream inData("d:\\bookInfo.txt", ios::in); if (!outData || !inData) { cout endl "对不起!!!!找不到文件!!!!" endl; system("pause"); return; } string sign; cout endl "你要删除的图书名或者图书号:"; //其他的图书属性也成 cin sign; bool flag = true; string str; while (inData booknumber bookname) { getline(inData, str); if ((sign==booknumber) || (sign==bookname)) { cout endl "你想删除的图书:" endl endl; cout setiosflags(ios::left) setw(5) booknumber " " booknamestr endl; flag = false; break; } outData setiosflags(ios::left) setw(5) booknumber " " bookname str endl; } if (flag) { cout endl "对不起!!!没有该图书的信息!!!!" endl endl; } else { while (getline(inData, str)) { outData str endl; } outData.close(); inData.close(); ofstream out("d:\\bookInfo.txt", ios::out); ifstream in("temp.txt", ios::in); if (!out || !in) { cout endl "对不起不能打开文件!!!" endl endl; return; } while (getline(in, str)) { out str endl; } out.close(); in.close(); cout endl "这本图书的信息已经删除!!!" endl endl; } }
package com.bms;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// book对象
public class Book {
private String bId; // 编号
private String bName; // 书名
// getset方法
public String getbId() {
return bId;
}
public void setbId(String bId) {
this.bId = bId;
}
public String getbName() {
return bName;
}
public void setbName(String bName) {
this.bName = bName;
}
//构造方法
public Book() {
}
public Book(String bId, String bName) {
this.bId = bId;
this.bName = bName;
}
/*
* 增加
* */
public static ListBook add(ListBook list) {
Scanner sn = new Scanner(System.in);
System.out.print("请输入编号:");
String bid = sn.next();
System.out.print("请输入名称:");
String bName = sn.next();
Book book = new Book(bid, bName);
for (Book b : list) {
if (b.bId.equals(book.bId)) {
System.out.println("编号重复,请重新输入!");
return list;
}
}
list.add(book);
System.out.println("添加成功!");
return list;
}
/*
* 查询
* */
public static void query(ListBook list) {
System.out.println("编号\t书名");
for (Book b : list) {
System.out.println(b.getbId() + "\t" + b.getbName());
}
}
/*
* 修改
* */
public static void update(ListBook list) {
query(list);
Scanner sc = new Scanner(System.in); // 键盘输入的对象
System.out.print("请输入编号:");
String s = sc.next();
Integer id = null;
for (int i = 0; i list.size(); i++) {
id = list.get(i).getbId().equals(s) ? i : null;
}
if (id == null) {
System.out.println("输入的编号不存在,请重新选择!");
return;
}
System.out.print("请输入新的书名:");
String newName = sc.next();
list.get(id).setbName(newName);
System.out.print("修改成功!");
}
/*
* 删除
* */
public static void del(ListBook list) {
query(list);
Scanner sc = new Scanner(System.in); // 键盘输入的对象
System.out.print("请输入编号:");
String s = sc.next();
for (int i = 0; i list.size(); i++) {
if (list.get(i).getbId().equals(s)) {
list.remove(i);
return;
}
}
System.out.println("输入的编号不存在,请重新选择!");
}
}
/*
* 测试*/
class Test {
public static void main(String[] args) {
ListBook bookList = new ArrayList(); // 存放所有图书的列表
bookList.add(new Book("1", "Java 基础")); // 图书的列表添加一本图书
System.out.print("欢迎进入图书管理系统,");
boolean b = true;
while (b) {
System.out.print("请选择:\n1.增加\n2.查询\n3.修改\n4.删除\n5.退出\n(输入序号):");
Scanner sn = new Scanner(System.in); // 键盘输入的对象
String select = sn.next();
switch (select) {
case "1":
System.out.println("您选择了增加");
Book.add(bookList);
break;
case "2":
System.out.println("您选择了查询:");
Book.query(bookList);
break;
case "3":
System.out.println("您选择了修改");
Book.update(bookList);
break;
case "4":
System.out.println("您选择了删除");
Book.del(bookList);
break;
case "5":
System.out.println("您选择了退出");
b = false;
System.out.println("退出程序!");
break;
default:
System.out.println("输入错误的序号,请重新输入");
break;
}
}
}
}