ITPub博客

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

Java入门第二季--答答租车系统

IT职场 作者:gabbro 时间:2021-09-09 09:02:35 0 删除 编辑

弄了好久,也参考了很多前辈的代码.最后终于做出属于自己的代码了
这里用到了第三季的try...catch语句,以防客人乱输入
萌新初次交作业,希望前辈们指点指点


1.首先是创建abstract父类

public abstract class Car {
    int id;
    String name;
    int price;
    int people;
    int weight;

    public abstract String showcarmsg();
}

2.接着创建载人汽车子类

public class PeopleCar extends Car {

    public PeopleCar(int id,String name,int price,int people){
        this.id=id;
        this.name=name;
        this.price=price;
        this.people=people;
    };

    public String showcarmsg() {
        return (id+"t"+name+"t"+price+"元/天t"+"t载人:"+people+"人");
    }

}

3.创建载货汽车子类

public class WeightCar extends Car {
    public WeightCar(int id,String name,int price,int weight){
        this.id=id;
        this.name=name;
        this.price=price;
        this.weight=weight;
    };
    public String showcarmsg() {
        return (id+"t"+name+"t"+price+"元/天t"+"t载物:"+weight+"吨");
    }

}

4.创建既可载人又可载货的汽车的子类

public class TwoCar extends Car {

    public TwoCar(int id,String name,int price,int people,int weight){
        this.id=id;
        this.name=name;
        this.price=price;
        this.people=people;
        this.weight=weight;
    };

    public String showcarmsg() {
        return (id+"t"+name+"t"+price+"元/天t"+"t载人:"+people+"人t"+"载物:"+weight+"吨");
    }

}

5.额外创建一个Inputter类,以防顾客输入错误信息,可以返回继续再输入

import java.util.Scanner;
public class Inputter {
    public int Inputter(){
        Boolean inputterBoolean=false;
        int getInputter=0;
        //int类输入
        while(inputterBoolean==false){
        try{
        Scanner input = new Scanner(System.in);
        int intInputter=input.nextInt();
        getInputter=intInputter;
        inputterBoolean=true;
        }catch(Exception e){
            System.out.println("您输入有误,请重新输入");
        }
        };
        return getInputter;     
    }
}

6.最后创建text类来实现人机互动

public class Text {
    public static void main(String[] args) {
        //0.创建cars数组,保存录入信息
        Car[] cars={
                new PeopleCar(1,"奥迪A4", 500, 4),
                new PeopleCar(2, "马自达6", 400, 4),
                new TwoCar(3, "皮卡雪6", 450, 4, 2),
                new PeopleCar(4, "金龙", 800, 20),
                new WeightCar(5, "松花江", 400,4),
                new WeightCar(6,"依维柯" , 1000,20),
        };

        //1.人机互动
        //创建一个对象以便使用input输入信息
        Inputter inputmsg=new Inputter();
        System.out.println("欢迎使用答答租车系统:n您是否要租车:1是 0否");
        int num=inputmsg.Inputter();

        //判断是否要进入系统
        if (num==0){
            System.out.println("感谢您的使用");
        }else if ((num!=1)&&(num!=0)){
            System.out.println("您输入的有误,请您输入0或者1");
        }else if (num==1){
            System.out.println("您可租车的类型及其价目表:");
            System.out.println("序号t汽车名称t租金 tt容量");
            //循环打印Cars数组中的车型
            for (int i=0;i

最后运行效果如图...感觉这样运行起来才会跟老师想要的作业一模一样
不过其实我自己还发现了一点漏洞.就好比如一开始租车,如果顾客不输入1或者0,而是输入3,4,5等等能不能够重新输入...这里其实是可以改善的,但是我还没想好要不要另外创建一个类或者在inputter类里面控制.不知道大侠没有没有什么好的想法?方便大家一起交流交流

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

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

注册时间:2019-08-19

  • 博文量
    85
  • 访问量
    726