我是菜鸟一级
2015-08-05 14:11
//父类 package com.sc; public abstract class Car { public String carName; public float carPrice; public int carWeight; public int carPerson; public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public float getCarPrice() { return carPrice; } public void setCarPrice(float carPrice) { this.carPrice = carPrice; } public int getCarWeight() { return carWeight; } public void setCarWeight(int carWeight) { this.carWeight = carWeight; } public int getCarPerson() { return carPerson; } public void setCarPerson(int carPerson) { this.carPerson = carPerson; } public abstract void info(); }
//小汽车客车 package com.sc; public class PassgerCar extends Car { public PassgerCar(String carName,float carPrice,int carPerson){ this.carName = carName; this.carPerson = carPerson; this.carPrice = carPrice; } @Override public void info() { System.out.println(getCarName() + '\t' +getCarPrice() +"元/天" + '\t' + "载人:"+getCarPerson() +"人"); } }
//货车 package com.sc; public class TrunkCar extends Car { public TrunkCar(String carName,float carPrice,int carWeight) { this.carName = carName; this.carWeight = carWeight; this.carPrice = carPrice; } @Override public void info() { // TODO 自动生成的方法存根 System.out.println(getCarName() + '\t' +getCarPrice() +"元/天" + '\t' + "载重:"+getCarWeight() +"吨"); } }
//皮卡 package com.sc; public class PickupCar extends Car { public PickupCar(String carName,float carPrice,int carPerson,int carWeight) { this.carName = carName; this.carPerson = carPerson; this.carWeight = carWeight; this.carPrice = carPrice; } @Override public void info() { // TODO 自动生成的方法存根 System.out.println(getCarName() + '\t' +getCarPrice() +"元/天" + '\t' + "载人:"+getCarPerson() +"人"+ " 载货:"+getCarWeight() +"吨"); } }
//测试代码 package com.sc; import java.util.*; public class DiDi { public static void main(String[] args) { Car[] allCar = {new PassgerCar("奥迪A4",500,4),new PassgerCar("马自达6",400,4),new PickupCar("皮卡雪6",450,4,2) ,new PassgerCar("金龙 ",800,20),new TrunkCar("松花江",400,4),new TrunkCar("依维河",1000,20)}; System.out.println("欢迎使用XX租车系统!!"); System.out.println("您是否需要租车:1.是 |2.否"); Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); while (num != 1 || num !=2) { if (num == 1) { System.out.println("***************************"); System.out.println("我们为您准备的车辆类型和价格如下:"); System.out.println("序号" + '\t' + "汽车名称" + '\t' + "租金" + '\t' +"容量"); for (int j = 0; j <allCar.length ; j++) { System.out.print((j+1) + ".\t" ); allCar[j].info(); } break; } else if (num == 2) { System.out.println("谢谢您的使用,再见!"); System.exit(0); }else { System.out.println("输入不正确,请重新输入!"); System.out.println("您是否需要租车:1.是 |2.否"+'\n'); num = scanner.nextInt(); } } System.out.println("请输入您要租车的序号:"); int num1 = scanner.nextInt(); while(num1 > 0){ if (num1 > 0 && num1 <= allCar.length) { System.out.println("您选择的是第"+ num1 +"辆车,"+ allCar[num1-1].getCarName() + ",租金为:"+allCar[num1-1].getCarPrice()+ "元/天。"); System.out.println("请输入你想租车的天数:"); int day = scanner.nextInt(); float rentPrice = allCar[num1-1].getCarPrice() * day; System.out.println("您应付的租金为:" + rentPrice + "元。" ); System.exit(0); } else{ System.out.println("输入车辆序号不正确,请重新输入!"); System.out.println("请输入您要租车的序号:"+'\n'); num1 = scanner.nextInt(); } } } }
求指导,第一次写,费劲,很多含糊的地方。
考虑到封装性,应该用private关键字修饰属性
Java入门第二季 升级版
530646 学习 · 6091 问题
相似问题