十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要介绍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; ArrayListcarList = new ArrayList (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 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(); } }
运行结果:
以上是“Java怎么实现答答租车系统”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!