Java實現(xiàn)簡單汽車租賃系統(tǒng)
本文實例為大家分享了Java實現(xiàn)簡單汽車租賃系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
需求如下:

問題分析:
首先應(yīng)當(dāng)構(gòu)建一個MotoVehicle的抽象(abstract)類,類里面包含一個brand屬性,表示汽車品牌;還包含一個no屬性,表示汽車牌號;
package cn.jbit.car;
public abstract class MotoVehicle {
private String no;
private String brand;
/**
* 無參構(gòu)造方法
*/
public MotoVehicle() {
}
/**
* 有參構(gòu)造方法
* @param no 汽車牌號
* @param brand 汽車品牌
*/
public MotoVehicle(String no,String brand) {
this.no=no;
this.brand=brand;
}
public String getNo() {
return no;
}
public String getBrand() {
return brand;
}
public abstract int calRent(int days);
}其次,應(yīng)有Car類繼承自MotoVehicle類,并有一個type屬性,表示轎車型號,應(yīng)有一個計算租金的方法calRent()
package cn.jbit.car;
public class Car extends MotoVehicle{
private String type;
public Car() {
}
public Car (String no,String brand,String type) {
super(no,brand);
this.type=type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public int calRent(int days) {
// TODO Auto-generated method stub
if("2".equals(type)) {
return days*500;
}
else if ("1".equals(type)) {
return days*600;
}
else {
return 300*days;
}
}
}再次,應(yīng)有Bus類繼承自MotoVehicle類,并有一個CountSet屬性,表示客車的容量,同樣的,應(yīng)有一個計算租金的方法calRent();
package cn.jbit.car;
public class Bus extends MotoVehicle {
int CountSet;
public Bus() {
}
/**
* 帶參構(gòu)造函數(shù)
*/
public Bus(String brand,String no,int CountSet) {
super(brand,no);
this.CountSet=CountSet;
}
public int getCountSet() {
return CountSet;
}
public void setCountSet(int countSet) {
CountSet = countSet;
}
@Override
public int calRent(int days) {
// TODO Auto-generated method stub
if(CountSet<16) {
return 800*days;
}
else {
return 1600*days;
}
}
}最后,以上三類應(yīng)在test類中測試;
package cn.jbit.car;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String no,brand,mtype;
int countSet,days;
Scanner input=new Scanner(System.in);
System.out.println("*****歡迎來到汽車租賃公司!******");
System.out.println("請輸入天數(shù):");
days=input.nextInt();
System.out.println("請輸入車輛類型:");
System.out.println("1、轎車 2、客車");
mtype=input.next();
if("1".equals(mtype)) {
System.out.println("請輸入轎車品牌:");
System.out.println("1、寶馬 2、別克");
brand=input.next();
if("1".equals(brand)) {
System.out.println("2、寶馬550i:500");
System.out.println("請輸入轎車型號:");
mtype=input.next();
System.out.println("請輸入輛數(shù):");
int count=input.nextInt();
Car car=new Car("遼B000",brand,mtype);
System.out.println("您需支付:"+count*car.calRent(days));
}
else {
System.out.println("1、別克商務(wù)GL8:600 3、別克林蔭大道:300");
mtype=input.next();
System.out.println("請輸入輛數(shù):");
int count=input.nextInt();
Car car=new Car("遼B000",brand,mtype);
System.out.println("您需支付:"+count*car.calRent(days));
}
}
else {
System.out.println("請輸入品牌:");
System.out.println("1、金杯 2、金龍");
brand=input.next();
System.out.println("請輸入座位數(shù):");
countSet=input.nextInt();
System.out.println("請輸入輛數(shù):");
int count=input.nextInt();
Bus b=new Bus(brand,"遼B000",countSet);
System.out.println("您需支付:"+b.calRent(days)*count);
}
}
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot2.6.x 與 Swagger3 兼容問題及解決方法
文章介紹了Spring Boot 2.6.x與Swagger 3兼容性問題的解決方法,如果項目中未引入spring-boot-starter-actuator,則在yml文件中加入相關(guān)配置,反之,需要添加其他配置,感興趣的朋友一起看看吧2025-03-03
IDEA使用Maven創(chuàng)建module出現(xiàn)Ignored?pom.xml問題及解決
這篇文章主要介紹了IDEA使用Maven創(chuàng)建module出現(xiàn)Ignored?pom.xml問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
java String 轉(zhuǎn)成Double二維數(shù)組的方法
下面小編就為大家?guī)硪黄猨ava String 轉(zhuǎn)成Double二維數(shù)組的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10

