Java 入门16 面向对象实现之前的小案例购物车

2022/6/23 5:19:52

本文主要是介绍Java 入门16 面向对象实现之前的小案例购物车,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

Goods 商品类

package com.heima.Shop;

public class Goods {
    private String name;
    private double price;
    private int  buyNumber;

    //生成 无参构造器

    public Goods() {
    }

    //生成有参构造器
    public Goods(String name, double price, int buyNumber) {
        this.name = name;
        this.price = price;
        this.buyNumber = buyNumber;
    }

    //生成get/set方法

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getBuyNumber() {
        return buyNumber;
    }

    public void setBuyNumber(int buyNumber) {
        this.buyNumber = buyNumber;
    }
}
View Code

Test 测试类

package com.heima.Shop;

import com.heima.scanner.Scan;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.Scanner;

//测试类
public class Test {
    public static void main(String[] args) {
        //创建数组 存储添加的商品
        Goods[] arrShop = new Goods[10];
        //获取输入流对象
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("add添加商品");
            System.out.println("query查询商品");
            System.out.println("update更新商品数量");
            System.out.println("pay付款");

            //获取输入的指令
            String command = sc.next();
            System.out.println(command);
            //创建Goods实例
            //switch 判断指令
            switch (command) {
                case "add":
                    //执行添加商品的指令
                    addGoods(arrShop);
                    System.out.println("add 指令执行成功");
                    break;
                case "query":
                    queryGoods(arrShop);
                    System.out.println("query指令执行成功");
                    break;
                case "update":
                    updateGoods(arrShop);
                    System.out.println("update指令执行成功");
                    break;
                case "pay":
                    payGoods(arrShop);
                    System.out.println("pay 指令执行成功");
                    break;
                default:
                    System.out.println("指令有错误");
            }
        }

    }

    private static void payGoods(Goods[] arrShop) {
        //存储计算的总金额
        double allPrice = 0;
        for (int i = 0; i < arrShop.length; i++) {
            if (arrShop[i] != null) {
                allPrice += (arrShop[i].getPrice() * arrShop[i].getBuyNumber());
            } else {
                break;
            }
        }
        System.out.println("付款金额" + allPrice);
    }

    private static void updateGoods(Goods[] arrShop) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入要修改的商品名称");
        String name = sc.next();
        for (int i = 0; i < arrShop.length; i++) {
            if (arrShop[i] != null) {
                if (arrShop[i].getName().equals(name)) {
                    System.out.println("请输入要修改的数量");
                    int buyNumber = sc.nextInt();
                    arrShop[i].setBuyNumber(buyNumber);
                    System.out.println(arrShop[i].getName() + "现在数量是" + arrShop[i].getBuyNumber());
                    break;
                }
            } else {
                System.out.println("没找到指定的商品");
                break;
            }
        }
    }

    private static void queryGoods(Goods[] arrShop) {
        //获取要查询的商品名称
        for (int i = 0; i < arrShop.length; i++) {
            if (arrShop[i] != null) {
                System.out.println("商品名称 : " + arrShop[i].getName() + "商品价格" + arrShop[i].getPrice() + "商品数量" + arrShop[i].getBuyNumber());
            } else {
                break;
            }
        }
    }

    private static void addGoods(Goods[] arrShop) {
        System.out.println(111);
        System.out.println("请输入name");
        Scanner sc = new Scanner(System.in);
        //获取name
        String name = sc.next();
        System.out.println("请输入price");
        //获取price
        double price = sc.nextDouble();
        System.out.println("请输入buyNumber");
        //获取数量
        int buyNumber = sc.nextInt();
        //实例化Goods对象
        Goods a = new Goods(name, price, buyNumber);

        //循环遍历arrShop 如果值为null 则添加商品
        for (int i = 0; i < arrShop.length; i++) {
            if (arrShop[i] == null) {
                arrShop[i] = a;
                break;
            }
        }
    }
}
View Code

 



这篇关于Java 入门16 面向对象实现之前的小案例购物车的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程