java第二季答答租車系統

lanyu發表於2021-09-09

/
車類Car
*/

public abstract class Car {
public String name;
public int personCapacity;//載客量
public int goodCapacity;//載貨量
public int price;//租車價格

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public abstract void printInfo();

}
//客車類
public class Coach extends Car {

public Coach(String name,int price,int personCapacity){
this.name=name;
this.personCapacity=personCapacity;
this.price=price;
}

public void printInfo(){
System.out.println(this.name+"t"+this.price
+"元/天t載客:"+this.personCapacity+"人");
}
//貨車
public class Truck extends Car {

public final int personCapacity=0;//貨車載客量為0

public Truck(String name,int price,int goodCapacity){
    this.name=name;
    this.price=price;
    this.goodCapacity=goodCapacity;
}

public void printInfo(){
    System.out.println(this.name+"t"+this.price
             +"元/天t載貨:"+this.goodCapacity+"噸");
}

}
//皮卡,既能載客又能載貨
public class Pk extends Car {

public Pk(String name,int price,int personCapacity,int goodCapacity){
    this.name=name;
    this.price=price;
    this.personCapacity=personCapacity;
    this.goodCapacity=goodCapacity;
}

public void printInfo(){
    System.out.println(this.name+"t"+this.price
             +"元/天t載客:"+this.personCapacity+"人t載貨:"+this.goodCapacity+"噸");
}

}

import java.util.Scanner;
public class Initial {

/**
 * @param args
 */
public static void main(String[] args) {

 Car[] cars={new Coach("奧迪",500,4),new Coach("馬自達6",400,4)
            ,new Coach("金龍",800,20),new Pk("皮卡雪6",450,4,2)
            ,new Truck("松花江",400,4),new Truck("依維柯",1000,20)};

    Scanner sc=new Scanner(System.in);
    System.out.println("歡迎使用答答租車系統");
    System.out.println("您是否要租車?0:不是,1:是");
    int i=sc.nextInt();
    switch(i){
    case 0:{
        System.out.println("您已退出該系統");
        break;
    }
    case 1:{
        System.out.println("您可租車的車型及其價目表如下:");
        System.out.println("序號t汽車名稱t租金t容量");
         for(int j=0;j

}
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2035/viewspace-2807599/,如需轉載,請註明出處,否則將追究法律責任。

相關文章