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

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

ArkFallen
关注TA
已关注
手记 1
粉丝 4
获赞 9

父类

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;
        }

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