java实现简单租车系统
程序员文章站
2024-02-29 22:39:52
本文实例为大家分享了java实现租车系统demo,供大家参考,具体内容如下
这也是参考了mooc上的一个基础项目,所以拿来写一下。不过我的demo肯定有不好或者错误的地方...
本文实例为大家分享了java实现租车系统demo,供大家参考,具体内容如下
这也是参考了mooc上的一个基础项目,所以拿来写一下。不过我的demo肯定有不好或者错误的地方,欢迎指出
1、项目功能/需求
a、展示所有可租车辆
b、选择车型、租车量
c、展示租车清单,包含:总金额、总载货量、总载客量
2、项目系统uml图
3、项目源码demo
car类
package car.rental; public class car { private string name; private int rent; private int passenger; private double volume; public string getname() { return name; } public void setname(string name) { this.name = name; } public int getrent() { return rent; } public void setrent(int rent) { this.rent = rent; } public int getpassenger() { return passenger; } public void setpassenger(int passenger) { this.passenger = passenger; } public double getvolume() { return volume; } public void setvolume(double volume) { this.volume = volume; } }
truck类
package car.rental; public class truck extends car { private string name; private int rent; private double volume; public truck(string name,int rent,double volume) { this.name=name; this.rent=rent; this.volume=volume; } public string getname() { return name; } public void setname(string name) { this.name = name; } public int getrent() { return rent; } public void setrent(int rent) { this.rent = rent; } public double getvolume() { return volume; } public void setvolume(double volume) { this.volume = volume; } }
coach类
package car.rental; public class coach extends car { private string name; private int rent; private int passenger; public coach(string name,int rent,int passenger) { this.name=name; this.passenger=passenger; this.rent=rent; } public int getpassenger() { return passenger; } public void setpassenger(int passenger) { this.passenger = passenger; } public string getname() { return name; } public void setname(string name) { this.name = name; } public int getrent() { return rent; } public void setrent(int rent) { this.rent = rent; } }
pickup类
package car.rental; public class pickup extends car { private string name; private int rent; private int passenger; private double volume; public pickup(string name,int rent,int passenger,double volume) { this.name=name; this.passenger=passenger; this.rent=rent; this.volume=volume; } public int getpassenger() { return passenger; } public void setpassenger(int passenger) { this.passenger = passenger; } public double getvolume() { return volume; } public void setvolume(double volume) { this.volume = volume; } public string getname() { return name; } public void setname(string name) { this.name = name; } public int getrent() { return rent; } public void setrent(int rent) { this.rent = rent; } }
测试类
package car.rental; import java.util.scanner; public class main_car { public static void main(string[] args) { // todo auto-generated method stub /*****创建车对象*******/ /*car tru=new truck("拉丝货车",200,1.1); car coa=new coach("法法客车",150,20); car pic=new pickup("无名皮卡车",300,4,0.5);*/ car[] allcar= {new truck("拉丝货车",200,1.1),new coach("法法客车",150,20), new pickup("无名皮卡车",300,4,0.5)}; system.out.println("请问您是否要租车:1.是 2.否"); scanner scan=new scanner(system.in); string input=scan.next(); if(input.equals("1")) { int all_rent = 0;//总租金 int all_passenger=0;//总载客 double all_volume=0.0;//总载货 system.out.println("您可租车的类型及其价目"); system.out.println("序号\t汽车名称\t租金\t容量\t"); for(int i=0;i<allcar.length;i++) { if(allcar[i] instanceof truck) { system.out.println((i+1)+"\t"+allcar[i].getname()+"\t"+allcar[i].getrent()+"\t"+"载货:"+allcar[i].getvolume()); } if(allcar[i] instanceof coach) { system.out.println((i+1)+"\t"+allcar[i].getname()+"\t"+allcar[i].getrent()+"\t"+"载客:"+allcar[i].getpassenger()); } if(allcar[i] instanceof pickup) { system.out.println((i+1)+"\t"+allcar[i].getname()+"\t"+allcar[i].getrent()+"\t"+"载客:"+allcar[i].getpassenger()+"载货:"+allcar[i].getvolume()); } } system.out.println("请输入您要租车的数量"); int sum=scan.nextint(); int car_order; for(int j=0;j<sum;j++) { system.out.print("你要租的第"+(j+1)+"辆车的序号是:"); car_order=scan.nextint(); system.out.println(); if(allcar[car_order-1] instanceof truck) { all_rent+=allcar[car_order-1].getrent(); all_volume+=allcar[car_order-1].getvolume(); } if(allcar[car_order-1] instanceof coach) { all_rent+=allcar[car_order-1].getrent(); all_passenger+=allcar[car_order-1].getpassenger(); } if(allcar[car_order-1] instanceof pickup) { all_rent+=allcar[car_order-1].getrent(); all_passenger+=allcar[car_order-1].getpassenger(); all_volume+=allcar[car_order-1].getvolume(); } } system.out.println("您的总租金是:"+all_rent); system.out.println("您的总载货是:"+all_volume); system.out.println("您的总载客是:"+all_passenger); } } }
4、项目命令行演示结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Java 中运行字符串表达式的方法
下一篇: PHP类型约束用法示例