作业, 不知道还要怎么改了
public abstract class BasicCar { public abstract void mgr(); public int cash; public String carName; }
public class CarFreight extends BasicCar { private final int hNumber; public CarFreight(String carName, int cash, int hNumber) { this.carName = carName; this.hNumber = hNumber; this.cash = cash; } @Override public void mgr() { // TODO Auto-generated method stub System.out.println(carName + " " + cash + "元/天" + " 载货: " + hNumber + "吨"); } }
public class CarPassenger extends BasicCar{ private int pNumber; public CarPassenger(String carName, int cash, int pNumber){ this.carName = carName; this.pNumber = pNumber; this.cash = cash; } @Override public void mgr() { // TODO Auto-generated method stub System.out.println(carName + " " + cash + "元/天" + " 载人: " + pNumber + "人"); } }
public class CarFreightPassenger extends BasicCar { private final int hNumber; private final int pNumber; public CarFreightPassenger(String carName, int cash, int pNumber, int hNumber) { this.carName = carName; this.hNumber = hNumber; this.pNumber = pNumber; this.cash = cash; } @Override public void mgr() { // TODO Auto-generated method stub System.out.println(carName + " " + cash + "元/天" + " 载人: " + pNumber + "人"+" 载货: " + hNumber + "吨"); } }
import java.util.InputMismatchException; import java.util.Scanner; public class RentCar { /** * 显示车辆的价目表。 * * @param cars */ public static void showCarInfo(BasicCar[] cars) { System.out.println("您可租车的类型及其价目表:"); System.out.println("序号 汽车名称 租金 容量"); for (int i = 1; i <= cars.length; i++) { System.out.print(i + " "); cars[i - 1].mgr(); } } @SuppressWarnings("resource") public static void rent(BasicCar[] cars) { System.out.println("欢迎使用答答租车系统:"); System.out.println("您是否要租车: 1是 0 否"); Scanner scanner = new Scanner(System.in); int isRent = 0; // 判断输入的值是否为1,0, 包括判断数字是否为数字,否则重新输入值, 直到输入的值为0,1; try { isRent = scanner.nextInt(); } catch (InputMismatchException e) { isRent = 3; } while (isRent != 1 && isRent != 0) { System.out.println("您的输入有误, 请重新输入:"); try { scanner = new Scanner(System.in); isRent = scanner.nextInt(); } catch (InputMismatchException e) { } } // 如果为0, 则退出;如果不为0,则开始租车流程; if (isRent == 0) { System.out.println("欢迎下次光临!"); } else { showCarInfo(cars); System.out.println("请输入您要租汽车的数量:"); int carNumber = scanner.nextInt(); int carCashTotal = 0; for (int i = 0; i < carNumber; i++) { System.out.println("请输入第" + i + 1 + "辆车的序号:"); carCashTotal = carCashTotal + cars[scanner.nextInt() - 1].cash; } System.out.println("您选了" + carNumber + "辆车, 分别为:"); for (int j = 0; j < carNumber; j++) { cars[j].mgr(); } System.out.println("所需的总金额为:" + carCashTotal + "¥"); } } public static void main(String[] args) { BasicCar[] cars = new BasicCar[6]; cars[0] = new CarPassenger("奥迪A4", 500, 4); cars[1] = new CarPassenger("马自达6", 400, 4); cars[2] = new CarFreightPassenger("皮卡雪6", 450, 4, 2); cars[3] = new CarPassenger("金龙 ", 800, 20); cars[4] = new CarFreight("松花江 ", 400, 4); cars[5] = new CarFreight("依维柯 ", 1000, 40); rent(cars); } }