题目要求:使用面向对象的概念表示出下面的生活场景,某人去超市买东西,所有买到的东西都放在了购物车之中,最后到收银台一起结账。
购物车小系统实现以下功能:
* 1.自行把商品放入购物车(添加商品)
* 2.可以查看购物车里面的商品的属性(价格、数量、名字、编号等)
* 3.把商品从购物车拿出来(删除商品)
* 4.查看购物车有什么东西(查看购物车所有商品)
* 5.结账、打印小票
思路:使用一个简单的Map集合,写好每个功能的方法,然后直接调用就好了
完整代码放在最后!!!完整代码放在最后!!!完整代码放在最后!!!
有BUG没有改:查找商品和删除商品的时候如果一直没有输入正确的编号或者购物车为空会进入死循环
解决方法:直接添加个判断条件(输入某个数或者当购物车为空的时候结束循环就ok了)
分解代码:
(1)购物车系统
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Goods{
private Integer id;
private String name;
private double price;
private Integer numbers;
Goods(){}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double jia) {
this.price = jia;
}
public Integer getNumbers() {
return numbers;
}
public void setNumbers(Integer numbers) {
this.numbers = numbers;
}
public Goods(Integer id, String name, Integer price, Integer numbers) {
super();
this.id = id;
this.name = name;
this.price = price;
this.numbers = numbers;
}
}
(2)实现把商品添加到购物车(添加商品)
//添加购物车
public class GouWu {
Map<Integer,Goods> al=new HashMap<Integer,Goods>();
public void cun() {
Scanner sc=new Scanner(System.in);
System.out.println("请输入商品的编号");
Integer id = sc.nextInt();
Goods m=new Goods();
if(al.containsKey(id)) {
al.get(id).setNumbers((al.get(id).getNumbers()+1));
}else {
m.setId(id);
System.out.println("请输入添加的商品");
String name = sc.next();
m.setName(name);
System.out.println("请输入"+name+"的单价");
double jia = sc.nextDouble();
System.out.println("请输入购买"+name+"的数量");
int amount =sc.nextInt();
System.out.println();
System.out.println();
m.setPrice(jia);
m.setNumbers(amount);
al.put(id, m);
}
}
(3)查看购物车里面的商品的属性(价格、数量、名字、编号等)
//查看一件(查看购物车)
public void cha() {
System.out.println("请输入要查看商品的编号");
Scanner sc=new Scanner(System.in);
Integer id = sc.nextInt();
if(al.containsKey(id)) {
System.out.println("编号\t名字\t价格\t数量");
System.out.println(al.get(id).getId()+"\t"+al.get(id).getName()+"\t"+al.get(id).getPrice()+"\t"+al.get(id).getNumbers());
}else {
System.out.println("该商品编号不存在,请重新输入");
this.cha();//返回重新调用cha这个方法
}
System.out.println();
System.out.println();
}
(4)把商品从购物车拿出来(删除商品)
//删除商品
public void deleate(){
System.out.println("请输入要删除的商品的编号");
Scanner sc=new Scanner(System.in);
Integer id = sc.nextInt();
if(al.containsKey(id)) {
al.remove(id);
}else {
System.out.println("该商品编号不存在,请重新输入");
this.deleate();
}
System.out.println();
System.out.println();
}
(5)查看购物车有什么东西(查看购物车所有商品)
//查看购物车
public void View() {
System.out.println("-----------购物清单-----------");
for(Goods g:al.values()) {
System.out.println("编号\t名字\t价格\t数量");
System.out.println(g.getId()+"\t"+g.getName()+"\t"+g.getPrice()+"\t"+g.getNumbers());
}System.out.println();System.out.println();
}
(6)结账、打印小票
//打印小票
public void print() {
double sum=0;//总价
System.out.println("------欢迎光临XXXXXX超市-------");
System.out.println("-----------购物清单-----------");
for(Goods g:al.values()) {
System.out.println("编号\t名字\t价格\t数量");
System.out.println(g.getId()+"\t"+g.getName()+"\t"+g.getPrice()+"\t"+g.getNumbers());
sum += g.getPrice()*g.getNumbers();//总价=单价*数量
}
System.out.println("----------------------------");
System.out.println(" 你一共需要支付:"+sum+"元");
}
(7)测试调用
public static void main(String[] args) {
GouWu myg=new GouWu();
Scanner sc =new Scanner(System.in);
do {
System.out.println("----功能列表----");
System.out.println("(1)添加商品到购物车");
System.out.println("(2)查看商品属性");
System.out.println("(3)删除购物车商品");
System.out.println("(4)查看购物车所有商品");
System.out.println("(5)结账(打印小票)");
System.out.println();
System.out.println();
Integer it = sc.nextInt();
switch(it) {
case 1: myg.cun();
break;
case 2:myg.cha();
break;
case 3:myg.deleate();
break;
case 4:myg.View();
break;
case 5:
sc.close();
myg.print();
System.exit(0);
default:System.out.println("输入有误,请重新输入");
break;
}
}while(true);
}
完整代码:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Goods{
private Integer id;
private String name;
private double price;
private Integer numbers;
Goods(){}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double jia) {
this.price = jia;
}
public Integer getNumbers() {
return numbers;
}
public void setNumbers(Integer numbers) {
this.numbers = numbers;
}
public Goods(Integer id, String name, Integer price, Integer numbers) {
super();
this.id = id;
this.name = name;
this.price = price;
this.numbers = numbers;
}
}
//添加购物车
public class GouWu {
Map<Integer,Goods> al=new HashMap<Integer,Goods>();
public void cun() {
Scanner sc=new Scanner(System.in);
System.out.println("请输入商品的编号");
Integer id = sc.nextInt();
Goods m=new Goods();
if(al.containsKey(id)) {
al.get(id).setNumbers((al.get(id).getNumbers()+1));
}else {
m.setId(id);
System.out.println("请输入添加的商品");
String name = sc.next();
m.setName(name);
System.out.println("请输入"+name+"的单价");
double jia = sc.nextDouble();
System.out.println("请输入购买"+name+"的数量");
int amount =sc.nextInt();
System.out.println();
System.out.println();
m.setPrice(jia);
m.setNumbers(amount);
al.put(id, m);
}
}
//查看一件
public void cha() {
System.out.println("请输入要查看商品的编号");
Scanner sc=new Scanner(System.in);
Integer id = sc.nextInt();
if(al.containsKey(id)) {
System.out.println("编号\t名字\t价格\t数量");
System.out.println(al.get(id).getId()+"\t"+al.get(id).getName()+"\t"+al.get(id).getPrice()+"\t"+al.get(id).getNumbers());
}else {
System.out.println("该商品编号不存在,请重新输入");
this.cha();
}
System.out.println();
System.out.println();
}
//删除商品
public void deleate(){
System.out.println("请输入要删除的商品的编号");
Scanner sc=new Scanner(System.in);
Integer id = sc.nextInt();
if(al.containsKey(id)) {
al.remove(id);
}else {
System.out.println("该商品编号不存在,请重新输入");
this.deleate();
}
System.out.println();
System.out.println();
}
//打印小票
public void print() {
double sum=0;
System.out.println("------欢迎光临XXXXXX超市-------");
System.out.println("-----------购物清单-----------");
for(Goods g:al.values()) {
System.out.println("编号\t名字\t价格\t数量");
System.out.println(g.getId()+"\t"+g.getName()+"\t"+g.getPrice()+"\t"+g.getNumbers());
sum += g.getPrice()*g.getNumbers();
}
System.out.println("----------------------------");
System.out.println(" 你一共需要支付:"+sum+"元");
}
//查看购物车
public void View() {
System.out.println("-----------购物清单-----------");
for(Goods g:al.values()) {
System.out.println("编号\t名字\t价格\t数量");
System.out.println(g.getId()+"\t"+g.getName()+"\t"+g.getPrice()+"\t"+g.getNumbers());
}System.out.println();System.out.println();
}
public static void main(String[] args) {
GouWu myg=new GouWu();
Scanner sc =new Scanner(System.in);
do {
System.out.println("----功能列表----");
System.out.println("(1)添加商品到购物车");
System.out.println("(2)查看商品属性");
System.out.println("(3)删除购物车商品");
System.out.println("(4)查看购物车所有商品");
System.out.println("(5)结账(打印小票)");
System.out.println();
System.out.println();
Integer it = sc.nextInt();
switch(it) {
case 1: myg.cun();
break;
case 2:myg.cha();
break;
case 3:myg.deleate();
break;
case 4:myg.View();
break;
case 5:
sc.close();
myg.print();
System.exit(0);
default:System.out.println("输入有误,请重新输入");
break;
}
}while(true);
}
}
效果如下: