Java面向?qū)ο髮崿F(xiàn)汽車租賃系統(tǒng)
本文實例為大家分享了Java實現(xiàn)汽車租賃系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下





父類Vehicle
public abstract class Vehicle {
? ? private String num;
? ? private String brand;
? ? private ?double rent;
? ? //重寫equals方法
? ? public abstract boolean equals(Vehicle v);
? ? //計算費用
? ? public abstract double cost(int days,double rent);
? ? //3個參數(shù)的有參構(gòu)造
? ? public Vehicle(String num, String brand, double rent) {
? ? ? ? this.num = num;
? ? ? ? this.brand = brand;
? ? ? ? this.rent = rent;
? ? }
? ? //無參構(gòu)造
? ? public Vehicle() {
? ? }
? ? //get,set方法
? ? public String getNum() {
? ? ? ? return num;
? ? }
? ? public void setNum(String num) {
? ? ? ? this.num = num;
? ? }
? ? public String getBrand() {
? ? ? ? return brand;
? ? }
? ? public void setBrand(String brand) {
? ? ? ? this.brand = brand;
? ? }
? ? public double getRent() {
? ? ? ? return rent;
? ? }
? ? public void setRent(double rent) {
? ? ? ? this.rent = rent;
? ? }
}子類Cars
public class Cars extends Vehicle{
? ? private String type;
? ? //重寫父類equals方法
? ? @Override
? ? public boolean equals(Vehicle v) {
? ? ? ? if(v instanceof Cars){
? ? ? ? ? ? Cars c=(Cars) v;
? ? ? ? ? ? return this.getBrand().equals(c.getBrand())&&this.getType().equals(c.getType());
? ? ? ? }
? ? ? ? return false;
? ? }
? ? //重寫父類費用方法
? ? @Override
? ? public double cost(int days,double sent) {
? ? ? ? if (days>150){
? ? ? ? ? ? return days*sent*0.7;
? ? ? ? }else if (days>30){
? ? ? ? ? ? return days*sent*0.8;
? ? ? ? }else if (days>7){
? ? ? ? ? ? return days*sent*0.9;
? ? ? ? }else {
? ? ? ? ? ? return days*sent;
? ? ? ? }
? ? }
? ? //無參構(gòu)造
? ? public Cars() {
? ? }
? ? //有參構(gòu)造
? ? public Cars(String num, String brand, double rent, String type) {
? ? ? ? super(num, brand, rent);
? ? ? ? this.type = type;
? ? }
? ? //2個有參構(gòu)造的方法
? ? public Cars(String brand,String type){
? ? ? ? super.setBrand(brand);
? ? ? ? this.type = type;
? ? }
? ? public String getType() {
? ? ? ? return type;
? ? }
? ? public void setType(String type) {
? ? ? ? this.type = type;
? ? }
}子類Bus
public class Bus extends Vehicle{
? ? private int seat;
? ? //重寫父類的equals方法
? ? @Override
? ? public boolean equals(Vehicle v) {
? ? ? ? if(v instanceof Bus){
? ? ? ? ? ? Bus b=(Bus) v;
? ? ? ? ? ? return this.getBrand().equals(b.getBrand())&&this.getSeat()==b.getSeat();
? ? ? ? }
? ? ? ? return false;
? ? }
? ? //重寫父類費用方法
? ? @Override
? ? public double cost(int days,double rent) {
? ? ? ? if (days>=150){
? ? ? ? ? ? return days*rent*0.6;
? ? ? ? }else if (days>=30){
? ? ? ? ? ? return days*rent*0.7;
? ? ? ? }else if (days>=7){
? ? ? ? ? ? return days*rent*0.8;
? ? ? ? }else if (days>=3){
? ? ? ? ? ? return days*rent*0.9;
? ? ? ? }else {
? ? ? ? ? ? return days*rent;
? ? ? ? }
? ? }
? ? //2個參數(shù)的有參構(gòu)造
? ? public Bus(String brand,int seat){
? ? ? ? super.setBrand(brand);
? ? ? ? this.seat=seat;
? ? }
? ? //子類有參構(gòu)造
? ? public Bus(String num, String brand, double rent, int seat) {
? ? ? ? super(num, brand, rent);
? ? ? ? this.seat = seat;
? ? }
? ? //子類無參構(gòu)造
? ? public Bus(){}
? ? //子類get set 方法
? ? public int getSeat() {
? ? ? ? return seat;
? ? }
? ? public void setSeat(int seat) {
? ? ? ? this.seat = seat;
? ? }
}汽車業(yè)務(wù)類VehicleServicer
public class VehicleServicer {
? ? public static List initVehicle(){
? ? ? ? Vehicle v1=new Bus("京6566754","金杯",800,16);
? ? ? ? Vehicle v2=new Bus("京9696996","金杯",1500,34);
? ? ? ? Vehicle v3=new Bus("京8696997","金龍",800,16);
? ? ? ? Vehicle v4=new Bus("京8696998","金龍",1500,34);
? ? ? ? Vehicle c1 =new Cars("京NT37465","別克",300,"林蔭大道");
? ? ? ? Vehicle c2 =new Cars("京9696996","別克",600,"GLB");
? ? ? ? Vehicle c3 =new Cars("京8696997","寶馬",800,"X6");
? ? ? ? Vehicle c4 =new Cars("京8696998","寶馬",600,"550i");
? ? ? ? //先裝入數(shù)組中
? ? ? ? Vehicle[] ve = {v1,v2,v3,v4,c1,c2,c3,c4};
? ? ? ? //將數(shù)組轉(zhuǎn)換成集合
? ? ? ? List<Vehicle> vehicles = Arrays.asList(ve);
? ? ? ? return vehicles;
? ? }
}測試類Test
public class Test {
? ? public static void main(String[] args) {
? ? ? ? Scanner sc = new Scanner(System.in);
? ? ? ? System.out.println("**********歡迎光臨秋明山守望者汽車租賃公司**********");
? ? ? ? System.out.println("1.轎車 ? ?2.客車");
? ? ? ? System.out.print("請選擇你要租賃的汽車類型:");
? ? ? ? int type = sc.nextInt();
? ? ? ? //橋車
? ? ? ? Vehicle ve;
? ? ? ? String brand;
? ? ? ? if(type==1){
? ? ? ? ? ? System.out.print("請選擇你要租賃的汽車品牌(1.別克 ?2.寶馬):");
? ? ? ? ? ? int pinpai = sc.nextInt();
? ? ? ? ? ? String model=pinpai==1?"別克":"寶馬";
? ? ? ? ? ? if(pinpai==1){
? ? ? ? ? ? ? ? System.out.print("請輸入你要租賃的汽車類型(1.X6 ?2.550i):");
? ? ? ? ? ? ? ? int leixin = sc.nextInt();
? ? ? ? ? ? ? ? brand=leixin==1?"林蔭大道":"GL8";
? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? System.out.print("請輸入你要租賃的汽車類型(1.X6 ?2.550i):");
? ? ? ? ? ? ? ? int leixin = sc.nextInt();
? ? ? ? ? ? ? ? brand=leixin==1?"X6":"550i";
? ? ? ? ? ? }
? ? ? ? ? ? ?ve = new Cars(model, brand);
? ? ? ? }else {//客車
? ? ? ? ? ? int seat;
? ? ? ? ? ? System.out.print("請選擇你要租賃的汽車品牌(1.金龍 ?2.金杯):");
? ? ? ? ? ? int pinpai = sc.nextInt();
? ? ? ? ? ? String s=pinpai==1?"金龍":"金杯";
? ? ? ? ? ? System.out.print("請選擇你要租賃的汽車座位數(shù)(1.16座 2.34座):");
? ? ? ? ? ? int z = sc.nextInt();
? ? ? ? ? ? seat =z==1?16:34;
? ? ? ? ? ? ve = new Bus(s, seat);
? ? ? ? }
? ? ? ? //根據(jù)選好的車型,輸出車牌和總價
? ? ? ? List<Vehicle> list = VehicleServicer.initVehicle();
? ? ? ? for (Vehicle v:list) {
? ? ? ? ? ? if(ve.equals(v)){
? ? ? ? ? ? ? ? System.out.print("請輸入你要租賃的天數(shù):");
? ? ? ? ? ? ? ? int days = sc.nextInt();
? ? ? ? ? ? ? ? System.out.println("分配給您的汽車牌號是:"+v.getNum());
? ? ? ? ? ? ? ? System.out.println("您需要支付的租賃費用是:"+v.cost(days,v.getRent()));
? ? ? ? ? ? }
? ? ? ? }
? ? }
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決nacos報錯java.lang.ClassNotFoundException: com.netflix.
這篇文章主要介紹了解決nacos報錯java.lang.ClassNotFoundException: com.netflix.config.DynamicPropertyFactory的問題,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
深入Spring Boot之ClassLoader的繼承關(guān)系和影響
這篇文章主要介紹了深入Spring Boot之ClassLoader的繼承關(guān)系和影響,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
springboot+vue實現(xiàn)oss文件存儲的示例代碼
對象存儲服務(wù)是一種海量、安全、低成本、高可靠的云存儲服務(wù),本文主要介紹了springboot+vue實現(xiàn)oss文件存儲的示例代碼,具有一定的參考價值,感興趣的可以了解一下2024-02-02
淺談SpringBoot中的@Conditional注解的使用
這篇文章主要介紹了淺談SpringBoot中的@Conditional注解的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
如何使用eclipse搭建maven多module項目(構(gòu)建父子項目)
這篇文章主要介紹了如何使用eclipse搭建maven多module項目(構(gòu)建父子項目) ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
springboot+jwt+springSecurity微信小程序授權(quán)登錄問題
這篇文章主要介紹了springboot+jwt+springSecurity微信小程序授權(quán)登錄問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
IDEA2021.2配置docker如何將springboot項目打成鏡像一鍵發(fā)布部署
這篇文章主要介紹了IDEA2021.2配置docker如何將springboot項目打成鏡像一鍵發(fā)布部署,本文圖文實例相結(jié)合給大家介紹的非常詳細,需要的朋友可以參考下2021-09-09

