Java實現(xiàn)汽車租賃系統(tǒng)
本文介紹的是利用java編寫一個控制臺版的“達達租車系統(tǒng)”,下面話不多說了,來看看詳細實現(xiàn)方法吧。
實現(xiàn)目標
java編寫一個控制臺版的“達達租車系統(tǒng)”
實現(xiàn)功能
1.展示所有可租車輛
2.選擇車型、租車量
3.展示租車清單,包含:總金額、總載貨量及其車型、總載人量及其車型
三大分析
數(shù)據(jù)模型分析

業(yè)務(wù)模型分析

顯示和流程分析

實現(xiàn)效果
租車頁面

租車賬單

實現(xiàn)思路
首先定義一個Car類,它包含基本功能:車名、載客數(shù)、載貨量、日租金。接著創(chuàng)建三個小類,分別是客車類、貨車類和皮卡類(既能載客又能載貨),它們都繼承Car類。最后需要一個主類,用于開啟整個系統(tǒng),調(diào)用每個小類。
實現(xiàn)代碼
public abstract class Car {
public int rent;//日租金
public int people;//載客人數(shù)
public int loads;//載貨量
public String name;//車名
public int getRent(){
return rent;
}
public void setRent(int rent){
this.rent=rent;
}
public int getPeople(){
return people;
}
public void setPeople(int people){
this.people=people;
}
public int getLoads(){
return loads;
}
public void setLoads(int loads){
this.loads=loads;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
}客車類
public class PassageCar extends Car{
public PassageCar(String name,int people,int rent){
this.setName(name);
this.setPeople(people);
this.setRent(rent);
}
public String toString(){
return this.getName()+"\t"+this.getPeople()+"\t\t\t\t"+this.getRent();
}
}卡車類
public class Truck extends Car {
public Truck(String name,int loads,int rent){
this.setName(name);
this.setLoads(loads);
this.setRent(rent);
}
public String toString(){
return this.getName()+"\t\t\t"+this.getLoads()+"\t\t"+this.getRent();
}
}皮卡類
public class Pickup extends Car {
public Pickup(String name,int people,int loads,int rent){
this.setName(name);
this.setPeople(people);
this.setLoads(loads);
this.setRent(rent);
}
public String toString(){
return this.getName()+"\t"+this.getPeople()+"\t\t"+this.getLoads()+"\t\t"+this.getRent();
}
}主類
import java.util.*;
public class Initial {
public static void main(String[] args) {
//對各類車實例化并保存到cars數(shù)組
Car[] cars={
new PassageCar("奧迪A4",4,500),
new PassageCar("馬自達6",4,400),
new Pickup("皮卡雪6",4,2,450),
new PassageCar("金龍",20,800),
new Truck("松花江",4,400),
new Truck("依維柯",20,1000)};
System.out.println("****歡迎使用達達租車系統(tǒng)!****");
System.out.println("****您確認租車嗎?****"+"\n"+"是(請輸入1) \t 否(請輸入2)");
Scanner in1=new Scanner(System.in);
int is=in1.nextInt();
if(is!=1){
System.out.println("****歡迎下次光臨!****");
System.exit(0);
}
if(is==1){
System.out.println("****您可租車的類型及價目表****");
System.out.println("序號"+"\t車名"+"\t載客數(shù)(人)"+"\t載貨量(噸)"+"\t日租金(元/天)");
//使用循環(huán)方式將各類車輸出
for(int i=0;i<cars.length;i++){
System.out.println((i+1)+"\t"+cars[i]);
}
System.out.println("****請輸入您的租車數(shù)量:****");
int num1=in1.nextInt();
Car[] rentcar=new Car[num1];
int price=0;//總價格
int totalpeople=0;//總?cè)藬?shù)
int totalloads=0;//總載貨量
for(int i=0;i<num1;i++){
System.out.println("****請輸入第"+(i+1)+"輛車的序號:****");
int numx=in1.nextInt();
rentcar[i]=cars[numx-1];
}
System.out.println("****請輸入天數(shù):****");
int day=in1.nextInt();
for(int i=0;i<num1;i++){
price=price+rentcar[i].rent *day;
}
System.out.println("****您的賬單:****");
System.out.println("已選載人車:");
for(int i=0;i<num1;i++){
if(rentcar[i].people!=0){
System.out.println(rentcar[i].name+"\t");
}
totalpeople=totalpeople+rentcar[i].people;
}
System.out.println('\n');
System.out.println("已選載貨車:");
for(int i=0;i<num1;i++){
if(rentcar[i].loads!=0){
System.out.println(rentcar[i].name+"\t");
}
totalloads=totalloads+rentcar[i].loads;
}
System.out.println('\n');
System.out.println("共載客:"+totalpeople+"人");
System.out.println("共載貨:"+totalloads+"噸");
System.out.println("租車總價格:"+price+"元");
System.out.println('\n');
System.out.println("****感謝您的惠顧,歡迎再次光臨!****");
}
}
}到此這篇關(guān)于Java實現(xiàn)汽車租賃系統(tǒng)的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 實戰(zhàn)范例之線上新聞平臺系統(tǒng)的實現(xiàn)
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+jsp+jdbc+mysql實現(xiàn)一個線上新聞平臺系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11
java基礎(chǔ)之Integer與int類型輸出示例解析
這篇文章主要為大家介紹了java基礎(chǔ)之Integer與int類型輸出示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
如何去掉IntelliJ IDEA中mybatis對應(yīng)的xml文件警告
這篇文章主要介紹了如何去掉IntelliJ IDEA中mybatis對應(yīng)的xml文件警告問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04

