这篇文章主要介绍了java怎么实现简单租车系统,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
具体内容如下
这也是参考了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怎么实现简单租车系统”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。