Java实现答答租车系统
本文实例为大家分享了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(); } }
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Spring集成MyBatis和PageHelper分页插件整合过程详解
Spring 整合 MyBatis 是将 MyBatis 数据访问框架与 Spring 框架进行集成,以实现更便捷的开发和管理,在集成过程中,Spring 提供了许多特性和功能,如依赖注入、声明式事务管理、AOP 等,这篇文章主要介绍了Spring集成MyBatis和PageHelper分页插件整合,需要的朋友可以参考下2023-08-08java中自定义Spring Security权限控制管理示例(实战篇)
本篇文章主要介绍了java中自定义Spring Security权限控制管理示例(实战篇) ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。2017-02-02Sharding-JDBC自动实现MySQL读写分离的示例代码
本文主要介绍了Sharding-JDBC自动实现MySQL读写分离,优点在于数据源完全有Sharding-JDBC托管,写操作自动执行master库,读操作自动执行slave库,感兴趣的可以了解一下2021-11-11详解Spring系列之@ComponentScan批量注册bean
本文介绍各种@ComponentScan批量扫描注册bean的基本使用以及进阶用法和@Componet及其衍生注解使用,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧2022-02-02
最新评论