Java基础语法之购物车Demo
1. 需求
- 实现购物车的商品添加、修改、查询等功能
- 能够与用户交互
2. 学习目标
- 学习数组
- 学习面向对象编程思想,类的定义以及使用
- 学习if else switch while 分支语句
2. 实现
-
创建商品类
Goods.java
package demo;
/**
* @title 商品类
* @description 商品类
* */
public class Goods {
public int id;
public String name;
public double price;
public int number;
}
-
创建公共打印函数,主要是为了打印商品信息
Common.java
package utils;
import demo.Goods;
public class Common {
/**
* @Title 遍历数组
* @Description
* @param int
* @return void
*/
public void printArray(int[] array) {
for (int i=0;i<array.length;i++) {
System.out.print(array[i] + ";");
}
System.out.println();
}
public void printShop(Goods[] ga) {
System.out.println("您购买的商品如下:");
System.out.println("编号\t\t名称\t\t价格\t\t购买数量");
for (int i=0;i<ga.length;i++) {
if(ga[i] == null) {
break;
}
System.out.println(ga[i].id + "\t\t;" + ga[i].name+"\t\t;" + ga[i].price+"\t\t;" + ga[i].number);
}
System.out.println();
}
}
- 创建主函数 ShopMain.java
package demo;
import java.util.Scanner;
import utils.Common;
public class ShopMain {
public static void main(String[] args) {
//1.定义一个商品类
//2.定义一个数组
Goods[] shopCar = new Goods[100];
//3.搭建操作架构
OUT:
while(true) {
System.out.println("########欢迎光临#########");
System.out.println("添加商品:1");
System.out.println("查询商品:2");
System.out.println("修改商品:3");
System.out.println("结算购买:4");
System.out.println("退出:0");
System.out.println("请选择命令操作");
System.out.println("########欢迎光临#########");
Scanner sc = new Scanner(System.in);
int userInput = sc.nextInt();
switch(userInput) {
case 1:
addGoods(shopCar, sc);
break;
case 2:
queryGoods(shopCar);
break;
case 3:
updateGoods(shopCar, sc);
break;
case 4:
pay(shopCar);
break;
case 0:
System.out.println("欢迎下次光临");
break OUT;
default:
System.out.println("命令错误");
}
}
}
/**
* @title 支付订单
* */
private static void pay(Goods[] shopCar) {
// TODO Auto-generated method stub
double sum = 0;
for(int i = 0; i < shopCar.length; i++) {
if(shopCar[i] != null) {
sum += shopCar[i].price * shopCar[i].number;
}
}
System.out.println("总共:"+sum+"¥");
}
/**
* @title 查询商品
* */
private static void queryGoods(Goods[] shopCar) {
// TODO Auto-generated method stub
Common com = new Common();
com.printShop(shopCar);
}
/**
* @title 修改商品数量
* */
private static void updateGoods(Goods[] shopCar, Scanner sc) {
// TODO Auto-generated method stub
System.out.println("输入商品的id:");
int id = sc.nextInt();
for(int i=0; i < shopCar.length; i++) {
if(shopCar[i] != null && id==shopCar[i].id) {
System.out.println("找到商品:" + shopCar[i].name);
System.out.println("输入新的数量");
int number = sc.nextInt();
shopCar[i].number = number;
System.out.println("修改完成!!");
break;
}
}
}
/**
* @title 增加商品
* */
private static void addGoods(Goods[] shopCar, Scanner sc) {
// TODO Auto-generated method stub
System.out.println("请输入商品的编号");
int id = sc.nextInt();
System.out.println("请输入商品的名称");
String name = sc.next();
System.out.println("请输入商品的价格");
double price = sc.nextDouble();
System.out.println("请输入购买的数量");
int number = sc.nextInt();
Goods goods = new Goods();
goods.id = id;
goods.name = name;
goods.price = price;
goods.number = number;
for(int i=0; i < shopCar.length; i++) {
if(shopCar[i] == null) {
shopCar[i] = goods;
System.out.println("商品:" + name+"添加成功");
return;
}
}
}
}
4.测试结果
-
添加商品
-
查询商品
-
修改商品数量
-
结算