基于JAVA对象流写的图书进销存系统管理

基于JAVA对象流写的图书进销存系统管理

1、用户登录
用户只需一个,即admin管理员,用户密码为123456,用户登录信息保存在属性文件user. properties中,用户登录时输入用户名和密码,如果成功则打印系统菜单,不成功则重新登录,不成功的次数最多3次,超过3次,则系统退出。
(在内存中一行一行读取出来,对字符串进行修改,然后覆盖)
2、系统菜单:1、库存管理 2、销售管理 3、查询库中所有图书信息
4、查询销售信息、5,退出
3、选择不同的菜单,进入不同的管理功能。1、库存管理 : 使用bookStore.txt文件作为图书数据文件,
进入库存管理后只能进行图书入库操作和退出,既向文件中添加图书信息,如果图书名称在文件中已经存在,则再录入此书的时候,需要在原有的图书的库存基础上添加数据。如果没有此书,则作为新的数据录入。
如果录入信息为: Java基础,则下一步只需要录入进货数量即可。
如果录入信息为:HTML,此书没有,则需要进行详细录入,下一步录入作者名称、进价、售价、库存等信息。
4、销售管理:使用sell.txt文件作为图书销售信息保存,
卖出去之后 用新的集合对象(盈利)

提示:第一次运行可能会抛出异常,因为第一次写入的时候会读到文件末尾流!!!后面运行一切正常!
第一个文件Book.java

package com.langxikeji.Object;
import java.io.Serializable;
//创建一个Book类,并且实现Serializable接口
public class Book implements Serializable{
    private int bookId;
    private String bookName;
    private String auther;
    private double purprice;
    private double sellprice;
    private int stock;
    private int salecount;
    private double profit;
    public int getBookId() {
        return bookId;
    }
    public void setBookId(int bookId) {
        this.bookId = bookId;
    }
    public String getBookName() {
        return bookName;
    }
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
    public String getAuther() {
        return auther;
    }
    public void setAuther(String auther) {
        this.auther = auther;
    }
    public double getPurprice() {
        return purprice;
    }
    public void setPurprice(double purprice) {
        this.purprice = purprice;
    }
    public double getSellprice() {
        return sellprice;
    }
    public void setSellprice(double sellprice) {
        this.sellprice = sellprice;
    }
    public int getStock() {
        return stock;
    }
    public void setStock(int stock) {
        this.stock = stock;
    }
    public int getSalecount() {
        return salecount;
    }
    public void setSalecount(int salecount) {
        this.salecount = salecount;
    }
    public double getProfit() {
        return profit;
    }
    public void setProfit(double profit) {
        this.profit = profit;
    }
  //读取和写入sell.txt文件
    public Book(int bookId, String bookName, String auther, double purprice,
            double sellprice, int salecount, double profit) {
        super();
        this.bookId = bookId;
        this.bookName = bookName;
        this.auther = auther;
        this.purprice = purprice;
        this.sellprice = sellprice;
        this.salecount = salecount;
        this.profit = profit;
    }
    //读取和写入bookStore.txt文件
    public Book(int bookId, String bookName, String auther, double purprice,
            double sellprice, int stock) {
        super();
        this.bookId = bookId;
        this.bookName = bookName;
        this.auther = auther;
        this.purprice = purprice;
        this.sellprice = sellprice;
        this.stock = stock;
    }
    //空的构造方法,初始化变量
    public Book() {
        // TODO Auto-generated constructor stub
    }
}

第二个文件Booksadd.java
使用对象流和集合输入和输出增加库存信息

