超市购物小票案例
1.1超市购物小票需求分析
模拟真实购物逻辑,以上述图片为需求原型,根据不同购买物品,完成购物小票内容打印到控制台。(简化项目逻辑,票脚部分只完成数据统计)
数据:
- 将超市购物小票案例中零散数据(名称、货号、单价、数量、计价单位、金额)封装为货物对象
- 所有的货物对象放置到集合中统一管理。
逻辑:
用户循环进行三个操作:
- 输入购买数量,代表为所购买货物的数量赋值,从而计算每项商品金额
- 打印小票,将已有数据打印
- 退出系统(因为该程序为循环操作,无法终止,如果不想再进行操作,则退出系统)
1.2超市购物小票知识点分析
复习知识点:
变量定义:记录部分数据
集合的使用:用于存储多个完整数据
运算符的使用:计算变量
流程控制语句的使用:
if语句:用于判断是否购买了商品
switch语句:用于进行三个操作(输入购买数量、打印小票、退出系统)的分支
for语句:switch外部使用for循环,当执行一个操作后,继续进行下一个操 作选择
方法封装:
由于代码量比较多,可以将输入购买数量、打印小票封装为方法使用
字符串/键盘录入:
打印小票完整过程均为字符串的拼写及打印,数量输入为键盘录入Scanner 完成
步骤:
定义GoodsItem类(名称,货号,单价,数量,计价单位,金额)
成员位置定义集合,定义方法向集合中存储所有GoodsItem数据
实现主干逻辑
打印欢迎语句
使用for/while进行循环进行操作
使用switch给出操作选择:1输入购买数量2打印小票3退出
完成switch中三个case的逻辑
运用方法封装功能:将1输入购买数量、2打印小票封装为独立方法,在case中 调用,提高代码可读性。
1.3超市购物小票商品项类的定义
商品项类,将要购买的商品封装到一个GoodItems类中,编码如下:
package com.igeek_03;
/**
* @ClassName: GoodsItem
* @Description: 商品项类
* @date 2017年11月13日 上午10:41:21
* Company www.igeekhome.com
*
* 定义商品项类:
* 名称,货号,单价,数量,计价单位,金额
*
*/
public class GoodsItem {
// 成员变量
/**
* @Fields name : 名称
*/
private String name;
/**
* @Fields id : 货号
*/
private String id;
/**
* @Fields price : 单价
*/
private double price;
/**
* @Fields number : 数量
*/
private int number;
/**
* @Fields unit : 计价单位
*/
private String unit;
/**
* @Fields money : 金额
*/
private double money;
// 构造方法
/**
* @Title: GoodsItem
*/
public GoodsItem() {
}
/**
* @Title: GoodsItem
* @param name
* @param id
* @param price
* @param number
* @param unit
* @param money
*/
public GoodsItem(String name, String id, double price, int number, String unit, double money) {
this.name = name;
this.id = id;
this.price = price;
this.number = number;
this.unit = unit;
this.money = money;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
/**
* @return the number
*/
public int getNumber() {
return number;
}
/**
* @param number the number to set
*/
public void setNumber(int number) {
this.number = number;
}
/**
* @return the unit
*/
public String getUnit() {
return unit;
}
/**
* @param unit the unit to set
*/
public void setUnit(String unit) {
this.unit = unit;
}
/**
* @return the money
*/
public double getMoney() {
return money;
}
/**
* @param money the money to set
*/
public void setMoney(double money) {
this.money = money;
}
}
1.4超市购物小票初始化数据
成员位置定义集合,定义方法向集合中存储所有GoodsItem数据
package com.igeek_04;
import java.util.ArrayList;
import com.igeek_03.GoodsItem;
/**
* @ClassName: ShoppingReceipt
* @Description: 超市购物小票类
* @date 2017年11月13日 上午10:48:20
* Company www.igeekhome.com
*
*/
public class ShoppingReceipt {
/**
* @Fields data : 成员位置定义集合,存储所有的商品项对象
*/
static ArrayList<GoodsItem> data = new ArrayList<GoodsItem>();
public static void main(String[] args) {
//为集合准备数据
initData();
}
/**
* @Title: initData
* @Description: 定义方法,向成员位置的集合赋值
*/
public static void initData() {
//创建多个商品项对象
GoodsItem sls = new GoodsItem("少林寺核桃", "090115", 15.5, 0, "个", 0);
GoodsItem shk = new GoodsItem("尚康饼干", "090027", 14.5, 0, "个", 0);
//将多个商品项对象放入集合中
data.add(sls);
data.add(shk);
}
}
1.5超市购物小票主干逻辑
1.5.1实现主干逻辑
打印欢迎语句
使用for/while进行循环进行操作
使用switch给出操作选择:1输入购买数量2打印小票3退出
完成switch中三个case的逻辑
运用方法封装功能:将1输入购买数量、2打印小票封装为独立方法,在case中调用,提高代码可读性。
package com.igeek_05;
import java.util.ArrayList;
import java.util.Scanner;
import com.igeek_03.GoodsItem;
/**
* @ClassName: ShoppingReceipt
* @Description: 超市购物小票类
* @date 2017年11月13日 上午10:48:20
* Company www.igeekhome.com
*
*/
public class ShoppingReceipt {
/**
* @Fields data : 成员位置定义集合,存储所有的商品项对象
*/
static ArrayList<GoodsItem> data = new ArrayList<GoodsItem>();
public static void main(String[] args) {
//为集合准备数据
initData();
//打印欢迎语句
System.out.println("欢迎使用超市管理系统!");
//使用死循环完成反复操作的逻辑
//for(;;) {}
while(true) {
//使用switch给出操作选择:1输入购买数量2打印小票3退出
//提示信息
System.out.println("请输入您要进行的操作:1输入购买数量 2打印小票 3退出");
//通过键盘录入得到数据
Scanner sc = new Scanner(System.in);
int optNumber = sc.nextInt();
//完成switch中三个case的逻辑
switch (optNumber) {
case 1:
System.out.println("给所有数据量赋值");
break;
case 2:
//调用打印购物小票的方法
System.out.println("打印购物小票");
break;
case 3:
System.out.println("欢迎下次光临");
//退出程序
System.exit(0);
default:
System.out.println("请输入正确的数字!");
break;
}
}
}
/**
* @Title: initData
* @Description: 定义方法,向成员位置的集合赋值
*/
public static void initData() {
//创建多个商品项对象
GoodsItem sls = new GoodsItem("少林寺核桃", "090115", 15.5, 0, "个", 0);
GoodsItem shk = new GoodsItem("尚康饼干", "090027", 14.5, 0, "个", 0);
//将多个商品项对象放入集合中
data.add(sls);
data.add(shk);
}
}
1.6超市购物小票为商品项赋值数量与金额
定义一个方法为所有的数据赋值数量和金额
package com.igeek_06;
import java.util.ArrayList;
import java.util.Scanner;
import com.igeek_03.GoodsItem;
/**
* @ClassName: ShoppingReceipt
* @Description: 超市购物小票类
* @date 2017年11月13日 上午10:48:20
* Company www.igeekhome.com
*
*/
public class ShoppingReceipt {
/**
* @Fields data : 成员位置定义集合,存储所有的商品项对象
*/
static ArrayList<GoodsItem> data = new ArrayList<GoodsItem>();
public static void main(String[] args) {
//为集合准备数据
initData();
//打印欢迎语句
System.out.println("欢迎使用超市管理系统!");
//使用死循环完成反复操作的逻辑
//for(;;) {}
while(true) {
//使用switch给出操作选择:1输入购买数量2打印小票3退出
//提示信息
System.out.println("请输入您要进行的操作:1输入购买数量 2打印小票 3退出");
//通过键盘录入得到数据
Scanner sc = new Scanner(System.in);
int optNumber = sc.nextInt();
//完成switch中三个case的逻辑
switch (optNumber) {
case 1:
//System.out.println("给所有数据量赋值");
//调用给所有数据的数量与金额赋值方法
enterNumber();
break;
case 2:
//调用打印购物小票的方法
System.out.println("打印购物小票");
break;
case 3:
System.out.println("欢迎下次光临");
//退出程序
System.exit(0);
default:
System.out.println("请输入正确的数字!");
break;
}
}
}
/**
* @Title: initData
* @Description: 定义方法,向成员位置的集合赋值
*/
public static void initData() {
//创建多个商品项对象
GoodsItem sls = new GoodsItem("少林寺核桃", "090115", 15.5, 0, "个", 0);
GoodsItem shk = new GoodsItem("尚康饼干", "090027", 14.5, 0, "个", 0);
//将多个商品项对象放入集合中
data.add(sls);
data.add(shk);
}
/**
* @Title: enterNumber
* @Description: 为所有的数据赋值数量与金额
*/
public static void enterNumber() {
//集合的遍历
for (int i = 0; i <data.size(); i++) {
//依次获取到集合中每一个元素
GoodsItem thisGoods = data.get(i);
//提示用户,输入该商品项的数量
//获取该商品项名称
String thisGoodsName = thisGoods.getName();
System.out.println("请输入"+thisGoodsName+"的购买数量");
//键盘录入接受商品数量
Scanner sc = new Scanner(System.in);
int thisGoodsNumber = sc.nextInt();
//根据数量计算金额金额 = 单价 * 数量
double thisGoodsMoney = thisGoods.getPrice() * thisGoodsNumber;
//为该商品的数量与金额赋值
thisGoods.setNumber(thisGoodsNumber);
thisGoods.setMoney(thisGoodsMoney);
}
}
}
1.7超市购物小票打印小票
定义打印超市购物小票方法完成小票打印功能
package com.igeek_07;
import java.util.ArrayList;
import java.util.Scanner;
import com.igeek_03.GoodsItem;
/**
* @ClassName: ShoppingReceipt
* @Description: 超市购物小票类
* @date 2017年11月13日 上午10:48:20
* Company www.igeekhome.com
*
*/
public class ShoppingReceipt {
/**
* @Fields data : 成员位置定义集合,存储所有的商品项对象
*/
static ArrayList<GoodsItem> data = new ArrayList<GoodsItem>();
public static void main(String[] args) {
//为集合准备数据
initData();
//打印欢迎语句
System.out.println("欢迎使用超市管理系统!");
//使用死循环完成反复操作的逻辑
//for(;;) {}
while(true) {
//使用switch给出操作选择:1输入购买数量2打印小票3退出
//提示信息
System.out.println("请输入您要进行的操作:1输入购买数量 2打印小票 3退出");
//通过键盘录入得到数据
Scanner sc = new Scanner(System.in);
int optNumber = sc.nextInt();
//完成switch中三个case的逻辑
switch (optNumber) {
case 1:
//System.out.println("给所有数据量赋值");
//调用给所有数据的数量与金额赋值方法
enterNumber();
break;
case 2:
//System.out.println("打印购物小票");
//调用打印购物小票的方法
printReceipt();
break;
case 3:
System.out.println("欢迎下次光临");
//退出程序
System.exit(0);
default:
System.out.println("请输入正确的数字!");
break;
}
}
}
/**
* @Title: initData
* @Description: 定义方法,向成员位置的集合赋值
*/
public static void initData() {
//创建多个商品项对象
GoodsItem sls = new GoodsItem("少林寺核桃", "090115", 15.5, 0, "个", 0);
GoodsItem shk = new GoodsItem("尚康饼干", "090027", 14.5, 0, "个", 0);
//将多个商品项对象放入集合中
data.add(sls);
data.add(shk);
}
/**
* @Title: enterNumber
* @Description: 为所有的数据赋值数量与金额
*/
public static void enterNumber() {
//集合的遍历
for (int i = 0; i <data.size(); i++) {
//依次获取到集合中每一个元素
GoodsItem thisGoods = data.get(i);
//提示用户,输入该商品项的数量
//获取该商品项名称
String thisGoodsName = thisGoods.getName();
System.out.println("请输入"+thisGoodsName+"的购买数量");
//键盘录入接受商品数量
Scanner sc = new Scanner(System.in);
int thisGoodsNumber = sc.nextInt();
//根据数量计算金额金额 = 单价 * 数量
double thisGoodsMoney = thisGoods.getPrice() * thisGoodsNumber;
//为该商品的数量与金额赋值
thisGoods.setNumber(thisGoodsNumber);
thisGoods.setMoney(thisGoodsMoney);
}
}
/**
* @Title: printReceipt
* @Description: 打印小票方法
*/
private static void printReceipt() {
//static ArrayList<GoodsItem> data = new ArrayList<GoodsItem>();
//票头
System.out.println(" 欢迎光临");
System.out.println("品名\t售价\t数量\t单位\t金额");
System.out.println("-------------------------------------------");
//票体
//定义变量,记录所有的商品数量
int totalNumber = 0;
//定义变量,记录所有的商品金额
double totalMoney = 0;
//遍历集合
for (int i = 0; i <data.size(); i++) {
//依次获取每一个商品项
GoodsItem g = data.get(i);
//打印商品项
System.out.println(g.getName()+g.getId()+"\t"+g.getPrice()+"\t"+g.getNumber()+" "+g.getUnit()+"\t"+g.getMoney());
//累加数量与金额
totalNumber += g.getNumber();
totalMoney += g.getMoney();
}
System.out.println("-------------------------------------------");
//票脚
System.out.println("共"+data.size()+"项商品");
System.out.println("共"+totalNumber+"件商品");
System.out.println("共"+totalMoney+"元");
System.out.println();
}
}