一:保存書店每日交易記錄架構
二:項目架構
三:代碼詳情
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);
}
}
四:運行效果