一:保存书店每日交易记录架构
二:项目架构
三:代码详情
1:ModelBooks类
package BookPurchasingSystem;
import java.util.Date;
/*
* 图书实体类
*/
public class ModelBooks {
//图书编号
private int id;
//图书名称
private String name;
//图书单价
private double price;
//图书数量
private int number;
//总价
private double money;
//出版社
private String Publish;
//扩展属性
private String buyingTime;
//购买数量
private String buyingNumber;
//有参构造函数
public ModelBooks(int id,String name,double price,int number,double money,String publish){
setId(id);
setName(name);
setPrice(price);
setNumber(number);
setPublish(publish);
setMoney(money);
}
//打印图书信息
public String toString(){
String message="图书编号:"+getId()+";图书名称:"+getName()+";出版社:"+getPublish()
+";单价:"+getPrice()+";库存数量:"+getNumber();
return message;
}
/*
* 属性访问器
*/
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 int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String getPublish() {
return Publish;
}
public void setPublish(String publish) {
Publish = publish;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBuyingTime() {
return buyingTime;
}
public void setBuyingTime(String buyingTime) {
this.buyingTime = buyingTime;
}
public String getBuyingNumber() {
return buyingNumber;
}
public void setBuyingNumber(String buyingNumber) {
this.buyingNumber = buyingNumber;
}
}
2:BookManager类
package BookPurchasingSystem;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import projectActualCombat.Books;
public class BookManager {
//字段分隔英文逗号
public static final String SEPARATE_FIELD=",";
//行分割
public static final String SEPARATE_LINE="\r\n";
/*
*保存购买图书信息
* Books books:图书信息
* String filePath:保存地址
* String fileSuffix:保存文件的格式
* 返回值:保存情况
*/
public static boolean saveBooks(ModelBooks books,String filePath,String fileSuffix,String buyingNumber){
//获取当前时间
Date date=new Date();
//定义日期格式为yyyyMMdd
DateFormat format=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
//设置购买时间
books.setBuyingTime(format.format(date));
books.setBuyingNumber(buyingNumber);
//拼接文件名
String name=filePath+"销售记录"+"."+fileSuffix;
//创建输入流:读取文件信息
InputStream inputStream=null;
try {
//判断本地是否存在此文件,返回null表示文件不存在,不为null表示存在
inputStream=new FileInputStream(name);
if (inputStream!=null) {
////关闭流
inputStream.close();
//可获取输入流,则存在文件,采取追加文件的方式
if(!createFile(name,false,books)){
return false;
}else {
return true;
}
}
return true;
} catch (FileNotFoundException e) {
//输入流获取失败,则不存在文件,采取新建文件方式
if(!createFile(name,false,books)){
return false;
}else{
return true;
}
}catch (IOException e) {
e.printStackTrace();
return false;
}
}
/*
* 创建文件
* String name:文件名
* boolean b:文件是否存在:true:存在,false:不存在
* Books books:图书对象
*/
private static boolean createFile(String name, boolean fileExit, ModelBooks books) {
//创建BufferedOutputStream对象
BufferedOutputStream out=null;
//创建StringBuffer对象,用于拼接文本内容
StringBuffer stringBuffer=new StringBuffer();
try {
//判断文件是否存在
if (fileExit) {
//当已存在当前文件,则文件内容后追加
//创建输出流,用于追加文件
out=new BufferedOutputStream(new FileOutputStream(name,true));
////写入内容
WriteStr(stringBuffer,books,out);
}else{
//当前文件不存在,添加内容
//创建输出流,,用于保存文件
out=new BufferedOutputStream(new FileOutputStream(name));
//定义文件标题属性
String[]fileSort=new String[]{
"图书编号","图书名称","购买数量","单价","总价","出版社,购买时间"
};
//将标题属性,添加到stringBuffer中去,并用SEPARATE_FIELD字段内容进行隔开
for(String fileKey:fileSort){
//新建时,将表头存入本地文件
stringBuffer.append(fileKey).append(SEPARATE_FIELD);
}
////写入内容
WriteStr(stringBuffer,books,out);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
//文件的释放在finally中进行
finally {
//判读该输出流释放被使用,被使用关闭该输出流
if (out!=null) {
try {
//关闭输出流
out.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
}
//写入内容
private static void WriteStr(StringBuffer stringBuffer,ModelBooks books,BufferedOutputStream out) throws IOException{
//追加换行符号
stringBuffer.append(SEPARATE_LINE);
//添加图书信息到stringBuffer中去
stringBuffer.append(books.getId()).append(SEPARATE_FIELD);
stringBuffer.append(books.getName()).append(SEPARATE_FIELD);
stringBuffer.append(books.getBuyingNumber()).append(SEPARATE_FIELD);
stringBuffer.append((double)books.getPrice()).append(SEPARATE_FIELD);
stringBuffer.append((double)books.getMoney()).append(SEPARATE_FIELD);
stringBuffer.append(books.getPublish()).append(SEPARATE_FIELD);
stringBuffer.append(books.getBuyingTime()).append(SEPARATE_FIELD);
//获取stringBuffer中所有字符
String str=stringBuffer.toString();
//将字符转换为byte字节
byte[]by=str.getBytes();
//通过字节方式进行内容的写入
for(int i=0;i<by.length;i++){
//将内容写入到本地
out.write(by[i]);
}
}
}
3:Test类
package BookPurchasingSystem;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
static ArrayList<ModelBooks>bookList=new ArrayList<>();//创建书架
public static void main(String[] args) {
//初始化书架
Init();
//打印书架清单
//将书架上所有的图书信息打印出来
PrintBooksMessage();
//用户购买图书
UserPurchaseBooks();
}
/*
* 打印书架图书信息
*/
private static void PrintBooksMessage() {
// TODO Auto-generated method stub
for(int i=0;i<bookList.size();i++){
System.out.println(bookList.get(i));
}
}
/*
* 用户购买图书
*/
private static void UserPurchaseBooks() {
while(true){
//获取控制台输入信息
Scanner scan=new Scanner(System.in);
System.out.print("请输入图书编号:");
int bookid=scan.nextInt();
//根据输入的图书编号获取图书信息
ModelBooks stockBooks=getBooksById(bookid);
//判断是否存在存在此图书
if (stockBooks!=null) {
System.out.println("当前图书信息:"+stockBooks);
System.out.print("请输入购买数量:");
int bookNumber=scan.nextInt();
//判断库存是否足够
if (bookNumber<=stockBooks.getNumber()) {
//将购买数量加入到对象中去
stockBooks.setBuyingNumber(Integer.toString(bookNumber));
//将输入信息封装成Books对象
ModelBooks books=new ModelBooks(stockBooks.getId(), stockBooks.getName(), stockBooks.getPrice()*bookNumber,
stockBooks.getNumber(), stockBooks.getMoney(),stockBooks.getPublish());
if (BookManager.saveBooks(books, "C:\\Users\\Administrator\\Desktop\\", "csv",stockBooks.getBuyingNumber())) {
//修改库存
stockBooks.setNumber(stockBooks.getNumber()-bookNumber);
System.out.println("购买成功!");
}else{
System.out.println("购买失败!");
}
}else{
System.out.println("库存不足!");
}
}else {
System.out.println("图书编号输入错误!");
}
}
}
/*
* 根据输入的图书编号查找图书信息,循环遍历书架中的图书信息,找到图书编号相等的取出
*/
private static ModelBooks getBooksById(int bookid) {
// TODO Auto-generated method stub
for (int i = 0; i < bookList.size(); i++) {
ModelBooks thisBooks=bookList.get(i);
if (bookid==thisBooks.getId()) {
return thisBooks;
}
}
return null;
}
/*
* 初始化书架上的图书信息,将图书放到书架上
*/
private static void Init() {
ModelBooks goods1=new ModelBooks(101, "Java基础入门", 44.5, 100, 4450.00,"清华大学出版社");
ModelBooks goods2=new ModelBooks(102, "Java编程思想", 108.00, 50, 5400.00,"机械工业出版社");
ModelBooks goods3=new ModelBooks(103, "Java基础入门", 99.00, 100, 9900.00,"电子工业出版社");
bookList.add(goods1);
bookList.add(goods2);
bookList.add(goods3);
}
}