答答租車(封裝+多型)

tankII發表於2021-09-09
主類

package dadazuche;

import java.util.Scanner;

public class Initial {

/**
 * @param args
 */
public static void main(String[] args) {
    Initial initial = new Initial();
    // TODO Auto-generated method stub
    System.out.println("歡迎使用達達租車系統:");
    System.out.println("您是否要租車:1是0否");
    //選擇是否租車
    int choose = initial.choose();
    if(choose==1){
        //列印價格類目表
        initial.carPrint();
                    //選擇車子數量及序號
        initial.chooseCar();
    }

}

private void chooseCar() {
    // TODO Auto-generated method stub
    System.out.println("需要租用幾輛車?");
    Scanner input = new Scanner(System.in);
    int n = input.nextInt();
    //總租金變數
    float sumPrice =0;

    for(int i=0;i

}

車父類

package dadazuche;

public abstract class Che {
private int zaihuoliang;
private int zaikeliang;
private float price;
private String brand;

public int getZaihuoliang() {
    return zaihuoliang;
}
public void setZaihuoliang(int zaihuoliang) {
    this.zaihuoliang = zaihuoliang;
}
public int getZaikeliang() {
    return zaikeliang;
}
public void setZaikeliang(int zaikeliang) {
    this.zaikeliang = zaikeliang;
}
public float getPrice() {
    return price;
}
public void setPrice(float price) {
    this.price = price;
}
public String getBrand() {
    return brand;
}
public void setBrand(String brand) {
    this.brand = brand;
}

}

貨車子類

package dadazuche;

public class HuoChe extends Che{
public HuoChe(String name,float price,int zaihuo) {
this.setBrand(name);
this.setPrice(price);
this.setZaihuoliang(zaihuo);
}
@Override
public int getZaihuoliang() {
// TODO Auto-generated method stub
return super.getZaihuoliang();
}

@Override
public void setZaihuoliang(int zaihuoliang) {
    // TODO Auto-generated method stub
    super.setZaihuoliang(zaihuoliang);
}

@Override
public float getPrice() {
    // TODO Auto-generated method stub
    return super.getPrice();
}

@Override
public void setPrice(float price) {
    // TODO Auto-generated method stub
    super.setPrice(price);
}

@Override
public String getBrand() {
    // TODO Auto-generated method stub
    return super.getBrand();
}

@Override
public void setBrand(String brand) {
    // TODO Auto-generated method stub
    super.setBrand(brand);
}

}

客車子類

package dadazuche;

public class KeChe extends Che {
public KeChe(String name,float price,int zaike){
this.setBrand(name);
this.setPrice(price);
this.setZaikeliang(zaike);
}

@Override
public int getZaikeliang() {
    // TODO Auto-generated method stub
    return super.getZaikeliang();
}

@Override
public void setZaikeliang(int zaikeliang) {
    // TODO Auto-generated method stub
    super.setZaikeliang(zaikeliang);
}

@Override
public float getPrice() {
    // TODO Auto-generated method stub
    return super.getPrice();
}

@Override
public void setPrice(float price) {
    // TODO Auto-generated method stub
    super.setPrice(price);
}

@Override
public String getBrand() {
    // TODO Auto-generated method stub
    return super.getBrand();
}

@Override
public void setBrand(String brand) {
    // TODO Auto-generated method stub
    super.setBrand(brand);
}

}

皮卡車子類

package dadazuche;

public class PiKa extends Che {
public PiKa(String name,float price,int zairen,int zaihuo){
this.setBrand(name);
this.setPrice(price);
this.setZaikeliang(zairen);
this.setZaihuoliang(zaihuo);
}
@Override
public int getZaihuoliang() {
// TODO Auto-generated method stub
return super.getZaihuoliang();
}

@Override
public void setZaihuoliang(int zaihuoliang) {
    // TODO Auto-generated method stub
    super.setZaihuoliang(zaihuoliang);
}

@Override
public int getZaikeliang() {
    // TODO Auto-generated method stub
    return super.getZaikeliang();
}

@Override
public void setZaikeliang(int zaikeliang) {
    // TODO Auto-generated method stub
    super.setZaikeliang(zaikeliang);
}

@Override
public float getPrice() {
    // TODO Auto-generated method stub
    return super.getPrice();
}

@Override
public void setPrice(float price) {
    // TODO Auto-generated method stub
    super.setPrice(price);
}

@Override
public String getBrand() {
    // TODO Auto-generated method stub
    return super.getBrand();
}

@Override
public void setBrand(String brand) {
    // TODO Auto-generated method stub
    super.setBrand(brand);
}

}

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

相關文章