为了账号安全,请及时绑定邮箱和手机立即绑定

第一个完整的java程序(答答打车系统)

标签:
Java

父类

package com.arkfallen;

/***********************************************************************
 * Module:  Car.java
 * Author:  saber
 * Purpose: Defines the Class Car
 ***********************************************************************/

/** @pdOid f85f6a45-758c-4bea-8be6-8dc1f9473cad */
public abstract class Car {

   private String name;
   private int price;
   private int capacity;

   public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getPrice() {
    return price;
}
public void setPrice(int price) {
    this.price = price;
}
public int getCapacity() {
    return capacity;
}
public void setCapacity(int capacity) {
    this.capacity = capacity;
}

   abstract  public String show();

   public void  setter(String n,int p,int c) {
      // TODO: implement
       name=n;
       price=p;
       capacity=c;
   }

子父类(1)

package com.arkfallen;
/***********************************************************************
 * Module:  Passengercars.java
 * Author:  saber
 * Purpose: Defines the Class Passengercars
 ***********************************************************************/
public class Passengercars extends Car {

   public String show() {
      // TODO: implement
       String s="载人:"+getCapacity()+"人。";
    return s;
   }
}

子父类(2)

package com.arkfallen;
/***********************************************************************
 * Module:  Truck.java
 * Author:  saber
 * Purpose: Defines the Class Truck
 ***********************************************************************/
public class Truck extends Car {
   public String show() {
      // TODO: implement
       String s="载货:"+getCapacity()+"吨。";
    return s;
   }
}

子类(1)

package com.arkfallen;
/***********************************************************************
 * Module:  Audi.java
 * Author:  saber
 * Purpose: Defines the Class Audi
 ***********************************************************************/
public class Audi extends Passengercars {
}

子类(2)

package com.arkfallen;
/***********************************************************************
 * Module:  Mazda.java
 * Author:  saber
 * Purpose: Defines the Class Mazda
 ***********************************************************************/

public class Mazda extends Passengercars {
}

子类(3)

package com.arkfallen;
/***********************************************************************
 * Module:  GoldenDragon.java
 * Author:  saber
 * Purpose: Defines the Class GoldenDragon
 ***********************************************************************/
public class GoldenDragon extends Passengercars {
}

子类(4)

package com.arkfallen;
/***********************************************************************
 * Module:  Iveco.java
 * Author:  saber
 * Purpose: Defines the Class Iveco
 ***********************************************************************/
public class Iveco extends Truck {
}

子类(5)

package com.arkfallen;
/***********************************************************************
 * Module:  TheSonghuaRiver.java
 * Author:  saber
 * Purpose: Defines the Class TheSonghuaRiver
 ***********************************************************************/
public class TheSonghuaRiver extends Truck {
}

子类(6)

package com.arkfallen;

/***********************************************************************
 * Module:  Pickup.java
 * Author:  saber
 * Purpose: Defines the Class Pickup
 ***********************************************************************/
public class Pickup extends Truck {
    int person;
   public String show() {
      // TODO: implement
       String s="载人:"+person+"人,载货:"+getCapacity()+"吨。";
    return s;
   }
public int getPerson() {
    return person;
}
public void setPerson(int person) {
    this.person = person;
}
}

主类

package com.arkfallen;

/***********************************************************************
 * Module:  Main.java
 * Author:  saber
 * Purpose: Defines the Class Main
 ***********************************************************************/
import java.util.Scanner;

public class Main {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        int num=0;
        int n=0;
        Scanner in=new Scanner(System.in);
        Audi au=new Audi();
        Mazda ma=new Mazda();
        Pickup pi=new Pickup();
        GoldenDragon go=new GoldenDragon();
        TheSonghuaRiver tsr=new TheSonghuaRiver();
        Iveco ive=new Iveco();
        au.setter("奥迪", 500, 4);
        ma.setter("马自达", 400, 4);
        go.setter("金龙", 800, 20);
        tsr.setter("松花江", 400, 4);
        ive.setter("依维柯", 1000, 20);
        pi.setName("皮卡");
        pi.setPrice(450);
        pi.setCapacity(2);
        pi.setPerson(4);
        Car[] car={au,ma,pi,go,tsr,ive};
        System.out.println("欢迎使用答答租车系统!\n你是否要租车?\ntrue、是   false、否");
        boolean x=in.nextBoolean();
        while(x){
            System.out.println("你的可租车辆有:\n序号********名称*******租金********载量");
            for(int i=0;i<car.length;i++){
                System.out.println(i+"********"+car[i].getName()+"********"+car[i].getPrice()+"元/天******"+car[i].show());
            }
            System.out.println("你要租几辆车?");
            while(true){
                n=in.nextInt();
                if(n<1n>6)
                    System.out.println("输入错误!情重新输入:");
                else
                    break;
            }
            for(int j=0;j<n;j++){
                System.out.println("请输入第"+(j+1)+"辆车的序号:");
                int t=0;
                while(true){
                    t=in.nextInt();
                    if(t<0t>5)
                        System.out.println("输入错误!情重新输入:");
                    else
                        break;
                }
                    num+=car[t].getPrice();
            }
            System.out.println("请输入租用天数:");
            int day=0;
            while(true){
                day=in.nextInt();
                if(day<0day>5)
                    System.out.println("输入错误!情重新输入:");
                else
                    break;
            }
            System.out.println("你的账单为:"+num*day+"元。");
            break;
        }

    }
}
点击查看更多内容
5人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消