首页 > IT职业 > IT生活 > 有一些冗余代码, 只是实现了功能
package com.dada;
public class Cars {
//定义子类应该有的属性
public String carName; //车名
public int id, carryMan; //编号, 载客量
public float unitPrice, carryGoods; //每日价格, 载货量
}
package com.dada;
public class Bus extends Cars{
//创建有参构造方法
public Bus(int id, String carName, float unitPrice, int carryMan ) {
this.carName = carName;
this.id = id;
this.carryMan = carryMan;
this.unitPrice = unitPrice;
}
}
package com.dada;
public class Truck extends Cars {
public Truck( int id, String carName, float unitPrice, float carryGoods ){
this.carName = carName;
this.id = id;
this.unitPrice = unitPrice;
this.carryGoods = carryGoods;
}
}
package com.dada;
public class PickUp extends Cars {
public PickUp( int id, String carName, float unitPrice, int carryMan, float carryGoods ){
this.carName = carName;
this.id = id;
this.carryMan = carryMan;
this.unitPrice = unitPrice;
this.carryGoods = carryGoods;
}
}
package com.dada;
import java.util.Scanner;
public class Users {
static Cars[] cars;
static int[] carsId;
static int days;
static float allMoney;
static int allPersons;
static float allGoods;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎进入哒哒租车系统:n 是否要租车:1 是 0 否");
Scanner input = new Scanner(System.in);
int entry = input.nextInt();
if(entry == 1) {
//罗列车目类型
Users use = new Users();
System.out.println("您可租车的类型及价目表:n 编号t 车名t 单价tt 容量");
use.getCars();
System.out.println("请选择要租车的数量:");
int numbers = input.nextInt();
//调用租车数量
use.carsNum(numbers);
System.out.println("输入要租的天数: ");
days = input.nextInt();
//调用打印结果
use.display();
}
}
//车目类型
public void getCars(){
Bus audi = new Bus(1, "奥迪a4", 500f, 5);
Bus mzd = new Bus(2, "马自达6", 400f, 5);
Truck shj = new Truck(3, "松花江", 400f, 4);
Truck ywk = new Truck(4, "依维柯", 1000f, 20);
PickUp mq = new PickUp(5, "猛禽", 1200f, 5, 5);
PickUp cc = new PickUp(6, "长城", 600f, 5, 3);
Cars cars[] = {audi, mzd, shj, ywk, mq, cc};
for(Cars car:cars){
if(car.getClass() == Bus.class) {
System.out.println(car.id + "t" + car.carName + "t" + car.unitPrice + "/天tt" + car.carryMan + "人");
} else if(car.getClass() == Truck.class) {
System.out.println(car.id + "t" + car.carName + "t" + car.unitPrice + "/天tt" + car.carryGoods + "吨");
} else if(car.getClass() == PickUp.class) {
System.out.println(car.id + "t" + car.carName + "t" + car.unitPrice + "/天tt" + car.carryMan + "人," + car.carryGoods + "吨");
}
}
}
//租车数量
void carsNum(int numbers) {
//选择每辆车的编号组成的数组
Scanner input = new Scanner(System.in);
//定义数组长度
carsId = new int[numbers];
for(int i = 1; i }
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1600/viewspace-2798914/ ,如需转载,请注明出处,否则将追究法律责任。