面向对象设计(购物车)
购物车
一、成员介绍
二、前期调查
三、系统功能结构图
四、系统功能描述
用户先注册一个自己的账号,然后登录,如果密码错误,则重新输入密码,成功登录后就可以开始购物。用户可以通过自己的爱好选择商品,可以选择商品的多少,将其添加到购物车,如果选错了,可以从购物车中删除选错的商品,最后计算总额,然后填写收货地址,购物就完成了。
五、UML
六、包
有三个包,gui包,service包,shoppingcart包。
gui包为图形界面,如登陆界面LoginFrame类和菜单界面MenuFrame类。
service包包含Login类,用于登陆注册的相关功能。
shoppingcart则包含购物车的相关功能,如添加商品、删除商品等。
七、关键代码及效果展示
//登陆功能
package ShoppingCart;
import gui.ErrorFrame;
import gui.LoginFrame;
import gui.MenuFrame;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author HH、dL
*/
public class Login {
public boolean login(String name,String pwd)
{
try {
InputStream is = new FileInputStream("message.txt");
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String str=null;
str=br.readLine();
while(str!=null)
{
String str1[]=str.split(" ");
if(str1[0].equals(name)&&str1[1].equals(pwd))
return true;
str=br.readLine();
}
} catch (FileNotFoundException ex) {
Logger.getLogger(LoginFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(LoginFrame.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
}
//登陆界面登陆按钮
private void loginActionPerformed(java.awt.event.ActionEvent evt) {
String name=nameText.getText();
char[] password=pwdText.getPassword();
String pwd=new String(password);
Login log=new Login();
if(log.login(name, pwd))
{
MenuFrame menu=new MenuFrame();
menu.setVisible(true);
this.setVisible(false);
this.dispose();
}
else {
ErrorFrame error=new ErrorFrame();
error.setVisible(true);
}
}
//注册功能
package ShoppingCart;
import gui.RegisterFrame;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author HH、dL
*/
public class Register {
public boolean register(String name,String pwd){
File file=new File("message.txt");
if(file.exists())
{
try {
PrintWriter pw = new PrintWriter(new FileOutputStream("message.txt",true));
pw.write(name);
pw.write(" ");
pw.write(pwd);
pw.println();
pw.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(RegisterFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
else {
try {
PrintWriter pw=new PrintWriter(new FileOutputStream("message.txt"));
pw.write(name);
pw.write(" ");
pw.write(pwd);
pw.println();
pw.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(RegisterFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
return true;
}
}
//注册界面注册按钮
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String user=UserText.getText();
char []cpwd1=pwdText1.getPassword();
char []cpwd2=pwdText2.getPassword();
String pwd1=new String(cpwd1);
String pwd2=new String(cpwd2);
Register reg=new Register();
if(pwd1.equals(pwd2))
reg.register(user, pwd2);
}
//商品
package ShoppingCart;
/**
*
* @author HH、dL
*/
public class Goods {
private int count;
private int num;
private String name;
private double price;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Goods(int num, String name, double price, int count) {
this.count = count;
this.num = num;
this.name = name;
this.price = price;
}
}
//加入购物车
int []selectedRows=jTable2.getSelectedRows();
DefaultTableModel model1=(DefaultTableModel)jTable1.getModel();
TableModel model=jTable2.getModel();
for(int i=0;i<selectedRows.length;i++){
int id=(int)model.getValueAt(selectedRows[i], 0);
String name=(String)model.getValueAt(selectedRows[i], 1);
Double price=(Double)model.getValueAt(selectedRows[i], 2);
if(goods[id-1].getCount()==0)
{
Object[] o={id,name,price};
model1.addRow(o);
goods[id-1].setCount(1);
}
}
}
//删除购物车
int selectedRow = jTable1.getSelectedRow();
DefaultTableModel model=(DefaultTableModel)jTable1.getModel();
TableModel model1=jTable1.getModel();
if(selectedRow!=-1)
{
int id=(int)model1.getValueAt(selectedRow, 0);
goods[id-1].setCount(0);
model.removeRow(selectedRow);
}
}
//清空购物车
int Row=jTable1.getRowCount();
DefaultTableModel model=(DefaultTableModel)jTable1.getModel();
for(int i=0;i<Row;i++)
model.removeRow(0);
for(int i=0;i<6;i++)
goods[i].setCount(0);
//结算
DefaultTableModel model=(DefaultTableModel)jTable3.getModel();
DefaultTableModel model2=(DefaultTableModel)jTable1.getModel();
int selectedRow = jTable1.getSelectedRow();
TableModel model1=jTable1.getModel();
int id=(int)model1.getValueAt(selectedRow, 0);
int num=Integer.valueOf(numText.getText());
Double sum=num*goods[id-1].getPrice();
goods[id-1].setCount(0);
if(selectedRow!=-1)
model2.removeRow(selectedRow);
Object []o={id,goods[id-1].getName(),goods[id-1].getPrice(),num,sum};
model.addRow(o);