1. 程式人生 > >JAVA圖書館庫存管理系統程式程式碼(管理系統+使用者購買結賬系統)

JAVA圖書館庫存管理系統程式程式碼(管理系統+使用者購買結賬系統)

JAVA圖書館庫存管理系統(管理系統+使用者購買結賬系統)

package Library;

import java.io.Serializable;

public class Book implements Serializable{

	private int bookId;
	private String bookName;
	private String auther;
	private String pubDate;
	private double price;
	private int store;
	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 String getPubDate() {
		return pubDate;
	}
	public void setPubDate(String pubDate) {
		this.pubDate = pubDate;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getStore() {
		return store;
	}
	public void setStore(int store) {
		this.store = store;
	}
	public Book(int bookId, String bookName, String auther, String pubDate,
			double price, int store) {
		super();
		this.bookId = bookId;
		this.bookName = bookName;
		this.auther = auther;
		this.pubDate = pubDate;
		this.price = price;
		this.store = store;
	}
	public Book() {
		// TODO Auto-generated constructor stub
	}
	

}</strong></span><span style="font-size:18px;font-weight: bold;">
</span>

package Library;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class BookBiz {
	private List<Book> bookList =new ArrayList<Book>();
	
	public BookBiz(){
		readFile();
		if(bookList.size()==0){
			Book book=new Book(10001,"鬼吹燈","天下霸唱","2006-1-1",34,3);
			insertBook(book);
			 book=new Book(10002,"魔戒","莫爾斯","2006-3-1",334,300);
			insertBook(book);
			 book=new Book(10003,"哈利波特","羅琳","2006-1-1",34,3);
			insertBook(book);
			 book=new Book(10004,"時間機器人","威爾","2006-3-1",90,30);
			insertBook(book);
			 book=new Book(10005,"宇宙奧祕","霍金","2006-1-1",89,79);
			insertBook(book);
		}
	}
	
	public void insertBook(Book book){
		bookList.add(book);
		saveFile();
	}
	public void deleteBook(int id){
		for(int i=0;i<bookList.size();i++){
			Book book =bookList.get(i);
			if(book.getBookId()==1){
				bookList.remove(i);
				saveFile();
				return;
			}
		}
	}
	public void updateBook(Book book){
		for(int i=0;i<bookList.size();i++){
			Book bk=bookList.get(i);
			if(bk.getBookId()==book.getBookId()){
				bk.setAuther(book.getAuther());
				bk.setBookId(book.getBookId());
				bk.setPrice(book.getPrice());
				bk.setStore(book.getStore());
				bk.setPubDate(book.getPubDate());
				saveFile();
				return;
				
			}
		}
	}
	public List<Book> selectBooks(){
		
		return bookList;	
	}
	public Book selectBook(int id ){
		
		Book book=new Book();
		for (int i = 0; i < bookList.size(); i++) {
			book=bookList.get(i);
			if(book.getBookId()==id){
				return book;
			}
		}
		return null;
	}
public Book selectBook2(int id,int store ){
		
		Book book=new Book();
		for (int i = 0; i < bookList.size(); i++) {
			book=bookList.get(i);
			if(book.getBookId()==id){
				int st=book.getStore()-store;
				book.setStore(st);
				return book;
			}
		}
		return null;
	}
public void readFile(){
	ObjectInputStream ois=null;
	FileInputStream fis=null;
	String name="E:\\Android\\workspace\\MyOopBook\\src\\book.txt";
		try {fis=new FileInputStream(name);
		ois=new ObjectInputStream(fis);	
		bookList=(List<Book>) ois.readObject();		
}catch (FileNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (ClassNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
finally{
	try {
		if(ois!=null){
			ois.close();
		}
	} catch (IOException e) {
		// TODO: handle exception
		e.printStackTrace();
	}
}
}
public void saveFile(){
	ObjectOutputStream oos=null;
	FileOutputStream fos=null;
	String name="E:\\Android\\workspace\\MyOopBook\\src\\book.txt";
	try {fos=new FileOutputStream(name);
	oos=new ObjectOutputStream(fos);
	oos.writeObject(bookList);
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		try {
			if(oos!=null){
				oos.close();
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

}




}
package Library;
import java.util.List;
import java.util.Scanner;



public class BookTest {

	/**
	 * @param args
	 */
	private BookBiz bookbiz=new BookBiz();
	public static void main(String[] args) {
		// TODO Auto-generated method stub
       Scanner input=new Scanner(System.in);
       System.out.println("歡迎使用圖書書店");
       System.out.println("請輸入使用者名稱");
       String userName=input.next();
       System.out.println("請輸入密碼");
		String userPwd=input.next();
		
		BookTest bt=new BookTest();
		if(userName.equals("admin")&&userPwd.equals("123")){
			System.out.println("登陸成功");
			bt.showMainView();
			bt.adminOption();
		}else if(userName.equals("user")&&userPwd.equals("123")){
			System.out.println("登陸成功");
			bt.showuser();
			bt.adminOptionuser();
		}
		else{
			System.out.println("賬號密碼有錯誤");
		}
	}	
	
public void showMainView(){
	System.out.println("============歡迎登陸庫存管理系統===========");
	System.out.println("書號\t\t書名\t\t作者\t\t日期\t\t價格\t\t數量\t\t");
	List<Book> list=bookbiz.selectBooks();
	for (Book book : list) {
		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.getPubDate()+"\t");
		System.out.print(book.getPrice()+"\t\t");
		System.out.print(book.getStore()+"\n");
	}
	System.out.println("請選擇進行的操作:1.圖書入庫2.圖書出庫3.查詢全部圖書4.新增圖書5.退出");
	
	}

public void showuser(){
	System.out.println("************歡迎光臨圖書系統*************");
	System.out.println("書號\t\t書名\t\t作者\t\t日期\t\t價格\t\t數量\t\t");
	List<Book> list=bookbiz.selectBooks();
	for (Book book : list) {
		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.getPubDate()+"\t");
		System.out.print(book.getPrice()+"\t\t");
		System.out.print(book.getStore()+"\n");
	}
	System.out.println("請選擇進行的操作:1.查詢全部圖書2結賬3.退出");
	
	}

public void adminOptionuser(){
	 Scanner input=new Scanner(System.in);
	 int  which=input.nextInt();
	 switch(which){
	 case 1:  showuser(); 
	 adminOptionuser();               //1.查詢全部圖書
		 break;
	 case 2:  jiezhang();
	 adminOptionuser();                //2結賬
		 break;
		default: 
		    System.out.println("謝謝使用");//3退出
			break;
		}
	 
	 }

public void adminOption(){
	 Scanner input=new Scanner(System.in);
	 int  which=input.nextInt();
	 switch(which){
	 case 1:  tushuruku();      //1.圖書入庫
		 break;
	 case 2:  tushuchuku();      //2.圖書出庫
		 break;
	 case 3:          //3查詢全部圖書
	 { showMainView();
		 adminOption();}
		 break;
	 case 4:    insertbook();      //4.新增圖書
		 break;
		 
		default: 
		    System.out.println("謝謝使用");//5退出
			break;
		}
	 
	 }
public void tushuruku(){
	
	System.out.println("請輸入圖書資訊");
	System.out.println("請輸入圖書ID:");
	 Scanner input=new Scanner(System.in);
	 int  id=input.nextInt();
	 Book book=bookbiz.selectBook(id);
	 System.out.println("請輸入入庫庫存數量:");
	 int  store=input.nextInt();
	store+=book.getStore();
	 book.setStore(store);
	 bookbiz.saveFile();
	// bookbiz.readFile(); 一開始用第一次用讀
	 showMainView();
	 adminOption();
}
public void tushuchuku(){
	
	System.out.println("請輸入圖書資訊");
	System.out.println("請輸入圖書ID:");
	 Scanner input=new Scanner(System.in);
	 int  id=input.nextInt();
	 System.out.println("請輸入出庫庫存數量:");
	 int  store=input.nextInt();
	 bookbiz.selectBook2(id,store);
	 bookbiz.saveFile();
	// bookbiz.readFile(); 一開始用第一次用讀
	 showMainView();
	 adminOption();
}

private void insertbook() {
	// TODO Auto-generated method stub
	Scanner a=new Scanner(System.in);
	System.out.println("請輸入增加圖書資訊");
	System.out.println("請輸入圖書ID");
	int m=a.nextInt();
	System.out.println("請輸入書名");
	String e=a.next();
	System.out.println("請輸入作者");
	String t=a.next();
	System.out.println("請輸入日期");
	String y=a.next();
	System.out.println("請輸入價格");
	double p=a.nextInt();
	System.out.println("請輸入庫存");
	int u=a.nextInt();
	Book c=new Book(m,e,t,y,p,u);
	bookbiz.insertBook(c);
	 showMainView();
	 adminOption();
	
}

public void jiezhang(){
	System.out.println("請輸入欲購買的圖書資訊:");
	System.out.println("請輸入圖書Id:");
	 Scanner input=new Scanner(System.in);
	 int  id=input.nextInt();
	System.out.println("請輸入購買數量");
	 int  store=input.nextInt();
	 bookbiz.selectBook2(id,store);
	 bookbiz.saveFile();
	 Book book=bookbiz.selectBook(id);
	 System.out.println(book.getBookName()+":"+book.getPrice());
	System.out.println("數量:"+store);
	System.out.println("小計:"+(store*book.getPrice()));
	showuser();
}

}



使用者購買系統




圖書館使用者購買系統優化

package biz;
import java.io.Serializable;
public class Book implements Serializable {
	private int bookId;
	private String bookName;
	private String author;
	private String pubDate;
	private double price;
	private int   store;
	public Book(){}
	public Book(int bookId, String bookName, String author, String pubDate,
			double price, int store) {
		super();
		this.bookId = bookId;
		this.bookName = bookName;
		this.author = author;
		this.pubDate = pubDate;
		this.price = price;
		this.store = store;
	}
	
	
	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 getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getPubDate() {
		return pubDate;
	}
	public void setPubDate(String pubDate) {
		this.pubDate = pubDate;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getStore() {
		return store;
	}
	public void setStore(int store) {
		this.store = store;
	}
	

}

package biz;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;


public class BookBiz {
   private List<Book> bookList = new ArrayList<Book>();
   public BookBiz(){
	   readFile();
	   if(bookList.size()==0){
		   Book book = new Book(10001,"鬼吹燈","天下霸唱","2006-01-01",87,100);
		   insertBook(book);
		   book = new Book(10002,"魔戒","莫爾斯","2006-01-01",22,55);
		   insertBook(book);
		   book = new Book(10003,"哈利波特","羅琳","2008-01-01",45,25);
		   insertBook(book);
		   book = new Book(10004,"時間機器","威爾","2009-01-01",66,64);
		   insertBook(book);
		   book = new Book(10005,"宇宙奧祕","霍金","2010-01-01",76,67);
		   insertBook(book);
	   }
   }
   public void insertBook(Book book){
	   bookList.add(book);
	   saveFile();
   }
   public void deleteBook(int id){
	   for(int i= 0 ; i<bookList.size(); i++){
		   Book book = bookList.get(i);
		   if(book.getBookId() == id){
			   bookList.remove(i);
			   saveFile();
			   return;
		   }
	   }
   }
   public void updateBook(Book book){
	   for(int i=0 ; i < bookList.size();i++){
		    Book bk = bookList.get(i);
		    if(bk.getBookId() == book.getBookId()){
		    	bk.setAuthor(book.getAuthor());
		    	bk.setBookName(book.getBookName());
		    	bk.setPrice(book.getPrice());
		    	bk.setStore(book.getStore());
		    	bk.setPubDate(book.getPubDate());
		    	saveFile();
		    	return;
		    }
	   }
   }
   public List<Book> selectBooks(){
	   return bookList;
   }
   public Book selectBook(int id){
	   Book book = new Book();
	   for(int i =0;i<bookList.size();i++){
		   book = bookList.get(i);
		   if(book.getBookId() == id){
			   return book;
		   }
	   }
	   return null;
   }
   public void readFile(){
	   ObjectInputStream ois = null;
	   FileInputStream   fis = null;
	   String name = "E:\\Android\\workspace\\MyOopBookTeacher\\src\\biz\\book.txt";
       try {
		  fis = new FileInputStream(name);
		  ois = new ObjectInputStream(fis);
		  bookList = (List<Book>) ois.readObject();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				if(ois!=null){
					ois.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
   }
   public void saveFile(){
	     ObjectOutputStream oos = null;
	     FileOutputStream  fos  = null;
	     String name = "E:\\Android\\workspace\\MyOopBookTeacher\\src\\biz\\book.txt";
	     try {
			fos = new FileOutputStream(name);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(bookList);
			System.out.println("資料儲存成功");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				if(oos!=null){
					oos.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
   }
   
   
   
   
   
   
   
}

package biz;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;



public class BookTest {
	private BookBiz bookbiz = new BookBiz();
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("歡迎使用圖書書店");
		System.out.println("請輸入使用者名稱:");
		String userName = input.next();
		System.out.println("請輸入密碼:");
		String userPwd  = input.next();
		BookTest bt = new BookTest();
		if(userName.equals("admin")&&userPwd.equals("123")){
			System.out.println("登陸成功");
			bt.showMainView("admin");
			bt.adminOption();
		}else if(userName.equals("user")&&userPwd.equals("user")){
			System.out.println("登陸成功");
			bt.showMainView("user");
			bt.userOption();
		}
		else{
			System.out.println("使用者或密碼");
		}
	}
	public void showMainView(String arg){
		System.out.println("==================歡迎登入庫存管理系統===================");
		System.out.println("書號\t\t書名\t\t作者\t\t釋出日期\t\t價格\t\t庫存");
		List<Book> list = bookbiz.selectBooks();
		for (Book book : list) {
			System.out.print(book.getBookId()+"\t\t");
			System.out.print(book.getBookName()+"\t\t");
			System.out.print(book.getAuthor()+"\t\t");
			System.out.print(book.getPubDate()+"\t\t");
			System.out.print(book.getPrice()+"\t\t");
			System.out.print(book.getStore()+"\n");
		}
		if(arg.equals("admin")){
			System.out.println("請選擇進行的操作:1.圖書入庫 2.圖書出庫 " +
			"3.查詢全部圖書 4.新增圖書 5.退出");
		}else{
			System.out.println("請選擇進行的操作:1.查詢 2.結賬 3.退出");
		}
		
	}
	public void adminOption(){
		Scanner input = new Scanner(System.in);
		int which = input.nextInt();
		switch(which){
		     case  1: //入庫
		    	 inStore();
		    	 break;
		     case  2: //出庫
		    	 outStore();
		    	 break;
		     case  3: //查詢全部
		    	 showMainView("admin");
		    	 break;
		     case  4: //新增圖書
		    	 addBook();
		    	 break;
		     default: //退出
		    	 bookbiz.saveFile();
		    	 break;
		}
	}
	public void inStore(){
		Scanner input = new Scanner(System.in);
		System.out.println("請輸入圖書資訊");
		System.out.println("請輸入圖書ID:");
		int bookId = input.nextInt();
		Book  book = bookbiz.selectBook(bookId);
		System.out.println("請輸入入庫的數量");
		int qty = input.nextInt();
		qty += book.getStore();
		book.setStore(qty);
		bookbiz.saveFile();
		showMainView("admin");
		adminOption();
	}
	public void outStore(){
		Scanner input = new Scanner(System.in);
		System.out.println("請輸入圖書資訊");
		System.out.println("請輸入圖書ID:");
		int bookId = input.nextInt();
		Book  book = bookbiz.selectBook(bookId);
		System.out.println("請輸入出庫的數量");
		int qty = input.nextInt();
		book.setStore(book.getStore()-qty);
		bookbiz.saveFile();
		showMainView("admin");
		adminOption();
	}
	public void addBook(){
		Scanner input = new Scanner(System.in);
		System.out.println("請輸入圖書資訊");
		System.out.println("請輸入圖書ID:");
		int bookId = input.nextInt();
		System.out.println("請輸入書名");
		String bookName = input.next();
		System.out.println("請輸入作者");
		String author = input.next();
		System.out.println("請輸入出版時間");
		String pubDate = input.next();
		System.out.println("請輸入價格");
		double price = input.nextDouble();
		System.out.println("請輸入數量");
		int qty = input.nextInt();
		Book book = new Book();
		book.setBookId(bookId);
		book.setBookName(bookName);
		book.setAuthor(author);
		book.setPubDate(pubDate);
		book.setPrice(price);
		book.setStore(qty);
		bookbiz.insertBook(book);
		showMainView("admin");
		adminOption();
	}
	
	public void userOption(){
		Scanner input = new Scanner(System.in);
		int which = input.nextInt();
		switch(which){
		case 1:
			break;
		case 2:
			pay();
			break;
		default:
			break;
		}
	}
	public void pay(){
		Scanner input = new Scanner(System.in);
		boolean flag = true;
		double amt = 0;
		Map map = new HashMap();
		do{
			System.out.println("請輸入要購買的圖書編號:");
			int bookId = input.nextInt();
			Book book = bookbiz.selectBook(bookId);
			System.out.println("請輸入購買數量:");
			int count = input.nextInt();
			if(book.getStore()<count){
				System.out.println("購買數量過大,請重新輸入");
				continue;
			}
			if(map.containsKey(book)){
				int temp = Integer.parseInt(map.get(book).toString());
				map.put(book, count+temp);
			}else{
				map.put(book, count);
			}
			
			System.out.println(book.getBookName()+":"+book.getPrice());
			System.out.println(count);
			System.out.println("小計:"+(count*book.getPrice()));
			amt += count*book.getPrice();
			System.out.println("是否繼續購買(y/n)");
			String str = input.next();
			if(!str.equals("y")){
				flag = false;
			}
		}while(flag);
		
		System.out.println("總計:"+amt);
		System.out.println("支付:");
		double pay = input.nextDouble();
		Set set = map.keySet();
		Iterator it = set.iterator();
		
		while(it.hasNext()){
			System.out.println("AAAAA");
			Book book = (Book) it.next();
			book = bookbiz.selectBook(book.getBookId());
			int count = Integer.parseInt(map.get(book).toString());
			book.setStore(book.getStore()-count);
			bookbiz.saveFile();
		}
		System.out.println("找零:"+(pay-amt));
		showMainView("user");
		userOption();
		
	}	
	
}




作者:沖天之峰    20160619