package Bookstore;
public class books {
int id;
String name;
double price;
int number;
double money;
String publish;
public books(int id,String name,double price,int number,double money,String publish){
this.id = id;
this.money = money;
this.name = name;
this.number = number;
this.price = price;
this.publish = publish;
}
@Override
public String toString() {
String message = "编号:"+id+"书名:"+name+"出版社"+publish+"单价"+price+"数量"+number+"总价"+money;
return message;
}
public void setNumber(int number) {
this.number = number;
}
}
package Bookstore;
import java.util.ArrayList;
import java.util.Scanner;
public class recordbooksorder {
static ArrayList<books> bookslist = new ArrayList<books>();
public static void main(String[] args) {
init();
for (int i = 0; i < bookslist.size(); i++) {
System.out.println(bookslist.get(i));
}
while (true){
Scanner sc = new Scanner(System.in);
System.out.println("请输入图书编号");
int bookid = sc.nextInt();
books stock = getbooksbyid(bookid);
if (stock != null){
System.out.println(stock);
System.out.println("请输入购买数量");
int booknumber = sc.nextInt();
if (booknumber < stock.number){
books books = new books(stock.id,stock.name,stock.price,stock.number,stock.money,stock.publish);
fileutil.savebooks(books);
stock.setNumber(stock.number - booknumber);
}else {
System.out.println("库存不足");
}
}else {
System.out.println("编号输入错误");
}
}
}
private static books getbooksbyid(int bookid) {
for (int i = 0; i < bookslist.size(); i++) {
books thisbook = bookslist.get(i);
if (bookid == thisbook.id){
return thisbook;
}
}
return null;
}
private static void init() {
books g1 = new books(101,"Java",11.1,10,111,"java出版社");
books g2 = new books(102,"Android",11.1,20,222,"Android出版社");
books g3 = new books(103,"Java",110.00,10,1100,"python出版社");
bookslist.add(g1);
bookslist.add(g2);
bookslist.add(g3);
}
}
package Bookstore;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class fileutil {
public static final String field = ",";
public static final String line = "\r\n";
public static void savebooks(books books){
Date d = new Date();
DateFormat format = new SimpleDateFormat("YYYYMMDD");
String name = "销售记录"+format.format(d) + ".csv";
InputStream in = null;
try {
in = new FileInputStream(name);
if (in != null){
in.close();
creatfile(name,true,books);
}
} catch (FileNotFoundException e) {
creatfile(name,false,books);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void creatfile(String name, boolean b, books books) {
BufferedOutputStream out = null;
StringBuffer sbf = new StringBuffer();
try {
if (b){
out = new BufferedOutputStream(new FileOutputStream(name,true));
}else {
out = new BufferedOutputStream(new FileOutputStream(name));
String[] fieldsort = new String[]{"图书编号","图书名称","购买数量","单价","总价","出版社"};
for (String fieldkye: fieldsort
) {
sbf.append(fieldkye).append(field);
}
}
sbf.append(line);
sbf.append(books.id).append(field);
sbf.append(books.name).append(field);
sbf.append(books.number).append(field);
sbf.append((double) books.price).append(field);
sbf.append((double) books.money).append(field);
sbf.append(books.publish).append(field);
String str = sbf.toString();
byte[] by = str.getBytes();
for (int i = 0; i < by.length; i++) {
out.write(by[i]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
}catch (Exception e2){
e2.printStackTrace();
}
}
}
}