我是靠谱客的博主 迷路黄蜂,这篇文章主要介绍Java基础案例教程 第七章IO(输入输出)———【任务7-1】保存书店每日交易记录 (字节流),现在分享给大家,希望可以做个参考。

一、任务描述:

 

 

二、运行结果:

 

 

 

 

三、实现思路:

1、图书类

封装成员变量:编号,名称,出版社,单价,库存数量

空参构造,含参构造;

Get,Set 方法;

2、销售书籍实现类

新建 ArrayList<Books>集合,作为书架存放书籍

复制代码
1
public static ArrayList<Books> booksList = new ArrayList<Books>();

初始化书架,添加图书

复制代码
1
2
3
4
5
6
7
8
9
10
11
/* * 初始化书架上的图书信息,添加几本书 * */ public static void init (ArrayList<Books> books){ Books book1 = new Books(101, "数据结构", 91, 100, "清华出版社"); Books book2 = new Books(102, "组成原理", 92, 200, "西北出版社"); Books book3 = new Books(103, "操作系统", 93, 300, "华大出版社"); books.add(book1); books.add(book2); books.add(book3); }

输入购买图书名称,获取图书信息;

复制代码
1
2
3
4
//用户提示 Scanner sc=new Scanner(System.in); System.out.print("请输入图书编号:"); int bookId=sc.nextInt();

输入购买数量,判断库存是否足够

        足够的话,输入信息封装为Books对象,调用FileUtil.saveBooks方法;

3、保存销售记录类

获取指定格式的日期

复制代码
1
2
Date date=new Date(); DateFormat format =new SimpleDateFormat("yyyyMMdd");

判断是否存在当日文件,来决定创建还是追加方式;

调用createFile() 写入记录;

字符串写入文件:

复制代码
1
2
3
4
5
6
7
8
9
10
//追加写入 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("D:\IDEA-file\"+fileName, true)); String str = sbf.toString(); byte[] b = str.getBytes(); out.write(b); FileOutputStream --> BufferedOutputStream --> out.write(b[i]) --> byte[] b = str.getBytes() --> String str = stringBufferStr.toString();

 

 

四、实现代码:

