要求
图书库存管理系统; 1、新增图书; 2、删除图书;; 3、按照书名查询图书; 4、按照书号查询图书; 5、库存不足,提示用户及时补货
源码链接 运行截图 核心代码 Book类:存放图书基本属性
public class Book {
private String name;
private String bookNo;
private String press;
private double price;
private int num;
private String work;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBookNo() {
return bookNo;
}
public void setBookNo(String bookNo) {
this.bookNo = bookNo;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getWork() {
return work;
}
public void setWork(String work) {
this.work = work;
}
public Book() {
}
public Book(String name, String bookNo, String press, double price, int num, String work) {
this.name = name;
this.bookNo = bookNo;
this.press = press;
this.price = price;
this.num = num;
this.work = work;
}
}
BookFrm:图书窗体
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BookFrm extends JFrame implements ActionListener {
private JMenu jMenu1;
private JMenuBar jMenuBar;
private JMenuItem jMenuItem1,jMenuItem2,jMenuItem3,jMenuItem4,jMenuItem5;
public BookFrm(){
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
init();
}
public void init(){
jMenuBar = new JMenuBar();
jMenu1 = new JMenu();
jMenuItem1 = new JMenuItem();
jMenuItem2 = new JMenuItem();
jMenuItem3 = new JMenuItem();
jMenuItem4 = new JMenuItem();
jMenuItem5 = new JMenuItem();
setTitle("图书信息系统主界面");
setBounds(100,100,1000,800);
jMenu1.setText("书店库存");
jMenuItem1.setText("图书录入");
jMenu1.add(jMenuItem1);
jMenuItem2.setText("删除图书");
jMenu1.add(jMenuItem2);
jMenuItem3.setText("图书库存");
jMenu1.add(jMenuItem3);
jMenuBar.add(jMenu1);
jMenuItem1.addActionListener(this);
jMenuItem2.addActionListener(this);
jMenuItem3.addActionListener(this);
jMenuItem4.addActionListener(this);
jMenuItem5.addActionListener(this);
setJMenuBar(jMenuBar);
}
@Override
public void actionPerformed(ActionEvent e) {
if ("图书录入".equals(e.getActionCommand())){
BookAddFrm bookAddFrm = new BookAddFrm();
bookAddFrm.setVisible(true);
}
if ("删除图书".equals(e.getActionCommand())){
BookDelFrm bookDelFrm = new BookDelFrm();
bookDelFrm.setVisible(true);
}
if ("图书库存".equals(e.getActionCommand())){
BookSelFrm bookSelFrm = new BookSelFrm();
bookSelFrm.setVisible(true);
}
}
}
BookAddFrm:新增图书
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Statement;
import javax.swing.*;
public class BookAddFrm extends JFrame {
private JPanel panBtn = new JPanel();
private JPanel panLab = new JPanel();
private JLabel jLabel1 = new JLabel("名称:");
private JLabel jLabel2 = new JLabel("书号:");
private JLabel jLabel3 = new JLabel("出版社:");
private JLabel jLabel4 = new JLabel("单价:");
private JLabel jLabel5 = new JLabel("库存:");
private JLabel jLabel6 = new JLabel("作者:");
private JTextField jTextField1 = new JTextField();
private JTextField jTextField2 = new JTextField();
private JTextField jTextField3 = new JTextField();
private JTextField jTextField4 = new JTextField();
private JTextField jTextField5 = new JTextField();
private JTextField jTextField6 = new JTextField();
private JButton jButton1 = new JButton("添加");
private JButton jButton2 = new JButton("重置");
private JLabel jLabel = new JLabel();
BookList bookList = new BookList();
public BookAddFrm() {
setTitle("添加书籍");
setBounds(200,200,400,370);
setResizable(false);
setLayout(null);
setDefaultCloseOperation(HIDE_ON_CLOSE);
jLabel.setFont(new Font("宋体", 0, 24));
jLabel.setHorizontalAlignment(JLabel.CENTER);
jLabel.setForeground(new Color(255, 51, 51));
jLabel.setText("添 加 图 书");
jLabel1.setBounds(90,30,60,30);
jLabel2.setBounds(90,65,60,30);
jLabel3.setBounds(90,100,60,30);
jLabel4.setBounds(90,135,60,30);
jLabel5.setBounds(90,170,60,30);
jLabel6.setBounds(90,205,60,30);
jTextField1.setBounds(150,30,150,30);
jTextField2.setBounds(150,65,150,30);
jTextField3.setBounds(150,100,150,30);
jTextField4.setBounds(150,135,150,30);
jTextField5.setBounds(150,170,150,30);
jTextField6.setBounds(150,205,150,30);
jButton1.setBounds(100,250,80,30);
jButton2.setBounds(190,250,80,30);
add(jLabel1);
add(jLabel2);
add(jLabel3);
add(jLabel4);
add(jLabel5);
add(jLabel6);
add(jTextField1);
add(jTextField2);
add(jTextField3);
add(jTextField4);
add(jTextField5);
add(jTextField6);
add(jButton1);
add(jButton2);
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String name = jTextField1.getText().trim();
String no = jTextField2.getText().trim();
String press = jTextField3.getText().trim();
double price = Double.valueOf(jTextField4.getText().trim());
int num = Integer.valueOf(jTextField5.getText().trim());
String work = jTextField6.getText().trim();
if (name.length()==0){
JOptionPane.showMessageDialog(null, "书名不能为空!");
}
if (no.length()==0){
JOptionPane.showMessageDialog(null, "书号不能为空!");
}
if (press.length()==0){
JOptionPane.showMessageDialog(null, "出版社不能为空!");
}
if (jTextField4.getText().trim().length() == 0){
JOptionPane.showMessageDialog(null, "价格不能为空!");
}
if (jTextField5.getText().trim().length()==0){
JOptionPane.showMessageDialog(null, "数量不能为空!");
}
if (work.length()==0){
JOptionPane.showMessageDialog(null, "作者不能为空!");
}
price = discount(price,num);
Book book = new Book(name,no,press,price,num,work);
bookList.add(book);
JOptionPane.showMessageDialog(null, "添加图书成功!");
dispose();
}
});
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
}
});
}
public double discount(double price,int num){
double p = price;
if (num>50){
p = price*0.95;
}
if (num>100){
p = price*0.92;
}
if (num>500){
p = price*0.92;
}
return p;
}
}
BookList:图书集合
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BookList {
public static List<Book> books;
public void init(){
this.books = new ArrayList<>();
Book book1 = new Book("数据库", "1101","清华大学", 49.5, 50,"李大仁");
Book book2 = new Book("java语言", "1102","南京大学", 84.5, 25,"李斯");
Book book3 = new Book("c++语言", "1103","北京大学", 66.25, 90,"王蓓");
Book book4 = new Book("需求分析", "1104","北京大学", 66.25, 99,"李明");
Book book5 = new Book("需求分析2", "1105","北京大学", 66.25, 4,"李明");
books.add(book1);
books.add(book2);
books.add(book3);
books.add(book4);
books.add(book5);
}
public BookList() {
}
public double discount(double price,int num){
double p = price;
if (num>50){
p = price*0.95;
}
if (num>100){
p = price*0.92;
}
if (num>500){
p = price*0.92;
}
return p;
}
public void add(Book book) {
boolean t = false;
for (Book b : books) {
if (book.getBookNo().equals(b.getBookNo()) && book.getName().equals(b.getName())){
t = true;
double price = book.getPrice();
int num = book.getNum()+b.getNum();
b.setPrice(discount(price,num));
b.setNum(book.getNum()+b.getNum());
b.setPress(book.getPress());
}
}
if (!t){
books.add(book);
}
}
public Book selByNo(String no) {
Book book = null;
for (Book s: books) {
if (no.equals(s.getBookNo())){
book = s;
}
}
return book;
}
public List<Book> num() {
List<Book> bookList = null;
for (Book s: books) {
if (s.getNum()<5){
bookList.add(s);
}
}
return bookList;
}
public void remove(String no) {
for (Book s: books) {
if (no.equals(s.getBookNo())){
books.remove(s);
break;
}
}
}
public Book selByName(String name) {
Book book = null;
for (Book s: books) {
if (name.equals(s.getName())){
book = s;
}
}
return book;
}
}
源码链接
|