继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java入门第二季 第6章 项目实战

Nirva_Troy
关注TA
已关注
手记 4
粉丝 12
获赞 51
Java入门第二季 第6章 项目实战 手记

哒哒租车系统

  • 客户端
package rentCar;
import java.util.Scanner;

public class Client {

    public static void main(String[] args) {
        System.out.println("欢迎来到哒哒租车系统:");
        System.out.println("");
        System.out.println("是否租车  【0】是  【1】 否");
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);

        if(sc.nextInt() == 0){
            Rent rent1 = new Rent();
            rent1.show();
            System.out.println("请输入您要租汽车的数量:"); 
            rent1.rentVehicle(sc.nextInt());    //调用租车程序
        }
        System.out.println("谢谢光临!");
    }

}
  • 租赁系统
package rentCar;
import java.util.Scanner;

public class Rent {
    private Vehicle[] vehicle = new Vehicle[6];
    {  //定义车库
        vehicle[0] = new Car("奥迪A4",500.0F,4);
        vehicle[1] = new Car("马自达A6",400.0F,4);
        vehicle[2] = new Vehicle("皮卡雪6",450.0F,4,2.0F);
        vehicle[3] = new Car("金龙",800.0F,20);
        vehicle[4] = new Truck("松花江",400.0F,2.0F);
        vehicle[5] = new Truck("依维柯",900.0F,20.0F);
    };

    //展示车辆信息
    public void show(){
         System.out.println("您可租车的类型及其价目表:");
         System.out.println("序号\t汽车名称\t租金\t\t容量");
        for(int i = 0; i <vehicle.length; i++) {
            System.out.println(vehicle[i].toString());
        }
        System.out.println("-----------------------\n");
    }

    //选择车辆
    public void rentVehicle(int num) {
        int[] nums = new int[num];  //声明 要租用车的序号数组
        @SuppressWarnings("resource")
        Scanner sc1 = new Scanner(System.in);
        for (int i = 0; i < num; i++) {
            System.out.println("请输入第" + (i+1) + "辆车的序号:");
            nums[i] = sc1.nextInt() -1; //实例化 要租用车的序号数组
        }
        System.out.println("请输入租车天数:");
        int day = sc1.nextInt();

        System.out.println("您的账单:");
        car(nums);
        truck(nums);
        money(day,nums);

    }

    private void money(int day, int[] nums) {
        int money = 0;
        for(int i = 0; i<nums.length;i++) {
            money += vehicle[nums[i]].getPrice();
        }
        System.out.println("***租车总价格:" + (money*day) + "元");
    }

    private void truck(int[] nums) {
        int totalload = 0;
        System.out.print("***可载货的车有:  ");
        for(int i = 0;i<nums.length;i++) {
            if(vehicle[nums[i]].getLoad() == 0) continue;
            System.out.print(vehicle[nums[i]].getName() + "  ");
            totalload += vehicle[nums[i]].getLoad();
        }
        System.out.println("\n最多载重:" + totalload + "吨");
    }

    private void car(int[] nums) {
        int totalseat = 0;
        System.out.print("***可载人的车有:  ");
        for(int i = 0;i<nums.length;i++) {
            if(vehicle[nums[i]].getSeat() == 0) continue;
            System.out.print(vehicle[nums[i]].getName() + "  ");
            totalseat += vehicle[nums[i]].getSeat();
        }
        System.out.println("\n最多共载人:" + totalseat + "人");
    }
}
  • 车类,父类
package rentCar;

public class Vehicle {
    private String name;
    private int id;
    private float price;
    private static int count;
    private int seat;
    private float load;
    public Vehicle(){
    }
    //载人车  构造
    public Vehicle(String name,float price,int seat){
        this.name = name;
        this.price = price;
        this.seat = seat;
        count++;
        this.id = count;    //使用静态变量设置车辆编号,只设置getter方法,用于获取
    }
    //载货车  构造
    public Vehicle(String name,float price,float load){
        this.name = name;
        this.price = price;
        this.load = load;
        count++;
        this.id = count;
    }
    // 既有载人又有载货  构造
    public Vehicle(String name,float price,int seat,float load){
        this.name = name;
        this.price = price;
        this.load = load;
        this.seat = seat;
        count++;
        this.id = count;
    }
    //toString 用于返回 字符串类型的 车辆信息,便于输出
    public String toString(){
        return this.id + ".\t" + this.name + "\t" + this.price + "¥\t\t" + this.seat + "人" + "\t" + this.load + "吨";
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    public float getPrice(){
        return price;
    }
    public void setLoad(float load) {
        this.load = load;
    }
    public float getLoad(){
        return load;
    }
    public void setSeat(int seat) {
        this.seat = seat;
    }
    public int getSeat() {
        return seat;
    }
    public int getID(){
        return id;
    }
}
  • 载人车 子类
package rentCar;

public class Car extends Vehicle {

    // 继承父类 Vehicle中载人车的方法
    public Car(String name, float price, int seat) {
        super(name, price, seat);
    }
    // 覆写 父类Vehicle中 toString()方法
    public String toString() {
        return super.getID() + ".\t" + super.getName()+ "\t" + super.getPrice() + "¥\t\t" + super.getSeat() + "人";
    }

}
  • 载货车 子类
package rentCar;

public class Truck extends Vehicle {

    // 继承父类 Vehicle中 载货车的方法
    public Truck(String name, float price, float load) {
        super(name, price, load);
    }
    // 覆写 父类Vehicle中 toString()方法
    public String toString() {
        return super.getID() + ".\t" + super.getName() + "\t" + super.getPrice() + "¥\t\t" + super.getLoad() + "吨";
    }

}
打开App,阅读手记
7人推荐
发表评论
随时随地看视频慕课网APP