import?java.io.BufferedReader;
創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計制作、做網(wǎng)站、蘭西網(wǎng)絡(luò)推廣、重慶小程序開發(fā)公司、蘭西網(wǎng)絡(luò)營銷、蘭西企業(yè)策劃、蘭西品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供蘭西建站搭建服務(wù),24小時服務(wù)熱線:028-86922220,官方網(wǎng)址: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("圖書導(dǎo)入中...");
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("導(dǎo)入"?+?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,控鶴監(jiān)秘記
005,漢宮春色
void del_book(){ //我會一種比較笨的辦法: //建立一個臨時文件,然后從原文件讀取圖書信息,把要刪除的項不寫入臨時文件,其他全部寫入。 //全部讀寫完成后,改變兩個文件的讀寫狀態(tài),把臨時文件的內(nèi)容全部覆蓋寫入原文件 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 "這本圖書的信息已經(jīng)刪除!!!" 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;
}
//構(gòu)造方法
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("編號重復(fù),請重新輸入!");
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 基礎(chǔ)")); // 圖書的列表添加一本圖書
System.out.print("歡迎進入圖書管理系統(tǒng),");
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;
}
}
}
}