这个案例,主要对数组及IO流进行总结!
利用IO流实现简单的图书管理系统,在控制台上打印!
功能:1,查看所有书籍
2,添加书籍
3,删除书籍
4,修改书籍
5,退出
package com.baojian.bookStore;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
/*
* 实体类,主要利用数组,和IO流,对两个知识点进行加强
*/
public class Book {
private int id;
// 书名
private String name ;
// 作者
private String authorName;
// 价格
private double price ;
// 书的数量
private int num;
// 用于从控制台输入内容
Scanner scan = new Scanner(System.in);
// 确定库存数为20
final static int SIZE = 20;
// 创建一个长度为20的数组,用于存放书籍
Book[] books = new Book[SIZE];
// 存放当前图书数量
static int count = 0;
public Book() {
super();
}
public Book(int id,String name, String authorName, double price, int num) {
super();
this.id = id;
this.name = name;
this.authorName = authorName;
this.price = price;
this.num = num;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
//查看图书列表
public void list() throws IOException{
// 从文件中读出数据
books = Service.readData();
// 如果count为0,说明系统里面还没有书籍
if(count == 0 ){
System.out.println("图书管理系统中还没有输入书籍,快去添加吧!");
} else {
// 如果有书籍则遍历书籍
System.out.println("图书编号\t\t" + "书名\t\t" + "作者\t\t" + "价格\t\t" + "数量\t\t");
for (int i = 0; i < count; i++) {
System.out.print(books[i].getId() + "\t\t");
System.out.print(books[i].getName() + "\t\t");
System.out.print(books[i].getAuthorName() + "\t\t");
System.out.print(books[i].getPrice() + "\t\t");
System.out.println(books[i].getNum() + "\t\t");
// 确保书的种类不能多于20种
//count++;
}
}
}
//添加图书
public void add() throws IOException {
System.out.println("----------------------------");
if (count < SIZE) {
// System.out.println("请输入图书编号:");
int id = count;
System.out.println("请输入图书名称:");
String name = scan.next();
System.out.println("请输入作者:");
String authorName = scan.next();
System.out.println("请输入单价:");
double price = scan.nextDouble();
System.out.println("请输入图书数量:");
int num = scan.nextInt();
Book book = new Book(id,name,authorName,price,num);
books[count] = book;
// 保存已经添加的书籍到文本文档中
Service.saveBook(books[count]);
count++;
System.out.println("添加成功!");
} else {
System.out.println("书库已满!");
}
}
/*
//删除图书
public void delete() throws IOException{
System.out.println("----------------------------");
System.out.println("请输入要删除书籍的id:");
//list();
int id = scan.nextInt();
for(int i = id; i < count - 1 ; i++){
//用for循环的形式实现对数组的删除
books[i+1].setId(i);
books[i]=books[i+1];
}
System.out.println("删除成功!");
count--;
// 重写写到文件里面
Service service = new Service();
service.saveBook(books);
}
*/
//删除图书
public void delete() throws IOException{
System.out.println("----------------------------");
System.out.println("请输入要删除书籍的id:");
//list();
int id = scan.nextInt();
// 删除指定索引的元素 修改于2019-12-11
/*for(int i = id; i < count - 1 ; i++){
//用for循环的形式实现对数组的删除
books[i+1].setId(i);
books[i]=books[i+1];
}*/
books = deleteUtil(id,books);
count--;
System.out.println("删除成功!");
// 重写写到文件里面
Service service = new Service();
service.saveBook(books);
}
// 删除数组指定索引的元素
public Book[] deleteUtil(int index, Book[] array) {
//数组的删除其实就是覆盖前一位
Book[] arrNew = new Book[array.length - 1];
for (int i = 0; i < array.length - 1; i++) {
if (i < index) {
arrNew[i] = array[i];
} else {
arrNew[i] = array[i + 1];
}
}
return arrNew;
}
//修改图书
public void update() throws IOException{
System.out.println("----------------------------");
System.out.println("请输入需要修改的图书编号:");
int id = scan.nextInt();
System.out.println("请输入图书名称:");
String name = scan.next();
System.out.println("请输入作者:");
String authorName = scan.next();
System.out.println("请输入单价:");
double price = scan.nextDouble();
System.out.println("请输入图书数量:");
int num = scan.nextInt();
Book book = new Book(id,name, authorName, price, num);
// 修改该书籍的数据
books[id] = book;
Service.saveBook(books);
System.out.println("修改成功!");
}
}
package com.baojian.bookStore;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
/*
* 用于向读取和写入数据
*/
public class Service {
private static final int SIZE = 20;
// 此方法用于从文本文件中读出数据,封装成book对象,存到books中
public static Book[] readData() throws IOException {
// 创建一个Book类型的数组,用于存放读取出来的数据封装成的Book对象
Book[] readBooks = new Book[SIZE];
// 创建一个字符串用于存放读取出来的数据
String data = "";
// 创建一个字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("c:\\bookData.txt"));
// 用于控制存放数组的下标
int id = 0;
// 每次度一行,如果有内容继续读取
while ((data = br.readLine()) != null) {
String[] dataArr = data.split(":");
// 创建Book对象
Book book = new Book();
// 第一个字符串用于设置id
//System.out.println(Integer.parseInt(dataArr[0].trim()));
book.setId(id);
// 第二个字符串用于设置name
book.setName(dataArr[1]);
// 第三个字符串用于设置author
book.setAuthorName(dataArr[2]);
// 第四个字符串用于设置价格
book.setPrice(Double.parseDouble(dataArr[3]));
// 第五个字符串用于设置存量
book.setNum(Integer.parseInt(dataArr[4]));
// 给数组中的元素赋值
readBooks[id] = book;
id++;
}
// 确定数据个数,就是书籍的种类
Book.count = id;
br.close();
// 返回读取到的数据
return readBooks;
}
// 将新添加的书籍保存到文本文档中
public static void saveBook(Book book) throws IOException {
// 创建字符输入缓冲流对象
// BufferedWriter bw = new BufferedWriter(new FileWriter("c:\\bookData.txt",true));
// FileWriter fw = new FileWriter("c:\\bookData.txt",true);
FileOutputStream fos = new FileOutputStream("c:\\bookData.txt",true);
// 将book中的数据拼接成字符串
StringBuffer data = new StringBuffer();
// 获取书籍的id值
data.append( "\r\n" + book.getId() + ":");
// 获取书名
data.append(book.getName() + ":");
// 获取作者名
data.append(book.getAuthorName() + ":");
// 获取价格
data.append(book.getPrice() + ":");
// 获取库存数
data.append(book.getNum() + "");
// 创建数据字符串
String dataStr = data.toString();
byte[] buf = dataStr.getBytes();
// 控制写入数据的长度
int len = buf.length;
fos.write(buf, 0, len);
fos.close();
// System.out.println("写入成功!");
}
// 重写saveBook方法用于删除和更新后向文件写数据
public static void saveBook(Book[] books) throws IOException{
// 创建字符输入缓冲流对象
// BufferedWriter bw = new BufferedWriter(new FileWriter("c:\\bookData.txt",true));
// FileWriter fw = new FileWriter("c:\\bookData.txt",true);
FileOutputStream fos = new FileOutputStream("c:\\bookData.txt");
// 存放数据
StringBuffer data = new StringBuffer();
//String data = "";
for(int i = 0;i < Book.count;i++){
if(i == 0){
data.append(books[i].getId() + ":");
// 获取书名
data.append(books[i].getName() + ":");
// 获取作者名
data.append(books[i].getAuthorName() + ":");
// 获取价格
data.append(books[i].getPrice() + ":");
// 获取库存数
data.append(books[i].getNum() + "");
}else{
data.append( "\r\n" + books[i].getId() + ":");
// 获取书名
data.append(books[i].getName() + ":");
// 获取作者名
data.append(books[i].getAuthorName() + ":");
// 获取价格
data.append(books[i].getPrice() + ":");
// 获取库存数
data.append(books[i].getNum() + "");
}
}
// 创建数据字符串
String dataStr = data.toString();
byte[] buf = dataStr.getBytes();
// 控制写入数据的长度
int len = buf.length;
fos.write(buf, 0, len);
fos.close();
}
}
package com.baojian.bookStore;
import java.util.Scanner;
/*
* 管理员实体类
*/
public class User {
// 用户名
private String userName ;
// 密码
private String password ;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//登录方法
public void login (){
Scanner sc = new Scanner(System.in);
while(true){
User user = new User();
System.out.println("请输入用户名:");
// 从控制台上输入用户名
user.setUserName(sc.nextLine());
System.out.println("请输入密码:");
// 从控制台上输入密码
user.setPassword(sc.nextLine());
if(user.getUserName().equals("张三") && user.getPassword().equals("123")){
System.out.println("登陆成功!欢迎【" + user.getUserName() + "】使用阅山管理系统");
break;
}else{
System.out.println("登录失败!请重新登录:");
login();
}
}
}
}
package com.baojian.bookStore;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
/*
* 测试类,用于测试功能
*/
public class Test {
public static void main(String[] args) throws IOException {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
User user = new User();
Book book = new Book();
//进行登录
user.login();
while(true){
System.out.println("请输入数字进行操作:1.查看图书列表 2.添加图书 3.删除图书 4.修改图书 5.退出本系统");
int num = sc.nextInt();
switch(num){
case 1 :
// 查看图书列表
book.list();
break;
case 2:
book.add();
break;
case 3:
System.out.println("----------------------------");
book.list();
book.delete();
break;
case 4:
System.out.println("----------------------------");
book.list();
book.update();
break;
case 5:
System.out.println("已退出系统!");
//break aa;
return;
default:
System.out.println("您输入的数字不符合要求!");
break;
}
}
}
}
文本写入格式为:
完成案例测试效果: