文章目录
- 包含的功能
- 增加商品
- 删除商品
- 修改商品购买数量
- 展示商品
- 源代码
- Item.java
- ShoppingCart.java
包含的功能
增加商品
private static void addItem(ArrayList<Item> shoppingCartList, Scanner sc) {
String id = null;
while(true){
System.out.println("请输入商品编号");
id = sc.next();
//判断购物车是否有这个物品编号,如果有返回索引,没有返回-1
int index = isExSitingId(shoppingCartList, id);
if(index == -1){
break;
}else{
System.out.println("您输入的商品编号已经存在!");
}
}
System.out.println("请输入商品的名字:");
String name = sc.next();
System.out.println("请输入商品的描述:");
String desc = sc.next();
System.out.println("请输入商品的价格:");
double price = sc.nextDouble();
System.out.println("请输入商品的购买数量:");
int count = sc.nextInt();
//每次添加一个物品,都要new一个新的Item
Item item = new Item(id, name, desc, price, count);
shoppingCartList.add(item);
System.out.println("添加成功");
}
删除商品
private static void deleteItemByItemId(ArrayList<Item> shoppingCartList,Scanner sc) {
if(shoppingCartList.isEmpty()){
System.out.println("购物车空空如也");
return;
}
String id = "";
//判断有没有这个商品id
System.out.println("请输入你要删除的商品编号:");
id = sc.next();
int index = isExSitingId(shoppingCartList, id);
if (index == -1) {
System.out.println("您输入的商品编号不存在,请确认后,重新选择操作!");
return;
} else {
shoppingCartList.remove(index);
System.out.println("删除成功");
}
}
修改商品购买数量
private static void updateItemByItemId(ArrayList<Item> shoppingCartList, Scanner sc) {
if(shoppingCartList.isEmpty()){
System.out.println("购物车空空如也");
return;
}
System.out.println("请输入你要修改的商品编号:");
String updateId = sc.next();
int index = isExSitingId(shoppingCartList,updateId);
if(index == -1){
System.out.println("您输入的商品编号不存在,请确认后,重新选择操作!");
return;
}else {
while (true) {
System.out.println("请输入你要购买的商品数量:");
int newCount = sc.nextInt();
if(newCount>0){
Item item = shoppingCartList.get(index);
item.setCount(newCount);
System.out.println("修改成功");
showItem(shoppingCartList);
break;
}else{
System.out.println("商品购买的数量必须大于0");
}
}
}
}
展示商品
private static void showItem(ArrayList<Item> shoppingCartList) {
if(shoppingCartList.isEmpty()){
System.out.println("购物车空空如也");
return;
}
System.out.println("商品编号\t\t商品名字\t\t商品描述\t\t商品价格\t\t商品购买数量");
for (int i = 0; i < shoppingCartList.size(); i++) {
Item item = shoppingCartList.get(i);
System.out.println(item.getId()+"\t\t\t"+item.getName()+"\t\t\t"+item.getDesc()+"\t\t\t"
+item.getPrice()+"\t\t\t"+item.getCount());
}
}
源代码
Item.java
package com.Junswl.shopping;
public class Item {
//商品编号
private String id;
//商品名称
private String name;
//商品描述
private String desc;
//商品价格
private double price;
//购买的商品数量
private int count;
//空参
public Item() {
}
//满参
public Item(String id, String name, String desc, double price, int count) {
this.id = id;
this.name = name;
this.desc = desc;
this.price = price;
this.count = count;
}
//get and set 方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
ShoppingCart.java
package com.Junswl.shopping;
import java.awt.*;
import java.util.ArrayList;
import java.util.Scanner;
public class ShoppingCart {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Item> shoppingCartList = new ArrayList<>();
while (true){
Menu();
System.out.println("请输入你的选择");
int num = sc.nextInt();
switch(num){
case 1:
addItem(shoppingCartList,sc);
break;
case 2:
deleteItemByItemId(shoppingCartList,sc);
break;
case 3:
updateItemByItemId(shoppingCartList,sc);
break;
case 4:
showItem(shoppingCartList);
break;
default:
System.out.println("选择错误,请重新选择");
}
}
}
private static void updateItemByItemId(ArrayList<Item> shoppingCartList, Scanner sc) {
if(shoppingCartList.isEmpty()){
System.out.println("购物车空空如也");
return;
}
System.out.println("请输入你要修改的商品编号:");
String updateId = sc.next();
int index = isExSitingId(shoppingCartList,updateId);
if(index == -1){
System.out.println("您输入的商品编号不存在,请确认后,重新选择操作!");
return;
}else {
while (true) {
System.out.println("请输入你要购买的商品数量:");
int newCount = sc.nextInt();
if(newCount>0){
Item item = shoppingCartList.get(index);
item.setCount(newCount);
System.out.println("修改成功");
showItem(shoppingCartList);
break;
}else{
System.out.println("商品购买的数量必须大于0");
}
}
}
}
private static void deleteItemByItemId(ArrayList<Item> shoppingCartList,Scanner sc) {
if(shoppingCartList.isEmpty()){
System.out.println("购物车空空如也");
return;
}
String id = "";
//判断有没有这个商品id
System.out.println("请输入你要删除的商品编号:");
id = sc.next();
int index = isExSitingId(shoppingCartList, id);
if (index == -1) {
System.out.println("您输入的商品编号不存在,请确认后,重新选择操作!");
return;
} else {
shoppingCartList.remove(index);
System.out.println("删除成功");
}
}
private static void showItem(ArrayList<Item> shoppingCartList) {
if(shoppingCartList.isEmpty()){
System.out.println("购物车空空如也");
return;
}
System.out.println("商品编号\t\t商品名字\t\t商品描述\t\t商品价格\t\t商品购买数量");
for (int i = 0; i < shoppingCartList.size(); i++) {
Item item = shoppingCartList.get(i);
System.out.println(item.getId()+"\t\t\t"+item.getName()+"\t\t\t"+item.getDesc()+"\t\t\t"
+item.getPrice()+"\t\t\t"+item.getCount());
}
}
private static void addItem(ArrayList<Item> shoppingCartList, Scanner sc) {
String id = null;
while(true){
System.out.println("请输入商品编号");
id = sc.next();
int index = isExSitingId(shoppingCartList, id);
if(index == -1){
break;
}else{
System.out.println("您输入的商品编号已经存在!");
}
}
System.out.println("请输入商品的名字:");
String name = sc.next();
System.out.println("请输入商品的描述:");
String desc = sc.next();
System.out.println("请输入商品的价格:");
double price = sc.nextDouble();
System.out.println("请输入商品的购买数量:");
int count = sc.nextInt();
Item item = new Item(id, name, desc, price, count);
shoppingCartList.add(item);
System.out.println("添加成功");
}
//判断商品id是否存在
private static int isExSitingId(ArrayList<Item> shoppingCartList,String id){
for (int i = 0; i < shoppingCartList.size(); i++) {
Item item = shoppingCartList.get(i);
if(item.getId().equals(id)){
return i;
}
}
return -1;
}
public static void Menu(){
System.out.println("================购物车=============");
System.out.println("1.添加商品到购物车");
System.out.println("2.删除购物车商品");
System.out.println("3.修改商品数量");
System.out.println("4.查询购物车");
}
}
学的不是技术,更是梦想!!!