import java.util.Objects;
/**
* @Author: 廾匸
* @Date: 2020/11/18 18:32
* @Description: 商品类
* @version: 1.01
*/
public class Goods{
private int id;
private String name;
private int quantity;
private double price;
public Goods(int id, String name, int quantity, double price) {
this.id = id;
this.name = name;
this.quantity = quantity;
this.price = price;
}
public Goods() {
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Goods goods = (Goods) o;
return id == goods.id &&
quantity == goods.quantity &&
Double.compare(goods.price, price) == 0;
}
@Override
public int hashCode() {
return Objects.hash(id, quantity, price);
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", Quantity=" + quantity +
", price=" + price +
'}';
}
}
import java.util.Comparator;
import java.util.List;
/**
* @Author: 廾匸
* @Date: 2020/11/18 18:36
* @Description: 方法类
* @version: 1.01
*/
public class Methed {
// 添加
public static void save(List<Goods> list,Goods goods){
if(list ==null||goods==null) {
System.out.println("无商品");
}
if(list.size() != 0){
for (Goods goods1 : list) {
if(goods1.getName().equals(goods.getName())){
System.out.println("用户名重复,请重新添加数据");
}
}
}
list.add(goods);
}
//删除
public static void delect(List<Goods> list,int id){
if (list==null||list.size()==0||id<1) {
System.out.println("没有此商品");
}
for (int i = 0; i < list.size(); i++) {
if(list.get(i).getId() == id){
list.remove(i);
System.out.println("删除成功");
}
}
System.out.println("删除之后剩余:");
list.forEach(System.out::println);
}
//修改
public static void update(List<Goods> list,int id,int quantity){
if (list==null||list.size()==0||id<1) {
System.out.println("没有此商品");
}
for (int i = 0; i < list.size(); i++) {
if(list.get(i).getId() == id){
list.get(i).setQuantity(quantity);
System.out.println("数量修改成功");
}
}
System.out.println("修改之后:");
list.forEach(System.out::println);
}
// 按购买商品的总价进行升序显示所购买的商品信息
public static void show(List<Goods> list){
if (list.size() != 0) {
list.sort(new Comparator<Goods>() {
@Override
public int compare(Goods o1, Goods o2) {
if ((o1.getPrice() * o1.getQuantity()) > (o2.getPrice() * o2.getQuantity())) {
return 1;
} else if ((o1.getPrice() * o1.getQuantity()) < (o2.getPrice() * o2.getQuantity())) {
return -1;
} else {
return 0;
}
}
});
for (Goods goods : list) {
System.out.println(goods);
}
} else {
System.out.println("你的购物车是空的,快去剁手吧!");
}
}
}
import java.util.ArrayList;
import java.util.List;
/**
* @Author: 廾匸
* @Date: 2020/11/18 18:29
* @Description: 购物车类
* @version: 1.01
*/
public class ShoppingCart {
public static void main (String[] args){
List<Goods> list = new ArrayList<>();
Methed.save(list,new Goods(1,"衣服",2,156.2));
Methed.save(list,new Goods(2,"食物",6,50.2));
Methed.save(list,new Goods(3,"鞋子",2,89.6));
Methed.save(list,new Goods(4,"帽子",1,46.3));
Methed.save(list,new Goods(5,"电脑",1,6399));
list.forEach(System.out::println);
System.out.println("******************************");
Methed.delect(list,2);
System.out.println("******************************");
Methed.update(list,3,1);
System.out.println("******************************");
Methed.show(list);
}
}
创建购物车类,模拟购物车功能 1)添加商品到购物车(输入商品的编号和数量) 2)删除商品(删除购物车中的指定购物项) 3) 修改商品(修改商品的数量)4)显示所购买的商品信息(按商品的总价进行升序显
最新推荐文章于 2022-09-23 11:36:58 发布