package com.langxikeji.Object;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class BooksAdd {
    //保存bookStore.txt文件里面的对象
      public  List<Book>booklist=new ArrayList<Book>();
     //无参的构造方法,Book对象初始化
     public BooksAdd(){
      readFile();
         if(booklist.size()==0){
         Book book=new Book(1,"JAVA基础","李刚",36.80,45.50,50);
            insert(book);
         book=new Book(2,"JAVAEE编程","李刚",25.50,36.78,100);
            insert(book);
         }
     }
     //往booklist中传入对象,并且写入bookStore.txt文件中
     public void insert(Book book){
             booklist.add(book);
             writeFile();
         }

     public List<Book> totalBooks(){
         return booklist; 
     }

   //对象输出流
     public void writeFile(){
         String name="C:\\Users\\lenovo\\Desktop\\bookStore.txt";
         try(FileOutputStream ops=new FileOutputStream(name);
                ObjectOutputStream oos=new ObjectOutputStream(ops);){
                oos.writeObject(booklist); 
                oos.flush();
                System.out.println("数据保存成功!");
         }catch(Exception e){
             e.printStackTrace();
         }
     }
     //对象输入流
     public void readFile(){
         String name="C:\\Users\\lenovo\\Desktop\\bookStore.txt";
         try(FileInputStream ips=new FileInputStream(name);
                 ObjectInputStream ois=new ObjectInputStream(ips)){
                 booklist=(List<Book>)ois.readObject();
         }catch(Exception e){
             e.printStackTrace();
         }
     }
}

第三个文件Bookssell.java
输入和输出销售信息

package com.langxikeji.Object;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class BooksSell {
    //保存sell.txt文件里面的对象
    public  List<Book>selllist=new ArrayList<Book>();
    public BooksSell(){
        readFilesell();
        if(selllist.size()==0){
            Book book=new Book(1,"JAVA基础","李刚",36.80,45.50,0,0.0);
            insertsell(book);
        }
    }
  //往insertsell中传入对象,并且写入sell.txt文件中
    public void insertsell(Book book){
     selllist.add(book);
     writeFilesell();
    }
    public List<Book> sellBooks(){
     return selllist;
    }
    public void readFilesell(){
     String name="C:\\Users\\lenovo\\Desktop\\sell.txt";
     File file=new File(name);
     try(InputStream ips=new FileInputStream(file);
             ObjectInputStream ois=new ObjectInputStream(ips)){
         selllist=(List<Book>)ois.readObject();
     }catch(Exception e){
         e.printStackTrace();
     }
    }
    //对象输出流
    public void writeFilesell(){
     String name="C:\\Users\\lenovo\\Desktop\\sell.txt";
     File file=new File(name);
     try(OutputStream ops=new FileOutputStream(file);
            ObjectOutputStream oos=new ObjectOutputStream(ops);){
            oos.writeObject(selllist); 
            oos.flush();
            System.out.println("数据保存成功!");
     }catch(Exception e){
         e.printStackTrace();
     }
    }
}

第四个文件,
登录程序,主程序

package com.langxikeji.Object;

import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Booksmanagement {
        //新建一个BooksIO对象
        BooksAdd bio=new BooksAdd();
        BooksSell bis=new BooksSell();
    public void welcome(){
         System.out.println("***********************");
         System.out.println("*******|-------|*******");
         System.out.println("*******|welcome|*******");
         System.out.println("*******|-------|*******");
         System.out.println("***********************");
         System.out.println("提示:用户名格式为5-10位字母,密码格式为6位数字!");
    }
    public void Login(){
        if(Logininfo()){
                System.out.println("*****欢迎登陆图书进销存管理系统*****");
                adminLogin();
        }else{
            System.out.println("登录失败,系统退出");
            System.exit(0); 
        }
        }
    public void adminLogin(){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入数字选择你需要的功能:");
        System.out.println("1,库存管理。2,销售管理。3,查询书库中的所有书籍。4,查询销售记录5,系统退出");
        switch(sc.nextInt()){
        case 1:
            Inmanagement();
            break;
        case 2:
            Outmanagement();
            break;
        case 3:
            polling();
            break;
        case 4:
            sellpolling();
            break;
        case 5:
            System.out.println("系统退出!");
            System.exit(0);
            break;
        default:
            System.out.println("输入错误,请从新输入!");
            break;
        }
    }
    //查询书库中所有的书籍信息
    public void polling(){
        System.out.println("序号\t\t书名\t\t作者\t\t进价\t\t售价\t\t库存\t\t");
        List<Book> booklist=bio.totalBooks() ;
        for(Book book:booklist){
            System.out.print(book.getBookId()+"\t\t");
            System.out.print(book.getBookName()+"\t\t");
            System.out.print(book.getAuther()+"\t\t");
            System.out.print(book.getPurprice()+"\t\t");
            System.out.print(book.getSellprice()+"\t\t");
            System.out.print(book.getStock()+"\n");
        } 
        adminLogin();
    }
    //输出销售信息
    public void sellpolling(){
        System.out.println("序号\t\t书名\t\t作者\t\t进价\t\t售价\t\t销售数量\t\t盈利\t\t");
        List<Book>selllist=bis.sellBooks();
        for(Book book:selllist){
            System.out.print(book.getBookId()+"\t\t");
            System.out.print(book.getBookName()+"\t\t");
            System.out.print(book.getAuther()+"\t\t");
            System.out.print(book.getPurprice()+"\t\t");
            System.out.print(book.getSellprice()+"\t\t");
            System.out.print(book.getSalecount()+"\t\t");
            System.out.print(book.getProfit()+"\n");
        }
        adminLogin();
    }
    //库存管理模块
    public void Inmanagement(){
        Scanner sc=new Scanner(System.in);
        System.out.println("请按照提示输入选择以下功能:");
        System.out.println("1,图书入库。2,退出");
        switch(sc.nextInt()){
        case 1:
            jionBook();
            break;
        case 2:
            adminLogin();
            break;
        default:
            System.out.println("输入错误,请从新输入!");
            break;
        }
    }
    public void jionBook(){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要入库书籍的名字:");
        String bookname=sc.nextLine();
        //创建一个对象
        List<Book>booklist=bio.totalBooks();
        Book book = new Book();
        //遍历booklist数组
     for(int i=0;i<booklist.size();i++){
         book=booklist.get(i);
         //判断书库中是否存在该书
         if(book.getBookName().equals(bookname)){
                System.out.println("库中已经存在该书!");
                System.out.println("请输入入库库存数量:");
                int store=sc.nextInt();
                store+=book.getStock();
                book.setStock(store);
                bio.writeFile();
                adminLogin(); 
         }else{
             System.out.println("书库中该书不存在!");
             System.out.println("请输入增加图书信息:");
             System.out.println("请输入书籍序号:");
             int m=sc.nextInt();
             System.out.println("请输入书名:");
             String a=sc.next();
             System.out.println("请输入作者:");
             String b=sc.next();
             System.out.println("请输入书籍进价:");
             double c=sc.nextDouble();
             System.out.println("请输入书籍售价:");
             double d=sc.nextDouble();
             System.out.println("请输入库存");
             int e=sc.nextInt();
             Book book1=new Book(m,a,b,c,d,e);
             bio.insert(book1); 
             adminLogin();
         }
     }
    }
    //销售管理
    public void Outmanagement(){
        Scanner sc=new Scanner(System.in);
        System.out.println("请按照提示输入选择以下功能:");
        System.out.println("1,图书销售。2,退出");
        switch(sc.nextInt()){
        case 1:
            outBook();
            break;
        case 2:
            adminLogin();
            break;
        default:
            System.out.println("输入错误,请从新输入!");
            break;
        }
    }
    //购买书籍改变bookStore中的库存
    public void outBook(){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要购买书籍的名字:");
        String bookname=sc.nextLine();
        //创建一个对象
        Book book = new Book();
        List<Book>booklist=bio.totalBooks();
        //遍历booklist数组
     for(int i=0;i<booklist.size();i++){
         book=booklist.get(i);
         //判断书库中是否存在该书
         if(book.getBookName().equals(bookname)){
             System.out.println("书库中存在该书!"); 
             System.out.println("请输入要购买的数量:");
             int num=sc.nextInt();
             if(num>book.getStock()){
                 System.out.println("购买数量超过库存!");
             }else{
                 System.out.println("购买成功!");
                 //剩余库存
                 int sum=book.getStock()-num;
                 //改变原有库存
                    book.setStock(sum);
                    //写入bookStore.txt文件中
                    bio.writeFile();
                    //传入到下一个方法中
                    Outsell(bookname,num);
                    }
                    adminLogin();
             }else{
                 System.out.println("书库中不存在该书,返回主菜单!");
                 adminLogin();
             }
         }
    }
    //链接OutBook方法 改变sell.txt文件里面的值
    public void Outsell(String bookname,int numbook){
        Scanner sc=new Scanner(System.in);
        Book book=new Book();
        List<Book>selllist=bis.sellBooks();
         BigDecimal num=new BigDecimal(numbook);
        for(int j=0;j<selllist.size();j++){
            book=selllist.get(j);
             if(book.getBookName().equals(bookname)){
                 //改变售卖数量
                 int salecount=book.getSalecount()+numbook;
                 book.setSalecount(salecount);
                 //改变售卖利润,处理精度丢失问题
                 //进价转型
                 BigDecimal purprice=new BigDecimal(book.getPurprice());
                 //售价转型
                 BigDecimal sellprice=new BigDecimal(book.getSellprice());
                 //原有的利润转型
                 double oldprofit=book.getProfit();
                 BigDecimal oprofit=new BigDecimal(oldprofit);
                 //进行计算
                 double bigprofit=(((sellprice.subtract(purprice)).multiply(num)).add(oprofit)).doubleValue();
                 book.setProfit(bigprofit);
                 bis.writeFilesell(); 
             }else{
                 BigDecimal purprice=new BigDecimal(book.getPurprice());
                 //售价转型
                 BigDecimal sellprice=new BigDecimal(book.getSellprice());
                 //进行计算
                 double bigprofit=((sellprice.subtract(purprice)).multiply(num)).doubleValue();
                 Book book3=new Book(book.getBookId(),book.getBookName(),book.getAuther(),book.getPurprice(),book.getSellprice(),numbook,bigprofit);
                 bis.insertsell(book3);

            }
        }
    }
    //系统登录模块
    public static boolean Logininfo(){
        try{
        String adress="D:\\JAVA\\Workspaces\\sina\\src\\user.properties";
        File file=new File(adress);
        Properties p=new Properties();
        //使用输入流拿到p的key-value对
        p.load(new FileInputStream(file));
            //控制三次循环
            for(int i=3;i>0;i--){
            //控制台输入用户名和密码
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入登录用户名:");
            String name=sc.nextLine();
            System.out.println("请输入登录密码:");
            String password=sc.nextLine();
            //判断输入格式是否正确和密码是否正确
            if(judge(name,password)&&p.containsKey(name)&&p.containsValue(password)){   
                System.out.println("登录用户名和密码正确,欢迎您!");
                   return true;
            }else if(i>1){
                System.out.println("警告:用户名或密码错误,今日剩余输入次数为"+(i-1)+"次!");
                System.out.println("---------------------------");
            }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
            return false;
    }
    //判断输入用户名和密码格式是否正确
    public static boolean judge(String name,String password){
        Pattern pattern1 =Pattern.compile("^[A-Za-z]{5,10}$");
        Matcher m1=pattern1.matcher(name);
        Pattern pattern2=Pattern.compile("^[0-9]{6}$");
        Matcher m2=pattern2.matcher(password);
        if(!m1.matches()||!m2.matches()){
            System.out.println("---------------------------");
            System.out.println("提示:用户名和密码输入格式不正确!");
            System.out.println("提示:用户名格式为5-10位字母,密码格式为6位数字!");
            System.out.println("-----------------------------");
            return false;
        }else{
            return true;
        }
    }
    public static void main(String[] args)  throws Exception{
        Booksmanagement bs=new Booksmanagement();
        bs.welcome();
        bs.Login();
    }
}
展开阅读全文

没有更多推荐了,返回首页