黑马程序员技术交流社区
标题:
【成都校区】java 面向对象 购物车
[打印本页]
作者:
小刀葛小伦
时间:
2019-12-26 17:48
标题:
【成都校区】java 面向对象 购物车
一个商城包括多个商品、多个用户。拥有销售商品、展示商品和查找商品功能。
2) 一个用户拥有一个购物车,购物车具有结算功能。
3) 商城具有名称,静态字符串类型
4) 用户类是抽象类,两个子类,售货员和顾客。购买商品作为抽象方法,售货员购买享八折优惠。
5) 商品和用户不再用二维数组,而是用类集来表示。
package tang;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.xml.soap.SAAJResult;
/*
*
* 基于上一个实验,建立商品类、购物车类、用户类、商城类,实现同样的功能。
1) 一个商城包括多个商品、多个用户。拥有销售商品、展示商品和查找商品功能。
2) 一个用户拥有一个购物车,购物车具有结算功能。
3) 商城具有名称,静态字符串类型
4) 用户类是抽象类,两个子类,售货员和顾客。购买商品作为抽象方法,售货员购买享八折优惠。
5) 商品和用户不再用二维数组,而是用类集来表示。
* */
public class work {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
shangping sp1 = new shangping(1001, "巧克力", 120, 5, "食品");
shangping sp2 = new shangping(1002, "牛奶", 100, 3, "食品");
shangping sp3 = new shangping(1003, "火腿肠", 500, 2, "食品");
shangping sp4 = new shangping(1004, "毛衣", 10, 1000, "衣服");
shangping sp5 = new shangping(1005, "裤子", 20, 999, "衣服");
shangping sp6 = new shangping(1006, "鞋子", 999, 200, "衣服");
gouwuche gu1 = new gouwuche(sp1, 2);
gouwuche gu2 = new gouwuche(sp2, 4);
gouwuche gu3 = new gouwuche(sp3, 2);
// 购物车
List<gouwuche> gous1 = new ArrayList<gouwuche>();
gous1.add(gu1);
gous1.add(gu2);
List<gouwuche> gous2 = new ArrayList<gouwuche>();
gous2.add(gu3);
gous2.add(gu2);
// 商品列表
List<shangping> shangpings1 = new ArrayList<shangping>();
shangpings1.add(sp1);
shangpings1.add(sp2);
shangpings1.add(sp6);
// 用户
guke g1 = new guke("xiaohua", "123456", 0, gous1);
guke g2 = new guke("laowang", "123456", 0, gous2);
List<yonghu> yonghus = new ArrayList<yonghu>();
yonghus.add(g1);
yonghus.add(g2);
// 商城
shangcheng sc = new shangcheng(yonghus, shangpings1);
System.out.println("请输入用户名");
String name = input.next();
System.out.println("请输入密码");
String mima = input.next();
boolean user = userhave(name, mima, sc);
// 1-加入购物车,2-查询商品,3-结算,4-退出”
if (user) {
System.out.println("登录成功");
while (true) {
System.out.println("1-查看购物车" + "\t" + "2-查询商品" + "\n" + "3-结算" + "\t" + "4-退出");
int a = input.nextInt();
yonghu loginuser = getuser(name, sc);
if (a == 1) {
showgou(loginuser);
} else if (a == 2) {
System.out.println("请输入查询商品信息:");
String searchname = input.next();
int i = chaxun(searchname, sc);
if (i == -1) {
System.out.println("没有此商品");
} else {
System.out.println("是否加入购物车 是/否");
String jiagou = input.next();
if (jiagou.equals("是")) {
jiagou(loginuser, i, sc);
} else {
continue;
}
}
} else if (a == 3) {
showgou(loginuser);
System.out.println();
System.out.println(jiesuan(loginuser));
} else {
break;
}
}
} else {
System.out.println("登录失败");
}
}
private static void jiagou(yonghu loginuser, int i, shangcheng sc) {
// TODO Auto-generated method stub
gouwuche gou = new gouwuche(sc.getShangpings().get(i), 1);
sc.getShangpings().get(i).setShuliang(sc.getShangpings().get(i).getShuliang() - 1);
loginuser.getGouwuches().add(gou);
}
private static int chaxun(String searchname, shangcheng sc) {
// TODO Auto-generated method stub
int s = -1;
for (int i = 0; i < sc.getShangpings().size(); i++) {
if (sc.getShangpings().get(i).getName().equals(searchname)) {
System.out.println("编号:" + sc.getShangpings().get(i).getBianhao());
System.out.println("名称:" + sc.getShangpings().get(i).getName());
System.out.println("价格:" + sc.getShangpings().get(i).getJiage());
System.out.println("库存:" + sc.getShangpings().get(i).getShuliang());
System.out.println("类别:" + sc.getShangpings().get(i).getLeibie());
s = i;
}
}
return s;
}
private static double jiesuan(yonghu loginuser) {
// TODO Auto-generated method stub
double sum = 0;
for (int i = 0; i < loginuser.getGouwuches().size(); i++) {
sum += loginuser.getGouwuches().get(i).getShuliang()
* loginuser.getGouwuches().get(i).getShangping().getJiage();
}
return sum;
}
private static void showgou(yonghu loginuser) {
// TODO Auto-generated method stub
System.out.println("商品编号" + " " + "商品名称" + " " + "商品价格" + " " + "商品数量");
for (int i = 0; i < loginuser.getGouwuches().size(); i++) {
System.out.print(loginuser.getGouwuches().get(i).getShangping().getBianhao() + " ");
System.out.print(loginuser.getGouwuches().get(i).getShangping().getName() + " ");
System.out.print(loginuser.getGouwuches().get(i).getShangping().getJiage() + " ");
System.out.print(loginuser.getGouwuches().get(i).getShuliang() + " ");
System.out.println();
}
System.out.println();
}
private static yonghu getuser(String name, shangcheng sc) {
// TODO Auto-generated method stub
for (int i = 0; i < sc.getYonghus().size(); i++) {
if (sc.getYonghus().get(i).getUsername().equals(name)) {
return sc.getYonghus().get(i);
}
}
return null;
}
private static boolean userhave(String name, String mima, shangcheng sc) {
// TODO Auto-generated method stub
for (int i = 0; i < sc.getYonghus().size(); i++) {
if (sc.getYonghus().get(i).getUsername().equals(name)) {
if (sc.getYonghus().get(i).getPassword().equals(mima))
return true;
}
}
return false;
}
}
//分别有商品编号、商品名称、商品数量、商品价格和商品类别。
class shangping {
private long bianhao;
private String name;
private int shuliang;
private double jiage;
private String leibie;
public long getBianhao() {
return bianhao;
}
public void setBianhao(long bianhao) {
this.bianhao = bianhao;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getShuliang() {
return shuliang;
}
public void setShuliang(int shuliang) {
this.shuliang = shuliang;
}
public double getJiage() {
return jiage;
}
public void setJiage(double jiage) {
this.jiage = jiage;
}
public String getLeibie() {
return leibie;
}
public void setLeibie(String leibie) {
this.leibie = leibie;
}
@Override
public String toString() {
return "shangping [bianhao=" + bianhao + ", name=" + name + ", shuliang=" + shuliang + ", jiage=" + jiage
+ ", leibie=" + leibie + "]";
}
public shangping(long bianhao, String name, int shuliang, double jiage, String leibie) {
super();
this.bianhao = bianhao;
this.name = name;
this.shuliang = shuliang;
this.jiage = jiage;
this.leibie = leibie;
}
}
class gouwuche {
private shangping shangping;
private int shuliang;
public shangping getShangping() {
return shangping;
}
public void setShangping(shangping shangping) {
this.shangping = shangping;
}
public int getShuliang() {
return shuliang;
}
public void setShuliang(int shuliang) {
this.shuliang = shuliang;
}
public gouwuche(tang.shangping shangping, int shuliang) {
super();
this.shangping = shangping;
this.shuliang = shuliang;
}
// List thinglist= new ArrayList<>();
// public List<shangping> getShangpings() {
// return shangpings;
// }
// public gouwuche(List<shangping> shangpings) {
// super();
// this.shangpings = shangpings;
// }
// public void setShangpings(List<shangping> shangpings) {
// this.shangpings = shangpings;
// }
//
//
// public double jiesuan(List<shangping> shangpings) {
// double sum=0;
// for(int i=0;i<shangpings.size();i++) {
// sum+=shangpings.get(i).getJiage();
// }
// return sum;
//
// }
}
abstract class yonghu {
private String username;
private String password;
private double yue;
private List<gouwuche> gouwuches;
public List<gouwuche> getGouwuches() {
return gouwuches;
}
public void setGouwuches(List<gouwuche> gouwuches) {
this.gouwuches = gouwuches;
}
public void goumai() {
}
public double getYue() {
return yue;
}
public void setYue(double yue) {
this.yue = yue;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "yonghu [username=" + username + ", password=" + password + ", yue=" + yue + ", gouwuches=" + gouwuches
+ "]";
}
public yonghu(String username, String password, double yue, List<gouwuche> gouwuches) {
super();
this.username = username;
this.password = password;
this.yue = yue;
this.gouwuches = gouwuches;
}
// public double jiesuan() {
// double sum=0;
// for (int i = 0; i < gouwuches.size(); i++) {
// sum+=gouwuches.get(i).getShangping().getJiage();
//
// }
// return sum;
// }
}
class guke extends yonghu {
public guke(String username, String password, double yue, List<gouwuche> gouwuches) {
super(username, password, yue, gouwuches);
// TODO Auto-generated constructor stub
}
}
class xiaoshou extends yonghu {
public xiaoshou(String username, String password, double yue, List<gouwuche> gouwuches) {
super(username, password, yue, gouwuches);
// TODO Auto-generated constructor stub
}
}
class shangcheng {
private static String name;
List<yonghu> yonghus = new ArrayList<yonghu>();
List<shangping> shangpings = new ArrayList<shangping>();
public shangcheng(List<yonghu> yonghus, List<shangping> shangpings) {
super();
this.yonghus = yonghus;
this.shangpings = shangpings;
}
public static String getName() {
return name;
}
public static void setName(String name) {
shangcheng.name = name;
}
public List<yonghu> getYonghus() {
return yonghus;
}
public void setYonghus(List<yonghu> yonghus) {
this.yonghus = yonghus;
}
public List<shangping> getShangpings() {
return shangpings;
}
public void setShangpings(List<shangping> shangpings) {
this.shangpings = shangpings;
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2