给大家提供另外一个思路,仅共参考
Vehicle.java
package com.biao.liu; public class Vehicel { private String name; //汽车名称 private int price; //租金 private int personNum; //载客量 private int cargoNum; //载货量 public Vehicel(String newName,int newPrice,int newPersonNum,int newCargoNum){ name = newName; price = newPrice; personNum = newPersonNum; cargoNum = newCargoNum; } public void show(int i){ System.out.print(i+1+".\t"+name + "\t"+price+"元/天\t "); if(0 != personNum){ System.out.print("载人:"+personNum+"人 "); } if(0 != cargoNum){ System.out.print("载货:"+cargoNum+"吨"); } } public String getName() { return name; } public int getPrice() { return price; } public int getPersonNum() { return personNum; } public int getCargoNum() { return cargoNum; } }
Test.java
package com.biao.liu; import java.util.Scanner; public class Test { Vehicel vehicle[] = {new Vehicel("奥迪A4",500,4,0),new Vehicel("马自达6",400,4,0), new Vehicel("皮卡雪6",450,4,2),new Vehicel("金龙",800,20,0), new Vehicel("松花江",400,0,4), new Vehicel("依维柯",1000,0,20)}; int totalNum = 0; //总租车数量 int totalPrice = 0; //总租车租金 int totalPerson = 0; //总在人数 int totalCargo = 0; //总载货数 public static void main(String[] args) { // TODO Auto-generated method stub Test test = new Test(); System.out.println("欢迎使用答答打车系统:"); System.out.println("您是否要租车:1是 0否"); Scanner input = new Scanner(System.in); int Select = input.nextInt(); if(0 == Select){ System.out.println("您不需要租车"); }else{ System.out.println("您可租车的类型及其价目表:"); System.out.println("序号\t 汽车名称\t租金\t容量"); test.showVehicle(); test.selectVehicle(); } } public void showVehicle(){ for(int i=0; i<vehicle.length;i++){ vehicle[i].show(i); System.out.println(); } } public void selectVehicle(){ System.out.println("请输入您要租车的数量"); Scanner input = new Scanner(System.in); totalNum = input.nextInt(); Vehicel select[] = new Vehicel[totalNum]; for(int i = 0;i< totalNum;i++){ System.out.println("请输入第"+(i+1)+"两车的序号"); Scanner inputNum = new Scanner(System.in); int vehicelnum = inputNum.nextInt(); select[i] = vehicle[vehicelnum -1]; } System.out.println("请输入租车天数:"); Scanner inputDay = new Scanner(System.in); int dayNum = inputDay.nextInt(); System.out.println("您的账单:"); for(int i=0; i < select.length;i++){ totalPrice += select[i].getPrice()*dayNum; totalPerson += select[i].getPersonNum(); totalCargo += select[i].getCargoNum(); } System.out.println("***可载人的车有:"); for(int i=0; i < select.length;i++){ if(select[i].getPersonNum() != 0){ System.out.print(select[i].getName() +"\t"); } } System.out.print("共载人:"+totalPerson+"人"); System.out.println(); System.out.println("***载货的车有:"); for(int i=0; i < select.length;i++){ if(select[i].getCargoNum() != 0){ System.out.print(select[i].getName() +"\t"); } } System.out.print("共载货:"+totalCargo+"吨"); System.out.println(); System.out.println("***租车总价格:"+totalPrice +"元"); } }
结果: