时间:2021-05-20
本文实例为大家分享了Java实现答答租车系统的具体代码,供大家参考,具体内容如下
项目需求:
基本界面需求:
and:
最后是把账单打印出来:
个人代码实现
基本思路:考虑到车辆之间的共性,设置一个父类Car, 子类MannedCar(载人), Truck(载货),BothCary(既载人又载货),三者继承父类Car的price, name属性, getName()方法, 同时重写getPersonNum, getGoodsNum方法。
Car.java:
package Car;public abstract class Car { protected int price; protected String name; protected int getPrice() { return price; } protected String getName() { return name; } public int getPersonNum() { // TODO Auto-generated method stub return 0; } public int getGoodsNum() { // TODO Auto-generated method stub return 0; }}MannedCar.java:
package Car;public class MannedCar extends Car { private int personNum; public MannedCar() { this.personNum = 0; this.price = 0; this.name = ""; } public MannedCar(int personNum, int price, String name) { this.personNum = personNum; this.price = price; this.name = name; } @Override public int getPersonNum() { return personNum; }}Truck.java:
package Car;public class Truck extends Car{ private int goodsNum; public Truck() { this.price = 0; this.goodsNum = 0; this.name = ""; } public Truck(int price, int goodsNum, String name) { this.price = price; this.goodsNum = goodsNum; this.name = name; } @Override public int getGoodsNum() { return goodsNum; }}BothCarry.java:
package Car;public class BothCarry extends Car { private int personNum; private int goodsNum; public BothCarry() { this.personNum = 0; this.goodsNum = 0; this.name = ""; this.price = 0; } public BothCarry(int price, int personNum, int goodsNum, String name) { this.personNum = personNum; this.goodsNum = goodsNum; this.price = price; this.name = name; } public int getPersonNum() { return personNum; } public int getGoodsNum() { return goodsNum; }}系统:
CarSystem.java:
package Car;import java.util.Scanner;import java.util.ArrayList;public class CarSystem { private static String goodByeInfo = "欢迎再次使用本系统,再见!"; private static int dayBorrow; public static void beginSystem() { CarSystem.SystemWelcome(); Scanner scanner = new Scanner(System.in); String userCommand = scanner.next(); switch(userCommand){ case "1": CarSystem.getUserInput(); break; case "0": System.out.println(goodByeInfo); break; default: System.out.println("输入错误..End running.."); System.exit(0); break; } } public static void SystemWelcome() { System.out.println("欢迎使用答答租车系统:"); System.out.println("您是否要租车: 1-是 0-否"); } public static void getUserInput() { int numCarBorrow = 0; ArrayList<Car> carList = new ArrayList<Car>(6); carList.add(new MannedCar(4,500,"奥迪A4")); carList.add(new MannedCar(4,400,"马自达6")); carList.add(new BothCarry(450,4,2,"皮卡雪6")); carList.add(new MannedCar(20,800,"金龙")); carList.add(new Truck(400,4,"松花江")); carList.add(new Truck(1000,20,"依维河")); System.out.println("请输入您要租汽车的数量:"); Scanner sr = new Scanner(System.in); numCarBorrow = sr.nextInt(); int[] carNumList = new int[numCarBorrow]; for(int i=0;i<numCarBorrow;i++) { System.out.println("请输入第" + (i+1) + "辆车的序号:"); if (sr.hasNext()) { carNumList[i] = sr.nextInt(); } } System.out.println("请输入租车天数:"); if (sr.hasNext()) { dayBorrow = sr.nextInt(); } sr.close(); StringBuilder manned = new StringBuilder(); int numOfManned = 0; StringBuilder goods = new StringBuilder(); int numOfGoods = 0; int totalCost = 0; for(int i = 0;i < carNumList.length;i++) { if(carNumList[i]>0 && carNumList[i] < 3 || carNumList[i]==4) { manned.append(" "); manned.append(carList.get(carNumList[i]-1).getName()); numOfManned += carList.get(carNumList[i]-1).getPersonNum(); } else if(carNumList[i]==3) { manned.append(" "); manned.append(carList.get(carNumList[i]-1).getName()); goods.append(" "); goods.append(carList.get(carNumList[i]-1).getName()); numOfManned += carList.get(carNumList[i]-1).getPersonNum(); numOfGoods += carList.get(carNumList[i]-1).getGoodsNum(); } else { goods.append(" "); goods.append(carList.get(carNumList[i]-1).getName()); numOfGoods += carList.get(carNumList[i]-1).getGoodsNum(); } totalCost += carList.get(carNumList[i]-1).getPrice(); } //Output System.out.println("您的账单:\n***可载人的车有:"); System.out.println(manned + " 共载人: " + numOfManned); System.out.println("***载货的车有:\n" + goods + " 共载货 : " + numOfGoods + "吨"); System.out.println("***租车总价格: " + totalCost * dayBorrow + "元"); }}主程序:
package Car;public class CarSystemTest { public static void main(String[] args) { // TODO Auto-generated method stub CarSystem.beginSystem(); }}运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文介绍的是利用java编写一个控制台版的“达达租车系统”,下面话不多说了,来看看详细实现方法吧。实现目标java编写一个控制台版的“达达租车系统”实现功能1.
本文实例为大家分享了java实现租车系统Demo,供大家参考,具体内容如下这也是参考了mooc上的一个基础项目,所以拿来写一下。不过我的Demo肯定有不好或者错
java实现银行ATM自助取款机,实现功能:用户登录、余额查询、存钱、取钱、转账、修改密码、退出系统。用java实现一个ATM机系统(2.0版)设计思路设计五个
使用java来实现ping功能。并写入文件。为了使用java来实现ping的功能,有人推荐使用java的Runtime.exec()方法来直接调用系统的Ping
新浪科技讯7月7日晚间消息,为了提升出租车行业信息化管理能力,滴滴出租车今日正式发布“桔行系统”,并将围绕该系统布局出租车信息化开放平台。“桔行系统”是以智