用Java做一个简易购物车

 主函数

package gouwuche;

import gouwuche.Goods;

import java.util.Scanner;

public class ShopCar {
    public static void main(String[] args){
        Goods[] shopCar=new Goods[100];
        while(true){
        System.out.println("请您选择如下命令进行操作:");
        System.out.println("添加商品到购物车:add");
        System.out.println("查询购物车商品展示:query");
        System.out.println("修改商品数量:update");
        System.out.println("结算购买商品的金额:pay");
        Scanner sc=new Scanner(System.in);

            System.out.println("请您输入命令:");
            String command=sc.next();
            switch (command){
                case"add":
                    addGoods(shopCar,sc);
                    break;
                case "query":
                    queryGoods(shopCar,sc);
                    break;
                case "update":
                    updateGoods(shopCar,sc);
                    break;
                case "pay":
                    payGoods(shopCar,sc);
                    break;
            }
        }
    }
    public static void addGoods(Goods[] shopCar,Scanner sc){
        System.out.println("请输入你要买的商品编号:");
        int id=sc.nextInt();
        System.out.println("请您输入购买商品的名称:");
        String name=sc.next();
        System.out.println("请您输入购买商品的数量:");
        int buyNumber=sc.nextInt();
        System.out.println("请您输入商品的单价:");
        double price=sc.nextDouble();
        Goods g=new Goods();
        g.setId(id);
        g.setName(name);
        g.setBuyNumber(buyNumber);
        g.setPrice(price);
        for(int i=0;i

 购物车类:

package gouwuche;

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



    public Goods(){

    }
    public Goods(int id,String name,double price,int buyNumber){
        this.id=id;
        this.name=name;
        this.price=price;
        this.buyNumber=buyNumber;
    }
    public int getId(){
        return id;
    }
    public void setId(int id){
        this.id=id;
    }

    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;
    }
}

 

 

你可能感兴趣的:(Java小实例,java)