这篇文章主要讲解了“Java怎么实现租车系统”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Java怎么实现租车系统”吧!
题目: 设计一个简易的租车系统,实现租车,并统计车型、载客数、载人数、租赁总价钱。
1、程序分析
Step1:创建汽车类
Step2:创建租赁类,完成汽车租赁任务
2、程序实现
car.java
package Jack_Cui;/* * Project: Lease System * Comments: 汽车类,客车、货车、皮卡车的基类 * JDK version used: JDK1.8 * Author: Jack-Cui * Create Date: 2016-8-29 */public class Car { private String s_name; //汽车名private int i_price; //汽车租赁价格/*类外访问,需要通过set、get方法写/读数据成员s_name、i_price*/public String getName() {return s_name; }public void setName(String name) {this.s_name = name; }public int getPrice() {return i_price; }public void setPrice(int price) {this.i_price = price; } }
Lease.java
package Jack_Cui; import java.util.Scanner; //导入Scanner,获取用户输入信息/* * Project: Lease System * Comments: 租赁 * JDK version used: JDK1.8 * Author: Jack-Cui * Create Date: 2016-8-29 */public class Lease {public static void main(String[] args){ System.out.println("欢迎使用答答租车系统\n您是否要租车:1是\t0否"); //打印提示信息int i_finalPrice = 0; //最终总价格int i_finalPersonNum = 0; //最终总载客量int i_finalGoodsNum = 0; //最终总载货量StringBuffer bufer_sBusName = new StringBuffer(); //记录租车的名字StringBuffer bufer_sTrunkName = new StringBuffer(); //记录租车的名字Scanner input = new Scanner(System.in); //获取用户输入信息if(input.nextInt() != 1){ System.out.println("谢谢光临,再会!"); input.close(); //关闭用户输入return; } System.out.println("您可租车的类型及其价目表:\n序号\t汽车名称\t租金\t容量"); Bus car1 = new Bus("奥迪A4",500,4); //实例化奥迪A4Bus car2 = new Bus("马自达6",400,4); //实例化马自达6Bus car3 = new Bus("金龙",800,20); //实例化金龙Trunk car4 = new Trunk("松花江",400,4); //实例化松花江Trunk car5 = new Trunk("依维柯",1000,20); //实例化依维柯Pickup car6 = new Pickup("皮卡雪6",450,4,2); //实例化皮卡雪6Car[] cars = {car1,car2,car3,car4,car5,car6};/*展示车辆信息*/for(int i = 0; i < cars.length; i++){ System.out.println((i + 1) + ".\t" + cars[i].toString()); } System.out.println("请输入您要租汽车的数量:");int i_num = input.nextInt(); //记录租车数量int[] i_car = new int[i_num]; //记录租车序号for(int i = 0; i < i_num; i++){ System.out.println("请输入第" + (i + 1) + "辆车的序号:");int i_key = input.nextInt();if(i_key >=1 && i_key <= 6){ i_car[i] = i_key; }else{ System.out.println("您输入的序列有误,请重新输入(1-6):"); i--; //输入错误,重新输入} }for(int i = 0; i < i_car.length ; i++){switch(i_car[i]){case 1: bufer_sBusName.append(car1.getName() + " "); i_finalPrice += car1.getPrice(); i_finalPersonNum += car1.getI_PersonNum();break;case 2: bufer_sBusName.append(car2.getName() + " "); i_finalPrice += car2.getPrice(); i_finalPersonNum += car2.getI_PersonNum();break;case 3: bufer_sBusName.append(car3.getName() + " "); i_finalPrice += car3.getPrice(); i_finalPersonNum += car3.getI_PersonNum();break;case 4: bufer_sTrunkName.append(car4.getName() + " "); i_finalPrice += car4.getPrice(); i_finalGoodsNum+= car4.getI_GoodsNum();break;case 5: bufer_sTrunkName.append(car5.getName() + " "); i_finalPrice += car5.getPrice(); i_finalGoodsNum += car5.getI_GoodsNum();break;case 6: bufer_sBusName.append(car6.getName() + " "); bufer_sTrunkName.append(car6.getName() + " "); i_finalPrice += car6.getPrice(); i_finalPersonNum += car6.getI_PersonNum(); i_finalGoodsNum += car6.getI_GoodsNum();break;default:break; } } System.out.println("请输入租车天数:");int days = input.nextInt();//最终信息输出System.out.println("您的账单:\n***可载人的车有:"); System.out.println(bufer_sBusName + "共载人:" + i_finalPersonNum + "人"); System.out.println("***载货的车有:\n" + bufer_sTrunkName + "共载货:" + i_finalGoodsNum + "吨"); System.out.println("***租车总价格:" + i_finalPrice * days); System.out.println("感谢使用答答租车系统"); input.close(); //关闭用户输入} }/* * Project: Lease System * Comments: 客车类,继承于汽车类 * JDK version used: JDK1.8 * Author: Jack-Cui * Create Date: 2016-8-29 */class Bus extends Car { //访问修饰符(默认):可在本类、同包中使用private int i_PersonNum; //客车类载人容量public Bus(String name, int price, int PersonNum){ //有参构造方法super(); //如果没有,系统默认调用super.setName(name); //设置客车名称super.setPrice(price); //设置客车租赁价格this.i_PersonNum = PersonNum; //设置客车容量}/*类外访问,需要通过set、get方法写/读数据成员i_PersonNum*/public int getI_PersonNum() {return i_PersonNum; }public void setI_PersonNum(int i_PersonNum) {this.i_PersonNum = i_PersonNum; } @Overridepublic String toString() {return this.getName() + "\t" + this.getPrice() + "元/天\t载人:" + i_PersonNum + "人"; } }/* * Project: Lease System * Comments: 货车类,继承于汽车类 * JDK version used: JDK1.8 * Author: Jack-Cui * Create Date: 2016-8-29 */class Trunk extends Car{private int i_GoodsNum; //货车的载货量public Trunk(String name, int price, int GoodsNum){ //有参构造方法super(); //如果没有,系统默认调用super.setName(name); //设置货车名称super.setPrice(price); //设置货车租赁价格this.i_GoodsNum = GoodsNum; //设置货车载货量}/*类外访问,需要通过set、get方法写/读数据成员i_GoodsNum*/public int getI_GoodsNum() {return i_GoodsNum; }public void setI_GoodsNum(int i_GoodsNum) {this.i_GoodsNum = i_GoodsNum; } @Overridepublic String toString() {return this.getName() + "\t" + this.getPrice() + "元/天\t载货:" + i_GoodsNum + "吨"; } }/* * Project: Lease System * Comments: 皮卡类,继承于汽车类 * JDK version used: JDK1.8 * Author: Jack-Cui * Create Date: 2016-8-29 */class Pickup extends Car{private int i_PersonNum; //客车类载人容量private int i_GoodsNum; //货车的载货量public Pickup(String name, int price, int PersonNum, int GoodsNum){ super(); //如果没有,系统默认调用super.setName(name); //设置皮卡名称super.setPrice(price); //设置皮卡租赁价格this.i_PersonNum = PersonNum; //设置皮卡载人量this.i_GoodsNum = GoodsNum; //设置皮卡载货量}/*类外访问,需要通过set、get方法写/读数据成员i_PersonNum、i_GoodsNum*/public int getI_PersonNum() {return i_PersonNum; }public void setI_PersonNum(int i_PersonNum) {this.i_PersonNum = i_PersonNum; }public int getI_GoodsNum() {return i_GoodsNum; }public void setI_GoodsNum(int i_GoodsNum) {this.i_GoodsNum = i_GoodsNum; } @Overridepublic String toString() {return this.getName() + "\t" + this.getPrice() + "元/天\t载人:" + i_PersonNum + "人\t载货:" + i_GoodsNum; } }
3、运行结果
感谢各位的阅读,以上就是“Java怎么实现租车系统”的内容了,经过本文的学习后,相信大家对Java怎么实现租车系统这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。