mt_Day3:面向对象基础

面向对象

1.案例练习:购物车对象

商品类

public class goods {
    int id;
    String name;
    double price;
    int buyNums;
}

购物车 功能

1.添加商品到购物车

2.查询购物车中商品,并展示

3.修改购买数量

4.结算金额

package com.xuexi.shopCarTest;

import java.util.Scanner;

public class shopCarTest {
    public static void main(String[] args) {
        goods[] shopcar=new goods[100]; //装商品类的地址,初始为{null,null,……}
        //添加功能
        while (true) {
            System.out.println("请您选择如下功能操作:");
            System.out.println("添加商品到购物车:add");
            System.out.println("查询购物车中商品,并展示:query");
            System.out.println("修改购买数量:update");
            System.out.println("结算金额:pay");
            System.out.println("请输入操作命令:");
            Scanner sc=new Scanner(System.in);
            String command=sc.next();

            switch (command){
                case "add":
                    addGoods(shopcar,sc);
                    break;
                case "query":
                    queryGoods(shopcar);
                    break;
                case "update":
                    updateGoods(shopcar,sc);
                    break;
                case "pay":
                    payGoods(shopcar);
                    break;
                default:
                    System.out.println("没有该指令");
            }
        }
    }

    private static void payGoods(goods[] shopcar) {
        double money=0;
        for (int i = 0; shopcar[i]!=null; i++) {
            goods g=shopcar[i];
            money+=g.price*g.buyNums;
        }
        System.out.println("总价格为:"+money);
    }

    private static void updateGoods(goods[] shopcar,Scanner sc) {
        //输入商品id,通过id修改数量
        while (true) {
            System.out.println("请输入要修改购买数量的商品的编号:");
            int id=sc.nextInt();
            goods g=getId(shopcar,id);
            if(g==null){
                System.out.println("购物车中没有该商品");
            }else{
                System.out.println("找到商品【"+g.name+"】,原数量为:"+g.buyNums);
                System.out.println("请输入修改的数量:");
                int n=sc.nextInt();
                g.buyNums=n;
                System.out.println("修改成功");
                queryGoods(shopcar);
                break;
            }
        }
    }
    public static goods getId(goods[] shopcar,int id){
        //goods index=-1;
        goods g=null;
        for (int i = 0; shopcar[i]!=null; i++) {
            if(id==shopcar[i].id){
                g=shopcar[i];
                return g;
            }
        }
        return null;
    }

    private static void queryGoods(goods[] shopcar) {
        System.out.println("==========购物车中商品清单如下=========");
        System.out.println("编号\t\t名称\t\t\t价格\t\t数量\t\t");
        for (int i = 0; shopcar[i]!=null; i++) {
            System.out.println(shopcar[i].id+"\t\t"+shopcar[i].name+"\t\t\t"+
                    shopcar[i].price +"\t\t"+shopcar[i].buyNums);
        }
    }

    private static void addGoods(goods[] shopcar,Scanner sc) {
        //1.记录下用户购买信息
        System.out.println("======输入商品信息======");
        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 buyNums=sc.nextInt();

        //2.将记录下来的信息封装一个商品对象
        goods g1=new goods();
        g1.id=id;
        g1.name=name;
        g1.price=price;
        g1.buyNums=buyNums;

        //3.将商品对象加到购物车数组中去
        for (int i = 0; i < shopcar.length; i++) {
            if(shopcar[i]==null){
                shopcar[i]=g1;  //存入的是商品对象的地址
                break;  //已经找到空位置,停止遍历数组
            }
        }
        System.out.println("您的商品"+g1.name+"已经加入购物车。");
    }
}

2.构造器

初始化一个对象,并返回对象的地址

1.无参构造:默认存在,初始化对象时,成员变量数据均采用默认值

2.含参构造:接收参数,为对象赋值

    public Car(){
        System.out.println("无参构造器被调用");
    }
    public Car(String name,double price){
        this.name=name;
        this.price=price;
        System.out.println("有参构造器被调用");
    }

3.封装

面向对象三大特征:封装、继承、多态

基本思想:决定属性和行为归属谁的问题

封装原则:对象代表什么,就得封装对应的数据,并提供对应数据的行为(例子:人画圆,画画行为,要封在圆类中,因为要调用半径成员变量)

4.JavaBean

也称实体类(例:人类、汽车类),其对象可以用于在程序中封装数据。

书写要求:

1成员变量使用private修饰

2提供成员变量对应的getter、setter方法

3必须提供无参构造器;有参构造器可写可不写

public class User {
    //成员变量建议使用private私有
    private String name;
    private double height;
    private double salary;
    
    //必须提供无参构造,有参构造随意
	public User() {
    }

    public User(String name, double height, double salary) {
        this.name = name;
        this.height = height;
        this.salary = salary;
    }

    //必须为成员变量提供getter和setter方法
    public String getName() {
        return name;
    }

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

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}
posted @ 2023-01-10 22:23  NIka_mt  阅读(26)  评论(0)    收藏  举报