不知道这样写好不好,求大神指点!!
/******父类******/ package com.DaDaZuChe; public abstract class Car {//所有车的父类 private int carNumber;//车序号 private String carName;//车名 private int carRent;//租金 public int getCarNumber() { return carNumber; } public void setCarNumber(int carNumber) { this.carNumber = carNumber; } public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public int getCarRent() { return carRent; } public void setCarRent(int carRent) { this.carRent = carRent; } } /******子类:只载人******/ package com.DaDaZuChe; public class passengerCar extends Car {//只拉人的车 private int passengerNumber;//乘客数量 public passengerCar(int carNumber,String carName,int carRent,int passengerNumber){ this.passengerNumber = passengerNumber; setCarNumber(carNumber); setCarName(carName); setCarRent(carRent); } public int getPassengerNumber() { return passengerNumber; } public void setPassengerNumber(int passengerNumber) { this.passengerNumber = passengerNumber; } public void out(){ System.out.println(getCarNumber()+"."+" "+getCarName()+" "+getCarRent()+"元/天"+" "+"载人:"+getPassengerNumber()+"人"); } } /******子类:只载货******/ package com.DaDaZuChe; public class goodsCar extends Car {//只拉货的车 private int goodsNumber;//货的吨数 public goodsCar(int carNumber,String carName,int carRent,int goodsNumber){ this.goodsNumber = goodsNumber; setCarNumber(carNumber); setCarName(carName); setCarRent(carRent); } public int getGoodsNumber() { return goodsNumber; } public void setGoodsNumber(int goodsNumber) { this.goodsNumber = goodsNumber; } public void out(){ System.out.println(getCarNumber()+"."+" "+getCarName()+" "+getCarRent()+"元/天"+" "+"载货:"+getGoodsNumber()+"吨"); } } /******子类:可载人载货******/ package com.DaDaZuChe; public class pandgCar extends Car {//即拉人又拉货的车 private int passengerNumber;//人数 private int goodsNumber;//吨数 public pandgCar(int carNumber,String carName,int carRent,int passengerNumber,int goodsNumber){ this.passengerNumber = passengerNumber; this.goodsNumber = goodsNumber; setCarNumber(carNumber); setCarName(carName); setCarRent(carRent); } public int getPassengerNumber() { return passengerNumber; } public void setPassengerNumber(int passengerNumber) { this.passengerNumber = passengerNumber; } public int getGoodsNumber() { return goodsNumber; } public void setGoodsNumber(int goodsNumber) { this.goodsNumber = goodsNumber; } public void out(){ System.out.println(getCarNumber()+"."+" "+getCarName()+" "+getCarRent()+"元/天"+" "+"载人:"+getPassengerNumber()+"人 "+"载货:"+getGoodsNumber()+"吨"); } } /******测试类******/ /* * 创建了Car父类,创建了passengerCar,goodsCar,pandgCar三个子类! * 程序中定义了: * 记录租车序号的数组 * 记录可载人的车名的数组 * 记录可载货的车名的数组 * 记录租金的数组 * 记录载人数的数组 * 记录载货吨数的数组, * ***通过for循环加switch语句为以上数组赋值,用来记录最后需要显示和计算结果的信息! */ package com.DaDaZuChe; import java.util.Scanner;//导入Scanner的包 public class Test { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("欢迎使用答答租车系统:"); System.out.println("您是否要租车:1是 0否"); Scanner input = new Scanner(System.in);//创建Scanner类对象 int keyValue1 = input.nextInt();//接收第一个键值 if(keyValue1 == 1){ passengerCar car1 = new passengerCar(1," 奥迪A4 ",500,4); passengerCar car2 = new passengerCar(2," 马自达6 ",400,4); pandgCar car3 = new pandgCar(3," 皮卡雪6 ",450,4,2); passengerCar car4 = new passengerCar(4," 金龙 ",800,20); goodsCar car5 = new goodsCar(5," 松花江 ",400,4); goodsCar car6 = new goodsCar(6," 依维柯 ",1000,20); System.out.println("您可以租车的类型及价目表:"); System.out.println("序号 汽车名称 租金 容量"); car1.out(); car2.out(); car3.out(); car4.out(); car5.out(); car6.out(); System.out.println("请您输入要租车的数量:"); int keyValue2 = input.nextInt();//接收租车的数量 int[] carNumber = new int[keyValue2];//用来记录租车序号的数组 String[] carName1 = new String[keyValue2];//用来记录可载人的车名的数组 String[] carName2 = new String[keyValue2];//用来记录可载货的车名的数组 int[] carRent = new int[keyValue2];//用来记录租金的数组 int[] passengerNumber = new int[keyValue2];//用来记录载人数的数组 int[] goodsNumber = new int[keyValue2];//用来记录载货吨数的数组 for(int i = 0;i < keyValue2;i++){ System.out.println("请您输入第"+(i+1)+"辆车的序号:"); carNumber[i] = input.nextInt(); switch(carNumber[i]){ case 1: carName1[i] = car1.getCarName(); carName2[i] = "no";//占空缺的位置 carRent[i] = car1.getCarRent(); passengerNumber[i] = car1.getPassengerNumber(); goodsNumber[i] = 0;break; case 2: carName1[i] = car2.getCarName(); carName2[i] = "no"; carRent[i] = car2.getCarRent(); passengerNumber[i] = car2.getPassengerNumber(); goodsNumber[i] = 0;break; case 3://皮卡可以载人载货所以需要在两个名称数组中存入 carName1[i] = car3.getCarName(); carName2[i] = car3.getCarName(); carRent[i] = car3.getCarRent(); passengerNumber[i] = car3.getPassengerNumber(); goodsNumber[i] = car3.getGoodsNumber();break; case 4: carName1[i] = car4.getCarName(); carName2[i] = "no"; carRent[i] = car4.getCarRent(); passengerNumber[i] = car4.getPassengerNumber(); goodsNumber[i] = 0;break; case 5: carName1[i] = "no"; carName2[i] = car5.getCarName(); carRent[i] = car5.getCarRent(); passengerNumber[i] = 0; goodsNumber[i] = car5.getGoodsNumber();break; case 6: carName1[i] = "no"; carName2[i] = car6.getCarName(); carRent[i] = car6.getCarRent(); passengerNumber[i] = 0; goodsNumber[i] = car6.getGoodsNumber();break; default:System.out.println("输入错误!");i--;//重新存储当前值 } } System.out.println("请您输入租车天数:"); int keyValue3 = input.nextInt();//接收租车天数 System.out.println("您的账单:\n***载人的车有:"); int sumPassenger = 0;//总乘客数 int sumRent = 0;//总金额 for(int i = 0;i < keyValue2;i++){ if(carName1[i] != "no") System.out.print(carName1[i]+" "); sumPassenger = sumPassenger + passengerNumber[i]; sumRent = sumRent + carRent[i]; } System.out.println("共载人:"+sumPassenger+"人"); System.out.println("您的账单:\n***载人的车有:"); int sumGoods = 0;//总货数 for(int i = 0;i < keyValue2;i++){ if(carName2[i] != "no") System.out.print(carName2[i]+" "); sumGoods = sumGoods + goodsNumber[i]; } System.out.println("共载货:"+sumGoods+"吨"); sumRent = sumRent * keyValue3; System.out.println("***租车总金额:"+sumRent+"元"); } else System.out.println("再见!"); } }