软件编程
位置:首页>> 软件编程>> java编程>> Java实现答答租车系统

Java实现答答租车系统

作者:希夏普  发布时间:2022-07-12 01:38:17 

标签:java,租车系统

本文实例为大家分享了Java实现答答租车系统的具体代码,供大家参考,具体内容如下

项目需求:

Java实现答答租车系统

基本界面需求:

Java实现答答租车系统

and:

Java实现答答租车系统

最后是把账单打印出来:

Java实现答答租车系统

个人代码实现

基本思路:考虑到车辆之间的共性,设置一个父类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();
 }

}

运行结果:

Java实现答答租车系统

Java实现答答租车系统

来源:https://blog.csdn.net/Joseph_Cherry/article/details/60530978

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com