Java学习日记 2022.6.28


1.数据类型

基本数据类型的变量只能指向常量池引用数据类型的变量(指针,引用)只能指向堆既可以指向常量池又可以指向堆
byteint[]Byte
shortbyte[]Short
intshort[]Integer
longStudentLong
floatFloat
doubleDouble
charCharacter
booleanBoolean
String

数据类型 变量名 = new 构造方法() — 指向堆
数据类型 变量名 = 直接量 — 指向常量池

基本数据类型
4类8种

引用数据类型
1、类
2、数组

既可以当基本数据类型又可以当引用数据类型的有几种
9个
8个与基本数据类型相对应的包装类
1个String类

String的底层是什么?
JDK1.8之前是char[];
JDk1.8之后是byte[];

String类的成员变量
在这里插入图片描述

2.简单程序练习

题目要求

汽车租赁系统

汽车类Car
属性
汽车品牌 brand
汽车型号 model
颜色    color  
租车人  leaser  
日租金   rent
租借天数 day
状态    state

成员方法show()

不定义构造方法, 构造对象用默认的无参构造方法, 对象中属性的读写全部用设值器和读值器

setter/getter方法

测试类
静态变量
控制台输入扫描器对象
长度为10的Car数组

主函数main
	菜单
	1. 添加汽车
	2. 删除汽车
	3. 查看汽车
	4. 汽车出租
	5. 汽车归还
	6. 退出

Car类

package rentcar;

public class Car {
    private String brand;
    private String model;
    private String color;
    private String leaser;
    private Double rent;
    private Integer day;
    private Integer state;

    public void show(){
        System.out.println("**************************");
        System.out.print("汽车品牌:" + this.brand);
        System.out.print("\t型号:" + this.model);
        System.out.print("\t颜色:" + this.color);
        System.out.print("\t状态:" + (this.state == 0 ? "未出租" : "已出租"));
        System.out.println("\n**************************");
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getLeaser() {
        return leaser;
    }

    public void setLeaser(String leaser) {
        this.leaser = leaser;
    }

    public Double getRent() {
        return rent;
    }

    public void setRent(Double rent) {
        this.rent = rent;
    }

    public Integer getDay() {
        return day;
    }

    public void setDay(Integer day) {
        this.day = day;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }
}

测试类

package rentcar;

import java.util.Scanner;

public class Demo {
    static Scanner sc = new Scanner(System.in);
    static Car[] carArr = new Car[10];

    public static void main(String[] args) {
        int choice;
        do {
            System.out.println("***欢迎使用汽车租赁系统***");
            System.out.println("******1.添加汽车********");
            System.out.println("******2.删除汽车********");
            System.out.println("******3.查看汽车********");
            System.out.println("******4.汽车出租********");
            System.out.println("******5.汽车归还********");
            System.out.println("******6.推出***********");
            System.out.println("请输入进行选择:");
            choice = sc.nextInt();
            switch (choice) {
                case 1:
                    addCar();
                    break;
                case 2:
                    delCar();
                    break;
                case 3:
                    display();
                    break;
                case 4:
                    rentCar();
                    break;
                case 5:
                    backCar();
                    break;
                case 6:
                    System.out.println("***汽车租赁系统已退出!***");
                    break;
                default:
                    System.out.println("输入不合法!!!");
                    break;
            }
        } while (choice != 6);

    }

    private static void backCar() {
        System.out.println("******进入汽车归还***********");
        display();
        int choice;
        System.out.println("请选择你需要归还的汽车(1`10)");
        choice = sc.nextInt() -1 ;
        Car car = carArr[choice];
        if (car == null) {
            System.out.println("该汽车不存在,请重新选择!");
        }else if (car.getState() == 0){
            System.out.println("对不起,该车未出租,不能归还!");
        }else {
            System.out.println(choice + "号汽车归还成功!你需要支付租金:"+car.getRent() * car.getDay() + "元");
            car.setLeaser(null);
            car.setRent(null);
            car.setDay(null);
            car.setState(0);
        }
    }

    private static void rentCar() {
        System.out.println("******进入汽车租接***********");
        display();
        int choice;
        System.out.println("请选择需要租接的汽车(1`10)");
        choice = sc.nextInt() - 1;
        Car car = carArr[choice];
        if (car == null){
            System.out.println("该汽车不存在,请重新选择!");
        }else if (car.getState() == 1){
            System.out.println("对不起,该车已出租,不能再租界了!");
        } else {
            System.out.println("请输入租界人姓名:");
            car.setLeaser(sc.next());
            System.out.println("请输入日租金:");
            car.setRent(sc.nextDouble());
            System.out.println("请输入租界天数:");
            car.setDay(sc.nextInt());
            car.setLeaser(car.getLeaser());
            car.setRent(car.getRent());
            car.setDay(car.getDay());
            car.setState(1);
            System.out.println(car.getBrand() + car.getModel() + "出租成功!");
        }

    }

    private static void display() {
        System.out.println("******进入汽车展示***********");
        for (int i = 0; i < carArr.length; i++) {
            System.out.println((i + 1) + "号汽车");
            if (carArr[i] != null){
                carArr[i].show();
            }else{
                System.out.println("空");
            }
        }
    }

    private static void delCar() {
        System.out.println("******进入删除汽车***********");
        display();
        int choice;
        System.out.println("请选择需要删除的汽车(1~10):");
        choice = sc.nextInt();
        if (carArr[choice-1].getState() == 1){
            System.out.println("对不起,该车已出租,不能删除!");
        }else {
            for (int i = choice-1; i < carArr.length-1; i++) {
                carArr[i] = carArr[i + 1];
            }
            carArr[carArr.length-1] = null;
            System.out.println(choice + "号汽车删除成功!");
        }
    }

    private static void addCar() {
        System.out.println("******添加汽车***********");
        System.out.println("请输入汽车品牌:");
        Car car = new Car();
        car.setBrand(sc.next());
        System.out.println("请输入汽车型号:");
        car.setModel(sc.next());
        System.out.println("请输入汽车颜色:");
        car.setColor(sc.next());
        System.out.println("请输入汽车状态:");
        car.setState(sc.nextInt());
        for (int i = 0; i < carArr.length; i++) {
            if (carArr[i] == null){
                carArr[i] = car;
                System.out.println(car.getBrand() + car.getModel() + "入库成功!");
                break;
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值