通过文件流来纪录每日的销售数量
效果图:
代码:
package enptity;
/**
* 图书类
* @author Geek
* @version 1.0
* @date 2018年6月28日 下午4:55:50
* @TextDemo Books
* @copyright
* @remark
*/
public class Book {
private String id;
private String name;
private String publish;
private double price;
private int count;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Book() {
}
public Book(String id,String name,String publish,double price,int count) {
setId(id);
setName(name);
setPublish(publish);
setPrice(price);
setCount(count);
}
@Override
public String toString() {
return "当前图书信息图书编号: "+getId()+"\t"+"图书名称: "+getName()+"\t"+"出版社: "+getPublish()+"\t"+"单价: "+getPrice()+"\t"+"库存数量: "+getCount();
}
}
package file;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import enptity.Book;
/**
* 存储图书类
* @author Geek
* @version 1.0
* @date 2018年6月28日 下午4:56:08
* @TextDemo Books
* @copyright
* @remark
*/
public class FileBooks {
private String[] rows = new String[] { "图书编号", "图书名称", "购买数量", "单价", "总价", "出版社" };
private static final String DH = ",";
private static final String HH = System.getProperty("line.separator");
public String getData() {
Calendar calendar = new GregorianCalendar();
StringBuffer data = new StringBuffer();
data.append(calendar.get(Calendar.YEAR));
data.append(calendar.get(Calendar.MONTH) + 1);
data.append(calendar.get(Calendar.DATE));
return data.toString();
}
/**
* 判断今天的销售记录是否已经创建
*
* @return
*/
public boolean isExitByDate() {
File file = new File("src/" + "销售记录" + getData() + ".csv");
if (file.exists()) {
return true;
}
return false;
}
/**
* 通过日期来存贮每日的销售记录
*
* @throws IOException
*/
public void saveBookByDate(Book book, int num) throws IOException {
BufferedWriter bufferedWriter = null;
StringBuffer content = null;
if (!isExitByDate()) {
content = new StringBuffer();
for (int i = 0; i < this.rows.length; i++) {
if (i != this.rows.length - 1)
content.append(this.rows[i] + DH);
else
content.append(this.rows[i] + HH);
}
}
bufferedWriter = new BufferedWriter(new FileWriter(new File("src/" + "销售记录" + getData() + ".csv"), true));
if(content!=null) {
bufferedWriter.write(content.toString());
}
bufferedWriter.write(book.getId() + DH);
bufferedWriter.write(book.getName() + DH);
bufferedWriter.write(num + DH);
bufferedWriter.write(book.getPrice() + DH);
bufferedWriter.write(book.getPrice() * num + DH);
bufferedWriter.write(book.getPublish() + HH);
bufferedWriter.close();
}
}
package mange;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import enptity.Book;
import file.FileBooks;
/**
* 服务类-封装了买书,显示图书信息等核心方法
* @author Geek
* @version 1.0
* @date 2018年6月28日 下午4:56:45
* @TextDemo Books
* @copyright
* @remark
*/
public class Mange {
FileBooks fileBooks = new FileBooks();
public List<Book> books;
/**
* 初始化系统的书本
*/
public void initBook() {
List<Book> books = new ArrayList<>();
Book book = new Book("101", "Java基础入门", "清华大学出版社", 44.5, 100);
Book book1 = new Book("102", "Java编程思想", "机械工业出版社", 108.0, 50);
Book book2 = new Book("103", "疯狂Java讲义", "电子工业出版社", 99.0, 100);
books.add(book);
books.add(book1);
books.add(book2);
this.books = books;
}
/**
* 通过图书编号来显示图书信息
*
* @param id
* 图书编号
*/
public int showBookInfo(String id) {
int i;
for (i = 0; i < this.books.size(); i++) {
if (id.equals(books.get(i).getId())) {
System.out.println(books.get(i));
return i;
}
}
if (i == this.books.size()) {
System.out.println("图书编号输入错误!");
}
return -1;
}
/**
* 买书
*/
public void buyBook(int i, int num) {
Book book = this.books.get(i);
if (num <= book.getCount()) {
book.setCount(book.getCount() - num);
this.books.set(i, book);
System.out.println("购买成功~");
try {
fileBooks.saveBookByDate(book, num);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("库存不足!");
}
}
public void start() {
for (int i = 0; i < this.books.size(); i++) {
System.out.println(books.get(i));
}
Scanner in = new Scanner(System.in);
while (true) {
System.out.print("请输入图书编号: ");
String id = in.next();
int i = showBookInfo(id);
if (i != -1) {
System.out.print("请输入购买数量: ");
int num = in.nextInt();
if (num <= 0) {
System.out.println("购买数量需大于0");
} else {
buyBook(i, num);
}
}
}
}
public Mange() {
initBook();
}
}
package text;
import java.io.IOException;
import enptity.Book;
import file.FileBooks;
import mange.Mange;
/**
* 测试
* @author Geek
* @version 1.0
* @date 2018年6月28日 下午4:57:59
* @TextDemo Books
* @copyright
* @remark
*/
public class Room {
public static void main(String[] args) {
Mange mange = new Mange();
mange.start();
}
}
最终效果图:
一:
二:
其实还可以连上数据库来进行交互,由于时间关系就不做了,有其他的好提议可以留言,欢迎转载分享