1.1  超市购物购物小票需求分析

用户循环进行三个操作:

1、输入购买数量,代表为所购买货物的数量赋值,从而计算每项商品金额

2、打印小票,将已有数据打印

3、退出系统(因为该程序为循环操作,无法终止,如果不想再进行操作,则退出系统)

1.1.1 案例代码一

public class GoodsItem {
                     //成员变量
                     private String name;   //名称
                     private String id;     //编号
                     private double price;  //单价
                     private int num;       //数量
                     private String unit;   //计价单位
                     private double money;  //金额
 
 //有参构造方法
 public GoodsItem(String name, String id, double price, int num, String unit, double money) {
            super();
            this.name = name;
            this.id = id;
            this.price = price;
            this.num = num;
            this.unit = unit;
            this.money = money;
 }
 //无参构造方法
 public GoodsItem() {
  super();
  // TODO Auto-generated constructor stub
 }
 
 //取值(get)和赋值(set)方法
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public double getPrice() {
  return price;
 }
 public void setPrice(double price) {
  this.price = price;
 }
 public int getNum() {
  return num;
 }
 public void setNum(int num) {
  this.num = num;
 }
 public String getUnit() {
  return unit;
 }
 public void setUnit(String unit) {
  this.unit = unit;
 }
 public double getMoney() {
  return money;
 }
 public void setMoney(double money) {
  this.money = money;
 }
            }
 
1.2  打印超市购物小票
import java.util.ArrayList;
import java.util.Scanner;
public class supermaket {
/**
*@Fields data :成员位置定义集合,存储所有的商品项对象
*/                    
          static ArrayList<GoodsItem> data=new ArrayList<GoodsItem>();
                       public static void main(String[] args) {
                       //为集合准备数据  
                       initData();
                       //打印欢迎语句
                       System.out.println("欢迎进入超市购物系统!");
  
while(true) {
                 //使用switch给出操作选择
                 System.out.println("请输入你要进行的操作:1输入购物数量 2打印小票 3退出");
                 //通过键盘录入得到数据
                 Scanner sc=new Scanner(System.in);
                 int optNumber=sc.nextInt();
  
          switch(optNumber) {
          case 1:   //给所有数据量赋值,调用给所有数据的数量与金额赋值方法
                        enterNumber();
                        break;
          case 2:  //打印购物小票,调用打印购物小票的方法
                        printReceipt();
                        break;
          case 3:
                      System.out.println("谢谢您的使用");
                      //退出程序         
                     System.exit(0);
         default:
                    System.out.println("请输入正确的数字!");
                    break;
               }
         }
}
//定义方法,向成员位置的集合赋值
 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);
 }
 
//为所有的数据赋值数量和金额
  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.setNum(thisGoodsNumber);
                                 thisGoods.setMoney(thisGoodsMoney);
                   }
  }
 
//打印小票的方法
 private static void printReceipt() {
                       //票头
                       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.getNum()+"\t"+g.getUnit()+"\t"+g.getMoney());
                           
                                //累加数量和金额  
                                totalNumber+=g.getNum();
                                totalMoney+=g.getMoney();
               }
             System.out.println("------------------------");
             //票脚
             System.out.println("共"+data.size()+"项商品");
             System.out.println("共"+totalNumber+"件商品");
             System.out.println("共"+totalMoney+"元");
             System.out.println();
       }
}

学习更多基础Java教程可以在这个网站学习:http://how2j.cn/p/2099

最新文章

  1. 共享Excel编辑的一些资源
  2. DOM对象—选中执行效果
  3. qt越来越好了
  4. stream的seek方法实例
  5. Linux常用命令大全(share)
  6. 在Debian下编译Postgresql
  7. AR增强现实特点、关键技术和应用
  8. Nagios+msn+fetion自定义时间发送报警消息
  9. [LeetCode#260]Single Number III
  10. 委托 delegate, 继承
  11. kernal linear regression
  12. 关于npm安装全局模块,require时报Error: Cannot find module &#39;XXX&#39;的解决办法
  13. C#中使用SHA1和MD5加密字符串
  14. Sql Server——查询(二)
  15. jQuery禁用、开启鼠标滚轮事件
  16. 数学——Euler方法求解微分方程详解(python3)
  17. C#语言————第二章 C#语言快速热身
  18. Expires和Cache-Control的理解
  19. 网络存储结构简明分析—DAS、NAS和SAN 三者区别
  20. 理解 Redis(1) - Redis 简介

热门文章

  1. iOS 浅谈MVC设计模式及Controllers之间的传值方式
  2. 行转列--Excel和数据库的完美结合
  3. Hadoop 解除 “Name node is in safe mode”
  4. Responsive Nav
  5. 每天进步一点点—mysql-mysqldump
  6. C # 踩坑记录(20190603)
  7. HDU1257 最少拦截系统 —— 贪心
  8. div+css布局教程系列1
  9. pycharm中关于django和buildout的配置
  10. CodeForces 723C Polycarp at the Radio (题意题+暴力)