手记

java练习

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Car a = new PassengerCar(1, "奥迪A4", 500, 4);
        Car b = new PassengerCar(2, "马自达6", 400, 4);
        Car c = new PkCar(3, "皮卡路6", 4500, 2, 4);
        Car d = new PassengerCar(4, "金龙", 800, 20);
        Car e = new Truck(5, "松花江", 400, 4);
        Car f = new Truck(6, "依维柯", 1000, 20);
        //数据存放在数组Car里面
        Car[] car = {a, b, c, d, e, f};
        System.out.println("欢迎使用哒哒租车系统");
        System.out.println("您是否要租车:1yes 2no");
        boolean x=true ;
        while (x) {
            //控制台输入
            int n = in.nextInt();//键盘输入数字赋值给a
            if (n == 1) {
                x=true;
                System.out.println("您可以租车的类型及价目表");
                System.out.println("序号" + "\t" + "汽车名称" + "\t" + "租金" + "\t" + "容量");
                for (int i = 0; i < car.length; i++) {
                    car[i].print();
                }
            } else if (n == 2) {
                System.out.println("感谢您的使用!\n是否重新使用租车系统:1yes 2no");
                if(in.nextInt()==1){
                    x=true;
                    System.out.println("您可以租车的类型及价目表");
                    System.out.println("序号" + "\t" + "汽车名称" + "\t" + "租金" + "\t" + "容量");
                    for (int i = 0; i < car.length; i++) {
                        car[i].print();
                    }
                }
                else {
                    System.out.println("欢迎您下次使用!");
                    return;
                }

            } else {
                System.out.println("请输入正确有效的数字!");
                continue;
            }
            System.out.println("请输入要租的车辆数量");
            int sumP = 0;
            int sumPC = 0;
            int sumCC = 0;
            int m = in.nextInt();
            for (int j = 0; j < m; j++) {
                System.out.println("请输入第" + (j + 1) + "辆车的序号:");
                int id = in.nextInt();
                sumP += car[id - 1].price;
                sumPC += car[id - 1].passengerCapacity;
                sumCC += car[id - 1].passengerCapacity;
                car[j] = car[id - 1];
            }
            System.out.println("请输入租车的天数:");
            double tsum = sumP * in.nextInt();
            System.out.println("您的账单R如下");
            System.out.println("可载人的车有:");
            for (int k = 0; k < m; k++) {
                if (car[k].passengerCapacity != 0) {
                    System.out.println(car[k].name);
                }
            }
            System.out.println("可载货的车有:");
            for (int k = 0; k < m; k++) {
                if (car[k].cargoCapacity != 0) {
                    System.out.println(car[k].name);
                }
            }
            System.out.println("总载人量:" + sumPC + "人");
            System.out.println("总载货量:" + sumCC + "吨");
            System.out.println("总价格" + tsum + "元");
            System.out.println("欢迎您下次使用!");
            return;
        }
    }
}


package com.demo4;

public class Car {
    public int id;
    public String name;
    public double price;
    public int passengerCapacity;
    public int cargoCapacity;
    public void print(){

    }
}


public class PkCar extends Car {
    public PkCar(int id,String name,double price,int passengerCapacity,int cargoCapacity){
        this.id=id;
        this.name=name;
        this.price=price;
        this.passengerCapacity=passengerCapacity;
        this.cargoCapacity=cargoCapacity;
    }
    public void print(){
        System.out.println(id+"\t"+name+"\t"+price+"元/天"+"\t"+"载客"+passengerCapacity+"人"+"载货"+cargoCapacity+"吨");
    }
}


package com.demo4;

public class Truck extends Car {
    public Truck(int id,String name,double price,int cargoCapacity){
        this.id=id;
        this.name=name;
        this.price=price;
        this.cargoCapacity=cargoCapacity;
    }
    public void print(){
        System.out.println(id+"\t"+name+"\t"+price+"元/天"+"\t"+"载货"+cargoCapacity+"吨");
    }
}


public class PassengerCar extends Car {
    public PassengerCar(int id,String name,double price,int passengerCapacity){
        this.id=id;
        this.name=name;
        this.price=price;
        this.passengerCapacity=passengerCapacity;
    }
    public void print(){
        System.out.println(id+"\t"+name+"\t"+price+"元/天"+"\t"+"载客"+passengerCapacity+"人");
    }
}



0人推荐
随时随地看视频
慕课网APP