为什么报错???

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package cn.itcast.task01; /** * @author wangyue * @version 1.0 * @date 2019/7/1 14:42 * @describe: 图书类 */ public class Books { //内部变量 private int id; private String name; private double price; private int number; // private double totalPrice; private String press; //出版社 //构造方法 public Books() { } public Books(int id, String name, double price, int number, String press) { this.id = id; this.name = name; this.price = price; this.number = number; //this.totalPrice = totalPrice; this.press = press; } //Get and Set public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } /* public double getTotalPrice() { return totalPrice; } public void setTotalPrice(double totalPrice) { this.totalPrice = totalPrice; }*/ public String getPress() { return press; } public void setPress(String press) { this.press = press; } @Override public String toString() { return "图书编号:" + id + " 图书名称:" + name + " 出版社:" + press + " 单价:" + price + " 库存数量:" + number; } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package cn.itcast.task01; import java.util.ArrayList; import java.util.Scanner; /** * @author wangyue * @version 1.0 * @date 2019/7/1 14:42 * @describe: 销售图书实现类 */ public class SellBooks { //建立书架 public static ArrayList<Books> booksList = new ArrayList<Books>(); public static void main(String[] args) { //实例化书架 init(booksList); //把书架上所有图书信息打印出来 for (Books book : booksList) { System.out.println(book); } //销售图书 while (true) { Scanner sc = new Scanner(System.in); //用户提示 System.out.print("请输入图书编号:"); int bookId = sc.nextInt(); //获取图书库存信息 Books book = getBookInfo(bookId); if (book == null) { System.out.println("输入图书编码有误"); } else { System.out.println(book); System.out.print("请输入购买数量:"); int buyNumber = sc.nextInt(); if (buyNumber > book.getNumber()) { //数量不足 System.out.println("库存不足"); } else { //库存数量足够 //封装Books对象 Books sellBook = new Books(bookId, book.getName(), book.getPrice(), book.getNumber(), book.getPress()); //保存销售记录 FileUnit.saveRecord(sellBook); //修改库存 book.setNumber(book.getNumber() - buyNumber); System.out.println("当前的库存状况"); System.out.println(book); } } } } /* * 初始化书架上的图书信息,添加几本书 * */ public static void init(ArrayList<Books> books) { Books book1 = new Books(101, "数据结构", 91, 100, "清华出版社"); Books book2 = new Books(102, "组成原理", 92, 200, "西北出版社"); Books book3 = new Books(103, "操作系统", 93, 300, "华大出版社"); books.add(book1); books.add(book2); books.add(book3); } public static Books getBookInfo(int bookId) { for (Books thisBook : booksList) { if (thisBook.getId() == bookId) { return thisBook; } } return null; } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package cn.itcast.task01; //import org.omg.CORBA.portable.InputStream; import java.io.*; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; /** * @author wangyue * @version 1.0 * @date 2019/7/2 14:07 * @describe: 文件工具类 */ public class FileUnit { public static final String SEPERATE_FIELD = ","; //字段分割符,逗号 public static final String SEPERATE_LINE = "rn"; //行分割符 /* * 根据传进来的对象,保存一条记录 */ public static void saveRecord(Books book) { //检查今天的记录文件是否已存在 Date date = new Date(); DateFormat format = new SimpleDateFormat("yyyyMMdd"); String fileName = "销售记录" + format.format(date) + ".csv"; try { FileInputStream in = new FileInputStream("D:\IDEA-file\" + fileName); if (in != null) { //存在该文件,追加写入 createRecord(fileName, true, book); } } catch (FileNotFoundException e) { //输入流获取失败,即不存在该文件,创建新的文件 createRecord(fileName, false, book); } catch (IOException e) { e.printStackTrace(); } } /** * 将图书的销售信息保存到记录文档里,同label来判断是修改还是新建 * * @param fileName 保存的记录文件名 * @param label true:已存在只修改 false:新创建文件 * @param book 记录对象 */ public static void createRecord(String fileName, boolean label, Books book) { BufferedOutputStream out = null; StringBuffer sbf = new StringBuffer(); //拼接字符串 //多个创建输出流的地方,都需要try...catch ,可以在最外围放一个,包含所有 try { if (label) { //直接追加即可 //创建输出流,用于追加文件 !!! out = new BufferedOutputStream(new FileOutputStream("D:\IDEA-file\" + fileName, true)); } else { //不存在当天文件,新建一个 out = new BufferedOutputStream(new FileOutputStream("D:\IDEA-file\" + fileName)); //新文件需要创建表头 String[] fieldSort = new String[]{"图书编号", "图书名称", "购买数量", "单价", "总价", "出版社"}; for (String fieldKey : fieldSort) { sbf.append(fieldKey).append(SEPERATE_FIELD); } } //购买记录的主体内容 sbf.append(SEPERATE_LINE); //追加换行符 sbf.append(book.getId()).append(SEPERATE_FIELD); sbf.append(book.getName()).append(SEPERATE_FIELD); sbf.append(book.getNumber()).append(SEPERATE_FIELD); sbf.append(book.getPrice()).append(SEPERATE_FIELD); sbf.append(book.getNumber() * book.getPrice()).append(SEPERATE_FIELD); sbf.append(book.getPress()).append(SEPERATE_FIELD); //写入文件 String str = sbf.toString(); byte[] b = str.getBytes(); out.write(b); } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }

 

 

 

 

最后

以上就是迷路黄蜂最近收集整理的关于Java基础案例教程 第七章IO(输入输出)———【任务7-1】保存书店每日交易记录 (字节流)的全部内容,更多相关Java基础案例教程内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部