运行截图
dao包
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.HashMap;
import java.util.Map;
import nuc.sw.shopping.db.DatabaseConfig;
import nuc.sw.shopping.entity.Project;
public class BookDaoImpl implements IBookDao {
//读文件
@Override
public Map<String, Project> getBook(){
Map<String , Project> books = new HashMap<String, Project>();
File f = new File(DatabaseConfig.BOOK_FILE_PATH);
FileInputStream fi = null;
ObjectInputStream or = null;
try {
fi = new FileInputStream(f);
or = new ObjectInputStream(fi);
books = (Map<String, Project>) or.readObject();
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
if(or!=null)
try {
or.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
return books;
}
//写文件
@Override
public void addBook(Map<String, Project> books) {
File f = new File(DatabaseConfig.BOOK_FILE_PATH);
ObjectOutputStream objo = null;
try {
objo = new ObjectOutputStream(new FileOutputStream(f));
objo.writeObject(books);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
if(objo != null)
try {
objo.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
@Override
public void addBook(Project b) {
Map<String, Project> books = getBook();
books.put(b.getBid(), b);
addBook(books);
}
@Override
public void deleteBook(Project project) {
Map<String, Project> users = getBook();
users.remove(project.getBid());
addBook(users);
}
}
package nuc.sw.shopping.dao;
import java.util.Map;
import nuc.sw.shopping.entity.Project;
public interface IBookDao {
//读文件
Map<String, Project> getBook();
//写文件
void addBook(Project b);
void addBook(Map<String, Project> books);
void deleteBook(Project project);
}
package nuc.sw.shopping.dao;
import java.util.HashMap;
import java.util.Map;
import nuc.sw.shopping.entity.User;
public interface IUserDao {
//读文件
public Map<String, User> getUsers();
//写文件
public void addUser(User u);
public void addUsers(Map<String, User> users);
}
package nuc.sw.shopping.dao;
import java.io.File;
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.HashMap;
import java.util.Map;
import nuc.sw.shopping.db.DatabaseConfig;
import nuc.sw.shopping.entity.User;
public class UserDaoImpl implements IUserDao {
@Override
public Map<String, User> getUsers() {
Map<String, User> users = new HashMap<String, User>();
File f = new File(DatabaseConfig.USER_FILE_PATH);
FileInputStream fi = null;
ObjectInputStream or = null;
try {
fi = new FileInputStream(f);
or = new ObjectInputStream(fi);
users = (Map<String, User>) or.readObject();
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
if(or!=null)
try {
or.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
return users;
}
@Override
public void addUser(User u) {
Map<String, User> users = getUsers();
users.put(u.getId(), u);
addUsers(users);
}
public void addUsers(Map<String, User> users) {
File f = new File(DatabaseConfig.USER_FILE_PATH);
ObjectOutputStream objo = null;
try {
objo = new ObjectOutputStream(new FileOutputStream(f));
objo.writeObject(users);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
if(objo != null)
try {
objo.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
bd
package nuc.sw.shopping.db;
public class DatabaseConfig {
public static final String USER_FILE_PATH = "src\\users.txt";
public static final String BOOK_FILE_PATH = "src\\books.txt";
public static final String CATEGORY_FILE_PATH = "src\\caegory.txt";
}
package nuc.sw.shopping.entity;
import java.io.Serializable;
public class Category implements Serializable{
private int id;
private String firstLevel;
private String secondLevel;
public Category() {
super();
}
public Category(int id, String firstLevel, String secondLevel) {
super();
this.id = id;
this.firstLevel = firstLevel;
this.secondLevel = secondLevel;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Category other = (Category) obj;
if (firstLevel == null) {
if (other.firstLevel != null)
return false;
} else if (!firstLevel.equals(other.firstLevel))
return false;
if (id != other.id)
return false;
if (secondLevel == null) {
if (other.secondLevel != null)
return false;
} else if (!secondLevel.equals(other.secondLevel))
return false;
return true;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstLevel() {
return firstLevel;
}
public void setFirstLevel(String firstLevel) {
this.firstLevel = firstLevel;
}
public String getSecondLevel() {
return secondLevel;
}
public void setSecondLevel(String secondLevel) {
this.secondLevel = secondLevel;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.firstLevel + ">" + this.secondLevel;
}
}
package nuc.sw.shopping.entity;
package nuc.sw.shopping.entity;
import java.io.Serializable;
public class Project implements Serializable{
private String bid;
private String name;
private String author;
private int number;
private double price;
private Category category;
public Project(String bid, String name, String author, double price, int number, nuc.sw.shopping.entity.Category category) {
super();
this.bid = bid;
this.name = name;
this.author = author;
this.price = price;
this.number = number;
this.category = category;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Project other = (Project) obj;
if (author == null) {
if (other.author != null)
return false;
} else if (!author.equals(other.author))
return false;
if (bid == null) {
if (other.bid != null)
return false;
} else if (!bid.equals(other.bid))
return false;
if (category == null) {
if (other.category != null)
return false;
} else if (!category.equals(other.category))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
return false;
return true;
}
public String getBid() {
return bid;
}
public void setBid(String bid) {
this.bid = bid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@Override
public String toString() {
return bid + ":" + name + ":" + author + ":" + number + ":"+this.category.getId()+":"
+ category.getFirstLevel()+ ":"+category.getSecondLevel();
}
}
package nuc.sw.shopping.entity;
import java.io.Serializable;
import java.util.HashMap;
public class Selecter extends HashMap<String, Project> implements Serializable{
private static final long serialVersionUID = 1L;
/**
* 将商品放入购物车
*
* @param project 书籍对象
* @param quantity 购买的数量
* @exception 购买数量超过实际库存
*/
public void buy(String bid, Project project) {
// 获取用户要购买的书籍的库存量
int num = 0;
// 判断是否满足用户的购买需求,若不满足,则抛出CartyException异常,给用户显示相关提示信息。
if(this.get(bid)!=null)
project.setNumber( this.get(bid).getNumber()+ project.getNumber());
// 更新用户购物车信息
put(project.getBid(), project);
}
// /**
// * 将商品从购物车中删除
// *
// * @param book 书籍对象
// * @exception 传入的书籍在购物车中不存在
// */
// public void remove(Book book) throws CartyException{
// // 判断传入的书籍在购物车中是否存在
// Integer num = null;
// if((num=get(book))==null) throw new CartyException("购物车内没有书籍:"+book.getName());
// else
// // 从购物车中删除指定书籍
// this.remove(book, this.get(book));
// // 加入库存
// book.setNumber(book.getNumber() + num.intValue());
// }
//
// // 显示购物车内容
// @Override
// public String toString() {
// StringBuffer buffer = new StringBuffer("");
// Set<Book> keys = super.keySet();
// Iterator<Book> looper = keys.iterator();
//
// // 循环显示购物车中用户购买的书籍
// while(looper.hasNext()) {
// Book book = looper.next();
// buffer.append(book.getName() + " 购买了:" + super.get(book) + "件\r\n");
// }
//
// return buffer.toString();
// }
//
}
package nuc.sw.shopping.entity;
import java.io.Serializable;
public class User implements Serializable{
private String id;
private String type;
private String password;
private String name;
private String department;
private Selecter selecter;
public User(String id, String type, String password) {
this.id = id;
this.type = type;
this.password = password;
selecter = new Selecter();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Byte getSex() {
return sex;
}
public void setSex(byte sex) {
this.sex = sex;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Selecter getSelecter() {
return selecter;
}
public void setSelecter(Selecter selecter) {
this.selecter = selecter;
}
@Override
public String toString() {
return id+":"+type+":"+password+":"+name+":"+sex+":"+city;
}
}
package nuc.sw.shopping.exception;
public class CartyException extends Exception {
public CartyException() {
super("您要购买的商品数量不足!");
}
public CartyException(String message) {
super(message);
}
}
package nuc.sw.shopping.frame;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import nuc.sw.shopping.entity.Category;
import nuc.sw.shopping.service.BookServiseImpl;
import nuc.sw.shopping.service.IBookServise;
import nuc.sw.shopping.entity.Project;
public class AddBookFrame extends JFrame {
public AddBookFrame() {
setTitle("添加图书");
setBounds(100, 500, 300, 500);
setResizable(false);
getContentPane().setLayout(new GridLayout(8,2));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
init();
setCenter(this);
this.setVisible(true);
}
private void setCenter(JFrame window) {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int width = window.getWidth();
int height = window.getHeight();
// 居中显示
window.setBounds((d.width - width) / 2,
(d.height - height) / 2,
width,
height);
}
private void init() {
JLabel jlb1,jlb2,jlb3,jlb4,jlb5,jlb6,jlb7;
JTextField bid,bname,bw,bj,bn;
JComboBox<String> c1,c2;
JButton add,re;
jlb1 = new JLabel("编号");
jlb2 = new JLabel("书名");
jlb3 = new JLabel("一级目录");
jlb4 = new JLabel("二级目录");
jlb5 = new JLabel("作者");
jlb6 = new JLabel("单价");
jlb7 = new JLabel("库存");
bid = new JTextField();
bname = new JTextField();
bw = new JTextField();
bj = new JTextField();
bn = new JTextField();
c1 = new JComboBox<String>();
c1.setModel(new DefaultComboBoxModel<String>(new String[] {
"名著", "工具"}));
c2 = new JComboBox<String>();
add = new JButton("添加");
re = new JButton("重置");
add(jlb1); add(bid);
add(jlb2); add(bname);
add(jlb3); add(c1);
add(jlb4); add(c2);
add(jlb5); add(bw);
add(jlb6); add(bj);
add(jlb7); add(bn);
add(add); add(re);
re.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
bid.setText("");
bname.setText("");
bw.setText("");
bj.setText("");
bn.setText("");
}
});
c1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
c2.removeAll();
if(c1.getSelectedIndex()==0)
c2.setModel(new DefaultComboBoxModel<String>(new String[] {
"古典名著", "现代名著"}));
else
if(c1.getSelectedIndex()==1)
c2.setModel(new DefaultComboBoxModel<String>(new String[] {
"软件开发", "电子技术"}));
}
});
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if(c2.getSelectedItem()!=null&&!bid.getText().trim().equals("")&&!bid.getText().trim().equals("")&&!bname.getText().trim().equals("")&&!bw.getText().trim().equals("")&&!bj.getText().trim().equals("")&&!bn.getText().trim().equals("")) {
Category cat = null;
if(c2.getSelectedItem().equals("软件开发"))
cat=new Category(1,"工具","软件开发");
else
if(c2.getSelectedItem().equals("电子技术"))
cat=new Category(2,"工具","电子技术");
else
if(c2.getSelectedItem().equals("古典名著"))
cat=new Category(3,"名著","古典名著");
else
if(c2.getSelectedItem().equals("现代名著"))
cat=new Category(4,"名著","现代名著");
Project project = new Project(bid.getText().trim(),cat);
IBookServise upbook = new BookServiseImpl();
upbook.addBook(project);
}
else
showMessage("请将书籍信息填写完整");
}
private void showMessage(String string) {
JOptionPane.showMessageDialog(null, string, "警告信息",
JOptionPane.WARNING_MESSAGE);
}
});
}
}
package nuc.sw.shopping.frame;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import nuc.sw.shopping.entity.User;
public class AdminMainFrame extends JFrame{
public AdminMainFrame(User u) {
setTitle("【欢迎"+u.getType()+u.getName()+"登录系统");
setSize(500,400);
setResizable(false);
getContentPane().setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init(u);
setCenter(this);
this.setVisible(true);
}
private void init(User u) {
JMenuBar jmb = new JMenuBar();
JMenu jm1 = new JMenu("商品管理");
JMenu jm2 = new JMenu("订单管理");
JMenu jm3 = new JMenu("角色管理");
JMenu jmi1_1 = new JMenu("查询商品信息");
JMenuItem jmi1_2 = new JMenuItem("添加商品");
JMenuItem jmi1_3 = new JMenuItem("删除商品");
JMenuItem jmi1_1_1 = new JMenuItem("按商品类型查询");
JMenuItem jmi1_1_2 = new JMenuItem("按商品名称查询");
JMenuItem jmi3_1 = new JMenuItem("修改个人信息");
jm1.add(jmi1_1); jm1.addSeparator(); jm1.add(jmi1_2); jm1.addSeparator(); jm1.add(jmi1_3);
jmi1_1.add(jmi1_1_1); jmi1_1.add(jmi1_1_2);
jm3.add(jmi3_1);
jmb.add(jm1); jmb.add(jm2); jmb.add(jm3);
setJMenuBar(jmb);
jmi1_3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new DeleteBookFrame();
}
});
jmi1_1_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CategoryShowBookFrame();
}
});
jmi1_1_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new NameShowBookFrame();
}
});
jmi1_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new AddBookFrame();
}
});
jmi3_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new UserForm(u);
JOptionPane.showMessageDialog(null, "修改成功", "警告信息",
JOptionPane.WARNING_MESSAGE);
}
});
}
private void setCenter(JFrame window) {
// 获取屏幕的大小
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int width = window.getWidth();
int height = window.getHeight();
// 居中显示
window.setBounds((d.width - width) / 2,
(d.height - height) / 2,
width,
height);
}
}
package nuc.sw.shopping.frame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import nuc.sw.shopping.entity.Project;
import nuc.sw.shopping.entity.Category;
import nuc.sw.shopping.service.BookServiseImpl;
import nuc.sw.shopping.service.IBookServise;
public class CategoryShowBookFrame extends JFrame{
JPanel jp1;
JLabel jlb1,jlb2;
JComboBox<String> c1,c2;
JButton jbt;
JTable jtb;
private IBookServise servise = new BookServiseImpl();
public CategoryShowBookFrame() {
setTitle("电商购物平台--商品查询");
setSize(550,450);
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
init();
setCenter(this);
this.setVisible(true);
}
private void init() {
setLayout(new BorderLayout());
jp1 = new JPanel();
jlb1 = new JLabel("一级目录");
c1 = new JComboBox<String>();
c2 = new JComboBox<String>();
c1.setModel(new DefaultComboBoxModel<String>(new String[] {
"名著", "工具"}));
jlb2 = new JLabel("二级目录");
jbt = new JButton("查询");
jtb = new JTable();
jp1.add(jlb1); jp1.add(c1); jp1.add(jlb2); jp1.add(c2); jp1.add(jbt);
add(jp1,BorderLayout.NORTH);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 97, 524, 273);
scrollPane.setViewportView(jtb);
add(scrollPane);
initBooksTableModel();
showBooks(null);
c1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
c2.removeAll();
if(c1.getSelectedIndex()==0)
c2.setModel(new DefaultComboBoxModel<String>(new String[] {
"古典名著", "现代名著"}));
else
if(c1.getSelectedIndex()==1)
c2.setModel(new DefaultComboBoxModel<String>(new String[] {
"软件开发", "电子技术"}));
}
});
jbt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
initBooksTableModel();
Category cat = null;
if(c2.getSelectedItem().equals("软件开发"))
cat=new Category(1,"工具","软件开发");
else
if(c2.getSelectedItem().equals("电子技术"))
cat=new Category(2,"工具","电子技术");
else
if(c2.getSelectedItem().equals("古典名著"))
cat=new Category(3,"名著","古典名著");
else
if(c2.getSelectedItem().equals("现代名著"))
cat=new Category(4,"名著","现代名著");
showBooks(cat);
}
});
}
private void initBooksTableModel() {
jtb.setBackground(new Color(255, 255, 255));
// 设置JTable表格对象被选中行的背景色
jtb.setSelectionBackground(new Color(128, 0, 255));
// 设置JTable表格对象被选中行的字体色
jtb.setSelectionForeground(new Color(255, 255, 255));
// 设置单选模式
jtb.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// 为JTable创建一个默认的表格模型
DefaultTableModel dt = new DefaultTableModel();
// 设置表格模型的列的个数和列的名字
dt.setColumnIdentifiers(new Object[] { "书籍编号", "书籍名称", "书籍作者", "单价", "库存", "书籍分类" });
// 为表格控件设置商品信息表格模型
jtb.setModel(dt);
// 设置表格各列的宽度
jtb.getColumnModel().getColumn(0).setPreferredWidth(70);
jtb.getColumnModel().getColumn(1).setPreferredWidth(150);
jtb.getColumnModel().getColumn(2).setPreferredWidth(70);
jtb.getColumnModel().getColumn(3).setPreferredWidth(50);
jtb.getColumnModel().getColumn(4).setPreferredWidth(70);
jtb.getColumnModel().getColumn(5).setPreferredWidth(164);
}
private void showBooks(Category cat) {
Map<String, Project> books = servise.selectBook(cat);
if (books == null || books.size() == 0) {
JOptionPane.showMessageDialog(null, "没有满足条件的书籍信息", "警告信息",
JOptionPane.WARNING_MESSAGE);
return;
}
// 在表格中显示数据
DefaultTableModel dt = (DefaultTableModel)jtb.getModel();
int i=0;
for (Map.Entry<String, Project> book:books.entrySet()) {
dt.insertRow(i, new Object[] { book.getValue().getBid(),
book.getValue().getName(), book.getValue().getAuthor(),book.getValue().getPrice(),
book.getValue().getNumber(),book.getValue().getCategory() });
i++;
}
}
private void setCenter(JFrame window) {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int width = window.getWidth();
int height = window.getHeight();
// 居中显示
window.setBounds((d.width - width) / 2,
(d.height - height) / 2,
width,
height);
}
}
package nuc.sw.shopping.frame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import nuc.sw.shopping.dao.BookDaoImpl;
import nuc.sw.shopping.entity.Project;
import nuc.sw.shopping.service.BookServiseImpl;
import nuc.sw.shopping.service.IBookServise;
public class DeleteBookFrame extends JFrame{
JPanel jp1,jp2;
JLabel jlb;
JTextField jtxt;
JButton jbt,jbt1;
JTable jtb;
private IBookServise servise = new BookServiseImpl();
public DeleteBookFrame() {
setTitle("电商购物平台--商品查询");
setSize(550,450);
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
init();
setCenter(this);
this.setVisible(true);
}
private void init() {
setLayout(new BorderLayout());
jp1 = new JPanel();
jp2 = new JPanel();
jlb = new JLabel("书籍名称");
jtxt = new JTextField(10);
jbt = new JButton("查询");
jbt1 = new JButton("删除书籍");
jtb = new JTable();
jp1.add(jlb); jp1.add(jtxt); jp1.add(jbt);
add(jp1,BorderLayout.NORTH);
jp2.add(jbt1);
add(jp2,BorderLayout.SOUTH);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 97, 524, 273);
scrollPane.setViewportView(jtb);
add(scrollPane);
initBooksTableModel();
showBooks(null);
jbt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
initBooksTableModel();
showBooks(jtxt.getText().trim());
}
});
jbt1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int index = jtb.getSelectedRow();
if(index != -1) {
Project project = null;
String bid = (String)jtb.getValueAt(index, 0);
project = new BookDaoImpl().getBook().get(bid);
if(new BookServiseImpl().deleteBook(project))
initBooksTableModel();
showBooks("");
JOptionPane.showMessageDialog(null, "删除成功");
}else{
JOptionPane.showMessageDialog(null, "还未选择商品");
} }
});
}
private void initBooksTableModel() {
jtb.setBackground(new Color(255, 255, 255));
// 设置JTable表格对象被选中行的背景色
jtb.setSelectionBackground(new Color(128, 0, 255));
// 设置JTable表格对象被选中行的字体色
jtb.setSelectionForeground(new Color(255, 255, 255));
// 设置单选模式
jtb.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// 为JTable创建一个默认的表格模型
DefaultTableModel dt = new DefaultTableModel();
// 设置表格模型的列的个数和列的名字
dt.setColumnIdentifiers(new Object[] { "书籍编号", "书籍名称", "书籍作者", "单价", "库存", "书籍分类" });
// 为表格控件设置商品信息表格模型
jtb.setModel(dt);
// 设置表格各列的宽度
jtb.getColumnModel().getColumn(0).setPreferredWidth(70);
jtb.getColumnModel().getColumn(1).setPreferredWidth(150);
jtb.getColumnModel().getColumn(2).setPreferredWidth(70);
jtb.getColumnModel().getColumn(3).setPreferredWidth(50);
jtb.getColumnModel().getColumn(4).setPreferredWidth(70);
jtb.getColumnModel().getColumn(5).setPreferredWidth(164);
}
private void showBooks(String bname) {
Map<String, Project> books = servise.selectBook(bname);
if (books == null || books.size() == 0) {
JOptionPane.showMessageDialog(null, "没有满足条件的书籍信息", "警告信息",
JOptionPane.WARNING_MESSAGE);
return;
}
// 在表格中显示数据
DefaultTableModel dt = (DefaultTableModel)jtb.getModel();
int i=0;
for (Map.Entry<String, Project> book:books.entrySet()) {
dt.insertRow(i, new Object[] { book.getValue().getBid(),
book.getValue().getName(), book.getValue().getAuthor(),book.getValue().getPrice(),
book.getValue().getNumber(),book.getValue().getCategory() });
i++;
}
}
private void setCenter(JFrame window) {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int width = window.getWidth();
int height = window.getHeight();
// 居中显示
window.setBounds((d.width - width) / 2,
(d.height - height) / 2,
width,
height);
}
}
package nuc.sw.shopping.frame;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import nuc.sw.shopping.dao.IUserDao;
import nuc.sw.shopping.dao.UserDaoImpl;
import nuc.sw.shopping.entity.User;
import nuc.sw.shopping.service.IUserService;
import nuc.sw.shopping.service.UserServiceImpl;
import nuc.sw.shopping.frame.UserForm;
public class LoginRegFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = -6571906457662372243L;
public LoginRegFrame() {
setTitle("" + "");
setBounds(100, 100, 389, 300);
setResizable(false);
getContentPane().setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
setCenter(this);
this.setVisible(true);
}
private void init() {
getContentPane().setLayout(new BorderLayout());
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
getContentPane().add(jp2,BorderLayout.SOUTH);
getContentPane().add(jp1,BorderLayout.CENTER);
jp1.setLayout(new GridLayout(3,2));
JLabel lb1 = new JLabel("用户名:",JLabel.CENTER);
JLabel lb2 = new JLabel("用户类型:",JLabel.CENTER);
JLabel lb3 = new JLabel("用户密码:",JLabel.CENTER);
JTextField t_name = new JTextField(18);
JComboBox<String> c_type = new JComboBox<String>();
c_type.addItem("普通用户");
c_type.addItem("管理员");
JPasswordField t_password = new JPasswordField(18);
JButton jbt1 = new JButton("登录");
JButton jbt2 = new JButton("重置");
JButton jbt3 = new JButton("点我注册");
jp1.add(lb1); jp1.add(t_name);
jp1.add(lb2); jp1.add(c_type);
jp1.add(lb3); jp1.add(t_password);
jp2.add(jbt1); jp2.add(jbt2); jp2.add(jbt3);
jbt1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
IUserService uService = new UserServiceImpl();
String id = t_name.getText();
String password = new String(t_password.getPassword());
String type = (String)c_type.getSelectedItem();
User u = new User(id, type, password);
if((u=uService.login(u)) != null){
if("管理员".equals(u.getType())) {
new AdminMainFrame(u);
dispose();
}
else
{new UserMainFrame(u);
dispose();
}
String name = u.getName();
if (name == null || "".equals(name)) {
// 没有设置所在城市,则进入用户信息编辑界面
System.out.println("没有设置所在城市,则进入用户信息编辑界面...");
UserForm userForm = new UserForm(u);
userForm.getUserForm().setVisible(true);}
} else {
showMessage("用户名或密码不正确,请重新输入!");
t_name.setText("");
t_password.setText("");
t_name.setFocusable(true);
}
}
});
jbt2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t_name.setText("");
t_password.setText("");
t_name.setFocusable(true);
}
});
jbt3.addActionListener(new ActionListener() {
IUserService uService = new UserServiceImpl();
@Override
public void actionPerformed(ActionEvent arg0) {
String id = t_name.getText();
String password = new String(t_password.getPassword());
String type = (String)c_type.getSelectedItem();
if(id.equals("")|| password.equals("")){
showMessage("用户名或密码不能为空!");
} else {
User u = new User(id, type, password);
String message = uService.register(u);
showMessage(message);
}
}
});
}
protected void showMessage(String string) {
JOptionPane.showMessageDialog(null, string, "警告信息",
JOptionPane.WARNING_MESSAGE);
}
private void setCenter(JFrame window) {
// 获取屏幕的大小
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int width = window.getWidth();
int height = window.getHeight();
// 居中显示
window.setBounds((d.width - width) / 2,
(d.height - height) / 2,
width,
height);
}
}
package nuc.sw.shopping.frame;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import nuc.sw.shopping.dao.IUserDao;
import nuc.sw.shopping.dao.UserDaoImpl;
import nuc.sw.shopping.entity.User;
public class MainClass{
public static void main(String[] args) {
new LoginRegFrame();
}
}
package nuc.sw.shopping.frame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import nuc.sw.shopping.entity.Project;
import nuc.sw.shopping.service.BookServiseImpl;
import nuc.sw.shopping.service.IBookServise;
public class NameShowBookFrame extends JFrame{
JPanel jp1;
JLabel jlb;
JTextField jtxt;
JButton jbt;
JTable jtb;
private IBookServise servise = new BookServiseImpl();
public NameShowBookFrame() {
setTitle("电商购物平台--商品查询");
setSize(550,450);
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
init();
setCenter(this);
this.setVisible(true);
}
private void init() {
setLayout(new BorderLayout());
jp1 = new JPanel();
jlb = new JLabel("书籍名称");
jtxt = new JTextField(10);
jbt = new JButton("查询");
jtb = new JTable();
jp1.add(jlb); jp1.add(jtxt); jp1.add(jbt);
add(jp1,BorderLayout.NORTH);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 97, 524, 273);
scrollPane.setViewportView(jtb);
add(scrollPane);
initBooksTableModel();
showBooks(null);
jbt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
initBooksTableModel();
showBooks(jtxt.getText().trim());
}
});
}
private void initBooksTableModel() {
jtb.setBackground(new Color(255, 255, 255));
// 设置JTable表格对象被选中行的背景色
jtb.setSelectionBackground(new Color(128, 0, 255));
// 设置JTable表格对象被选中行的字体色
jtb.setSelectionForeground(new Color(255, 255, 255));
// 设置单选模式
jtb.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// 为JTable创建一个默认的表格模型
DefaultTableModel dt = new DefaultTableModel();
// 设置表格模型的列的个数和列的名字
dt.setColumnIdentifiers(new Object[] { "书籍编号", "书籍名称", "书籍作者", "单价", "库存", "书籍分类" });
// 为表格控件设置商品信息表格模型
jtb.setModel(dt);
// 设置表格各列的宽度
jtb.getColumnModel().getColumn(0).setPreferredWidth(70);
jtb.getColumnModel().getColumn(1).setPreferredWidth(150);
jtb.getColumnModel().getColumn(2).setPreferredWidth(70);
jtb.getColumnModel().getColumn(3).setPreferredWidth(50);
jtb.getColumnModel().getColumn(4).setPreferredWidth(70);
jtb.getColumnModel().getColumn(5).setPreferredWidth(164);
}
private void showBooks(String bname) {
Map<String, Project> books = servise.selectBook(bname);
if (books == null || books.size() == 0) {
JOptionPane.showMessageDialog(null, "没有满足条件的书籍信息", "警告信息",
JOptionPane.WARNING_MESSAGE);
return;
}
// 在表格中显示数据
DefaultTableModel dt = (DefaultTableModel)jtb.getModel();
int i=0;
for (Map.Entry<String, Project> book:books.entrySet()) {
dt.insertRow(i, new Object[] { book.getValue().getBid(),
book.getValue().getName(), book.getValue().getAuthor(),book.getValue().getPrice(),
book.getValue().getNumber(),book.getValue().getCategory() });
i++;
}
}
private void setCenter(JFrame window) {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int width = window.getWidth();
int height = window.getHeight();
// 居中显示
window.setBounds((d.width - width) / 2,
(d.height - height) / 2,
width,
height);
}
}
package nuc.sw.shopping.frame;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import javax.swing.*;
import javax.swing.event.*;
import nuc.sw.shopping.dao.*;
import nuc.sw.shopping.entity.*;
import nuc.sw.shopping.service.*;
@SuppressWarnings("serial")
public class UserMainFrame extends JFrame{
IUserService userService = new UserServiceImpl();
User user = null;
public UserMainFrame(User user) {
this.user = user;System.out.println(user);
this.setTitle("电商购物平台");
setSize(700, 600);
setLocation(300,300);
init(user);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setCenter(this);
setVisible(true);
}
private void init(User u) {
// TODO Auto-generated method stub
JMenu usermanger = new JMenu("账户管理");
JMenuItem logout = new JMenuItem("注销账户");
JMenuItem change = new JMenuItem("修改密码");
JMenuItem upu = new JMenuItem("修改个人信息");
usermanger.add(logout);
usermanger.add(change);
usermanger.add(upu);
JMenu shopCart = new JMenu("购物车");
JMenu market = new JMenu("商场");
JMenuBar mb = new JMenuBar();
mb.add(market);
mb.add(usermanger);
mb.add(shopCart);
setJMenuBar(mb);
market();
shopCart.addMenuListener(new MenuListener() {
@Override
public void menuSelected(MenuEvent e) {
// TODO Auto-generated method stub
getContentPane().removeAll();
shopcart();
repaint();
revalidate();
}
@Override
public void menuDeselected(MenuEvent e) {
// TODO Auto-generated method stub
}
@Override
public void menuCanceled(MenuEvent e) {
// TODO Auto-generated method stub
}
});
market.addMenuListener(new MenuListener() {
@Override
public void menuSelected(MenuEvent e) {
// TODO Auto-generated method stub
getContentPane().removeAll();
total = 0;
market();
revalidate();
}
@Override
public void menuDeselected(MenuEvent e) {
// TODO Auto-generated method stub
}
@Override
public void menuCanceled(MenuEvent e) {
// TODO Auto-generated method stub
}
});
logout.addActionListener(new ActionListener() {
@SuppressWarnings("unused")
public void actionPerformed1(ActionEvent e) {
// TODO Auto-generated method stub
int i = JOptionPane.showConfirmDialog(null, "确定注销?", "警告", JOptionPane.YES_NO_OPTION);
if(i==0) {
new UserServiceImpl().delete(u);
dispose();
new LoginRegFrame();
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
change.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String inputValue = JOptionPane.showInputDialog( "请输入你的新密码:");
if(inputValue.equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空", "警告", JOptionPane.WARNING_MESSAGE);
}else {
if(new UserServiceImpl().repw(u, inputValue)) {
JOptionPane.showMessageDialog(null, "密码修改成功", "警告", JOptionPane.WARNING_MESSAGE);
}
}
}
});
upu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new UserForm(u);
}
});
}
protected void shopcart() {
// TODO Auto-generated method stub
setLayout(new FlowLayout());
JLabel hello = new JLabel("你好:"+user.getName());
JPanel jp1=new JPanel();
JPanel jp2=new JPanel(new FlowLayout());
JPanel jp3=new JPanel();
jp1.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.blue));
jp1.setPreferredSize(new Dimension(1000,40));
jp1.add(hello);
JButton J_change = new JButton("取消购买");
JButton J_changeAll = new JButton("清空购物车");
this.add(jp1);
JTable table = add2(user);
JScrollPane scrollpane=new JScrollPane(table);
jp2.add(scrollpane);
JLabel tatol = new JLabel("总价:"+total,JLabel.CENTER);
Font font = new Font("宋体", Font.PLAIN, 30);//创建1个字体实例
tatol.setFont(font);//设置JLabel的字体
tatol.setForeground(Color.PINK);//设置文字的颜色
jp2.add(tatol);
this.add(jp2);
jp3.add(J_change);
jp3.add(J_changeAll);
add(jp3);
J_changeAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
changeAll();
getContentPane().removeAll();
total = 0;
shopcart();
revalidate();
JOptionPane.showMessageDialog(null, "清空成功");
}
private void changeAll() {
new BookServiseImpl().addBook(user.getSelecter());
user.getSelecter().clear();
new UserDaoImpl().addUser(user);
}
});
J_change.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
change(table);
getContentPane().removeAll();
total = 0;
shopcart();
revalidate();
JOptionPane.showMessageDialog(null, "取消成功");
}
});
}
protected void change(JTable table) {
// TODO Auto-generated method stub
int index = table.getSelectedRow();
if(index!=-1) {
Selecter cart = new Selecter();
String bid = (String)table.getValueAt(index, 0);
Project project = user.getSelecter().get(bid);
if(new BookDaoImpl().getBook().get(bid)!=null)
project.setNumber(project.getNumber()+new BookDaoImpl().getBook().get(bid).getNumber());
new BookDaoImpl().addBook(user.getSelecter().get(bid));
user.getSelecter().remove(bid);
new UserDaoImpl().addUser(user);
}else{
JOptionPane.showMessageDialog(null, "还未选择商品");
}
}
public void market() {
setLayout(new FlowLayout());
JLabel hello = new JLabel("你好:"+user.getName());
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
jp1.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.blue));
jp1.setPreferredSize(new Dimension(1000,40));
jp2.setPreferredSize(new Dimension(500,560));
jp1.add(hello);
this.add(jp1);
JTable table = add1(user);
JScrollPane scrollpane=new JScrollPane(table);
jp2.add(scrollpane);
this.add(jp2);
JLabel J_No = new JLabel("购买数量");
JTextField j_TNo = new JTextField(5);
JButton J_Buy = new JButton("购买");
J_Buy.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(j_TNo.getText()!=""&&j_TNo.getText().trim().matches("^[0-9]+$"))
{
buy(table,user,Integer.parseInt(j_TNo.getText().trim()));
getContentPane().removeAll();
market();
repaint();
revalidate();
}
else
JOptionPane.showMessageDialog(null, "请输入正确的数量");
}
});
jp3.setLayout(new GridLayout(3,1));
jp3.add(J_No);
jp3.add(j_TNo);
jp3.add(J_Buy);
this.add(jp3);
}
public JTable add1(User u) {
Map<String, Project> books = new BookServiseImpl().selectBook("");
String[] colum = {"编号","书籍名称","作者","价格","库存","类名"};
int length = books.size();
String[][] row = new String[length][6];
JTable table = new JTable(row,colum);
int i=0;
for (Map.Entry<String, Project> book:books.entrySet()) {
row[i][0] = book.getValue().getBid();
row[i][1] = book.getValue().getName();
row[i][2] = book.getValue().getAuthor();
row[i][3] = book.getValue().getPrice()+"";
row[i][4] = book.getValue().getNumber()+"";
row[i][5] = book.getValue().getCategory()+"";
i++;
}
return table;
}
float total = 0;
public JTable add2(User u) {
Map<String, Project> books = u.getSelecter();
String[] colum = {"编号","书籍名称","单价","数量","总价"};
int length = books.size();
String[][] row = new String[length][5];
int i=0;
for (Map.Entry<String, Project> book:books.entrySet()) {
row[i][0] = book.getValue().getBid();
row[i][1] = book.getValue().getName();
row[i][2] = book.getValue().getPrice()+"";
row[i][3] = book.getValue().getNumber()+"";
row[i][4] = book.getValue().getPrice()*book.getValue().getNumber()+"";
total+=book.getValue().getPrice()*book.getValue().getNumber();
i++;
}
JTable table = new JTable(row,colum);
return table;
}
public void buy(JTable table,User u,int number) {
int index = table.getSelectedRow();
if(index != -1) {
Selecter cart = u.getSelecter();
Project project = null;
String bid = (String)table.getValueAt(index, 0);
IBookServise upbook = new BookServiseImpl();
project = new BookDaoImpl().getBook().get(bid);
project.setNumber(number);
if(upbook.buyBook(project)) {
u.getSelecter().buy(project.getBid(), project);
new UserDaoImpl().addUser(u);
JOptionPane.showMessageDialog(null, "购买成功");
}
}else{
JOptionPane.showMessageDialog(null, "还未选择商品");
}
}
private void setCenter(JFrame window) {
// 获取屏幕的大小
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int width = window.getWidth();
int height = window.getHeight();
// 居中显示
window.setBounds((d.width - width) / 2,
(d.height - height) / 2,
width,
height);
}
}
package nuc.sw.shopping.service;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.JOptionPane;
import nuc.sw.shopping.dao.BookDaoImpl;
import nuc.sw.shopping.dao.IBookDao;
import nuc.sw.shopping.entity.Project;
import nuc.sw.shopping.entity.Category;
public class BookServiseImpl implements IBookServise{
@Override
public void addBook(Project project) {
IBookDao bDao = new BookDaoImpl();
Map<String, Project> books = bDao.getBook();
if(books.get(project.getBid())!=null) {
if(books.get(project.getBid()).equals(project))
project.setNumber(books.get(project.getBid()).getNumber()+ project.getNumber());
else
{JOptionPane.showMessageDialog(null,"该编号不属于此书,书籍编号填写错误或书籍信息填写有误,请重新输入", "警告信息",
JOptionPane.WARNING_MESSAGE);
return;
}
}
bDao.addBook(project);
JOptionPane.showMessageDialog(null, "添加成功", "警告信息",
JOptionPane.WARNING_MESSAGE);
}
@Override
public Map<String, Project> selectBook(String bookName) {
if(bookName==null)
bookName = "";
Map<String, Project> books = new BookDaoImpl().getBook();
Map<String, Project> b =new HashMap<String, Project>();
Iterator itr = books.entrySet().iterator();
while(itr.hasNext()) {
Map.Entry<String, Project> entry = (Entry<String, Project>) itr.next();
if(entry.getValue().getName().indexOf(bookName)!=-1) {
b.put(entry.getKey(), entry.getValue());
}
}
return b;
}
@Override
public boolean deleteBook(Project project) {
IBookDao bDao = new BookDaoImpl();
Map<String, Project> books = bDao.getBook();
if(books.get(project.getBid())!=null) {
if(books.get(project.getBid()).equals(project))
project.setNumber(books.get(project.getBid()).getNumber()+ project.getNumber());
else
{JOptionPane.showMessageDialog(null,"该编号不属于此书,书籍编号填写错误或书籍信息填写有误,请重新输入", "警告信息",
JOptionPane.WARNING_MESSAGE);
return false;
}
}
bDao.deleteBook(project);
return true;
}
@Override
public Map<String, Project> selectBook(Category category) {
if (category==null)
return new BookDaoImpl().getBook();
Map<String, Project> books = new BookDaoImpl().getBook();
Map<String, Project> b =new HashMap<String, Project>();
Iterator itr = books.entrySet().iterator();
while(itr.hasNext()) {
Map.Entry<String, Project> entry = (Entry<String, Project>) itr.next();
if(entry.getValue().getCategory().equals(category)) {
b.put(entry.getKey(), entry.getValue());
}
}
return b;
}
@Override
public boolean buyBook(Project project) {
Map<String, Project> books = new BookDaoImpl().getBook();
int number = 0;
if(books.get(project.getBid().trim())!=null) {
if(books.get(project.getBid()).equals(project)) {
if(books.get(project.getBid()).getNumber() >= project.getNumber()) {
number = project.getNumber();
project.setNumber(books.get(project.getBid()).getNumber()- project.getNumber());}
else
{JOptionPane.showMessageDialog(null,"很抱歉《"+ project.getName()+"》只剩"+books.get(project.getBid()).getNumber()+"本了","抱歉",
JOptionPane.WARNING_MESSAGE);
return false;
}
}
else
{JOptionPane.showMessageDialog(null,"该编号不属于此书,书籍编号填写错误或书籍信息填写有误,请重新输入", "警告信息",
JOptionPane.WARNING_MESSAGE);
return false;
}
}
new BookDaoImpl().addBook(project);
project.setNumber(number);
return true;
}
@Override
public void addBook(Map<String, Project> Cate) {
Iterator itr = Cate.entrySet().iterator();
while(itr.hasNext()) {
Map.Entry<String, Project> entry = (Entry<String, Project>) itr.next();
if(new BookDaoImpl().getBook().get(entry.getKey())!=null)
entry.getValue().setNumber(entry.getValue().getNumber()+(new BookDaoImpl()).getBook().get(entry.getKey()).getNumber());
new BookDaoImpl().addBook(entry.getValue());
}
}
}
package nuc.sw.shopping.service;
import java.util.Map;
import nuc.sw.shopping.entity.Project;
import nuc.sw.shopping.entity.Category;
public interface IBookServise {
public void addBook(Project project);
public Map<String, Project> selectBook(String bookName);
public Map<String, Project> selectBook(Category category);
public boolean deleteBook(Project project);
public boolean buyBook(Project project);
public void addBook(Map<String, Project> Cate);
}
package nuc.sw.shopping.service;
import nuc.sw.shopping.entity.User;
public interface IUserService {
public String register(User u);
public User login(User u);
}
package nuc.sw.shopping.service;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import nuc.sw.shopping.dao.IUserDao;
import nuc.sw.shopping.dao.UserDaoImpl;
import nuc.sw.shopping.entity.User;
public class UserServiceImpl implements IUserService {
IUserDao uDao = new UserDaoImpl();
@Override
public String register(User u) {
if(useridIsValid(u.getId()) && passwordIsValid(u.getPassword())){
Map<String, User> users = uDao.getUsers();
if(users.containsKey(u.getId())){
return "注册失败,该用户id已经存在!请重新输入!!!";
} else{
uDao.addUser(u);
return "注册成功,请登录!";
}
} else {
return "用户名或密码格式不对,用户名必须是u开头,5位数字;密码必须是6~12位字母数字构成!";
}
}
public boolean passwordIsValid(String s){
Pattern p = Pattern.compile("^\\w{6,12}$");
Matcher m = p.matcher(s);
if(m.matches())
return true;
else
return false;
}
//该方法判断用户名是否符合格式要求。
public boolean useridIsValid(String s){
Pattern p = Pattern.compile("^[u]\\d{5}$");
Matcher m = p.matcher(s);
if(m.matches())
return true;
else
return false;
}
@Override
public User login(User u) {
Map<String, User> users = uDao.getUsers();
if(users.containsKey(u.getId())){
if(users.get(u.getId()).getPassword().equals(u.getPassword())){
return users.get(u.getId());
} else
return null;
}
else
return null;
}
public void delete(User u) {
Map<String, User> users = uDao.getUsers();
if(users.containsKey(u.getId())){
if(users.get(u.getId()).getPassword().equals(u.getPassword())){
users.remove(u.getId());
uDao.addUsers(users);
}
}
}
public boolean repw(User u,String pass) {
Map<String, User> users = uDao.getUsers();
if(users.containsKey(u.getId())){
if(users.get(u.getId()).getPassword().equals(u.getPassword())){
users.get(u.getId()).setPassword(pass);
uDao.addUsers(users);
return true;
}
else return false;
}
else return false;
}
}