ITPub博客

首页 > IT职业 > IT职场 > Java入门 第二季 租车系统

Java入门 第二季 租车系统

IT职场 作者:danan 时间:2021-09-09 08:57:02 0 删除 编辑

欢迎沟通交流@__@

运行效果:

图片描述
图片描述

Car.java //车基类
public abstract class Car {
    public String name; //车名
    public int price; //租金 元/天
    public int manCap;//载人
    public int goodsCap;//载货
    public Car(String name, int price) {
        this.name = name;
        this.price = price;
    }
}
MGCar.java //载客载货车
public class MGCar extends Car {
    public MGCar(String name, int price, int manCap, int goodsCap) {
        super(name, price);
        this.manCap = manCap;
        this.goodsCap = goodsCap;
    }
    @Override
    public String toString() {
        return ""+name+"t   "+price+"元/天t载人:"+manCap+"人t载货:"+goodsCap+"吨";
    }
}
ManCar.java //载客车
public class ManCar extends Car {
    public ManCar(String name, int price, int manCap) {
        super(name, price);
        this.manCap = manCap;
    }
    @Override
    public String toString() {
        return ""+name+"t   "+price+"元/天t载人:"+manCap+"人t";
    }
}
GoodsCar.java //载货车
public class GoodsCar extends Car {
    public GoodsCar(String name,int price,int goodsCap){
        super(name,price);
        this.goodsCap = goodsCap;
    }
    @Override
    public String toString() {
        return  ""+name+"t   "+price+"元/天t载货:"+goodsCap+"吨t";
    }
}
CarRentSys.java //租车系统入口
import java.util.Arrays;
import java.util.Scanner;
/**
 * 哒哒租车系统
 * @author Anders
 *
 */
public class CarRentSys {

    public static void main(String[] args) {
        int first = 0;//首次
        int[] rentNum = new int[100];//租车代码
        int rentDays = 0;//租车天数
        Car[] cars = {new ManCar("奥迪A4", 500, 4),new ManCar("马自达6", 400, 4),new MGCar("皮卡雪6", 450, 4, 2),
                new ManCar("金龙", 800, 20),new GoodsCar("松花江", 400, 4),new GoodsCar("依维柯", 1000, 20)};
        if(first == 0){
            System.out.println("~~~~~~~~~~欢迎使用哒哒租车系统~~~~~~~~~~");
        }
        Scanner input = new Scanner(System.in);
        System.out.println("您是否要租车?t1是t0否,请输入对应数字:");
        while(first == 0){
            first++;
            String sel = input.next();
            if("1".equals(sel) || "0".equals(sel)){
                if("1".equals(sel)){
                    showProduct(cars);
                    select(input,cars,rentNum);
                    Arrays.sort(rentNum);
                    System.out.println("请输入租车天数:");
                    while(first==1){
                        first++;
                        try{
                            rentDays = input.nextInt();
                        }catch(Exception e){
                            System.out.println("输入有误,请重新输入:");
                            first = 1;
                            input.next();
                        }
                    }
                    printList(rentNum,cars,rentDays);
                }else{
                    System.out.println("谢谢光临,下次再见!");
                    input.close();
                    return;
                }
            }else{
                System.out.println("输入有误,请重新输入:");
                first=0;
            }
        }
        input.close();
    }
    /*
     * 展示车辆
     */
    private static void showProduct(Car[] cars) {
        System.out.println("您可租车的类型及其价目表:nr序号t汽车名称t       租金tt容量");
        for (int i=1;i

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2844/viewspace-2798028/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-16

  • 博文量
    76
  • 访问量
    414