程序编写:编写一个保存书店每日交易记录的程序,使用字符流将书店的交易信息记录在本地的csv或txt文件中。当用户输入图书编号时,后台会根据图书编号查询到相应图书信息,并返回打印出来。用户紧接着输入购买数量,系统会判定库存是否充足,如果充足则将信息保存至本地的csv文件中,其中每条记录包含“图书编号”、“图书名称”、“购买数量”、“单价”、“总价”和“出版社”等数据。每个数据之间用英文逗号或空格分隔,每条数据之间由换行符分隔。保存的时候需要判断本地是否存在当天的数据,如果存在则追加,不存在则新建。文件命名格式为“销售记录”加上当天日期加上“.csv”或“.txt”后缀,如“销售记录20200426.csv”或“销售记录20200426.csv”。
package com.pdsu.rjxy;
public class Book_122 {
int ID,number;//数量,ID
String Name;//名字
double price,money;//单价,总价
String Appear;//出版社
public Book_122(int iD, int number, String name, double price, double money, String appear) {
this.ID = iD;
this.number = number;
this.Name = name;
this.price = price;
this.money = money;
this.Appear = appear;
}
@Override
public String toString() {
return "图书编号=" + ID + ", 图书名字=" + Name +", 图书数量=" + number + ", 图书价格=" + price
+ ", 出版社=" + Appear;
}
public void setnumber(int number) {
this.number=number;
}
}
package com.pdsu.rjxy;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class File_book {
public static final String SEPARATE_FIELD = ",";
public static final String SEPARATE_LINE = "\r\n";
static void judge_file(Book_122 book) {
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyyMMdd");
String file_name = "销售记录" + format.format(date)+".txt";
File file = new File(file_name);
if(file.exists()) {
create(file_name,true,book);//当天销售记录已经存在
}
else {
create(file_name, false, book);//当天销售记录不存在
}
}
static void create(String name,boolean flag,Book_122 book) {
BufferedOutputStream out = null;
StringBuffer f = new StringBuffer();
try {
if(flag) {
out = new BufferedOutputStream(new FileOutputStream(name,true));
}
else {
out = new BufferedOutputStream(new FileOutputStream(name));
String [] s = new String [] {"图书编号","图书名称","图书数量",
"图书单价","图书总价","出版社"};
for(String it : s) {
f.append(it + SEPARATE_FIELD);
}
}
f.append(SEPARATE_LINE);
f.append(book.ID + SEPARATE_FIELD);
f.append(book.Name + SEPARATE_FIELD);
f.append(book.number + SEPARATE_FIELD);
f.append((double)book.price +SEPARATE_FIELD);
f.append((double)(double)book.money + SEPARATE_FIELD);
f.append(book.Appear + SEPARATE_FIELD);
String str = f.toString();
byte[] by = str.getBytes();
for(int i=0;i<by.length;i++) {
out.write(by[i]);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally {
try {
if(out!=null) {
out.close();
}
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
}
3.package com.pdsu.rjxy;
import java.util.ArrayList;
import java.util.Scanner;
public class test_122 {
static ArrayList<Book_122> bookslist = new ArrayList<Book_122>();
public static void main(String[] args) {
//初始化,导入书籍
init();
for(int i=0;i<bookslist.size();i++) {
System.out.println(bookslist.get(i));
}
Scanner reader = new Scanner(System.in);
while(true) {
System.out.println("请输入图书的编号 :");
int id = reader.nextInt();
Book_122 b= getBook(id);
if(b!=null) {
System.out.println("当前图书信息:");
System.out.println(b);
System.out.println("请输入要购买的数量:");
int buy = reader.nextInt();
if(buy>b.number) {
System.out.println("抱歉,您购买该书数量超出我们库存的数量");
}
else {
Book_122 c = new Book_122(b.ID,buy, b.Name, b.price, b.price*buy, b.Appear);
//将数据转向本地文件。
File_book.judge_file(c);
b.setnumber(b.number-buy);
}
}
else {
System.out.println("图书ID输入错误");
}
}
}
static void init() {
Book_122 book1= new Book_122(1, 100, "Java2实用教程", 49.9, 4990, "清华出版社");
Book_122 book2= new Book_122(2, 100, "c语言基础", 39.9, 3990, "清华出版社");
Book_122 book3= new Book_122(3, 100, "算法基础", 109.9, 10990, "清华出版社");
Book_122 book4= new Book_122(4, 100, "高等数学", 29.9, 2990, "平顶山出版社");
bookslist.add(book1);
bookslist.add(book2);
bookslist.add(book3);
bookslist.add(book4);
}
static Book_122 getBook(int id) {//查找该书是否存在
for(int i=0;i<bookslist.size();i++) {
Book_122 a = bookslist.get(i);
if(id==a.ID) {
return a;
}
}
return null;
}
}