入口类
package text;
import java.util.Scanner;
public class Initail {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner input = new Scanner(System.in);
Car[] Garage = {new PassengerCar("奥迪A4",4,500),new PassengerCar("马自达6",4,400),new PickUp("皮卡雪6",4,2,450),
new PassengerCar("金龙",20,800),new Truck("松花江",4,400),new Truck("依维柯",20,1000)}; //建立车库。
System.out.println("*******您是否要租车 (任意数.是 0.否)");
int op = input.nextInt();
if(op == 1){
System.out.println("*******您可租车的类型及其价目表:");
System.out.println("序号 汽车名称 租金 功能");
for(int i=0;i<6;i++){
Garage[i].print(i); //循环遍历,打印车库中的类型
}
System.out.println("请输入您要租汽车的数量:");
int num = input.nextInt();
String zaike=" "; //建立一个变量,用来存载客车的名字字符串
String zaihuo=" "; //建立一个变量,用来存载货车的名字字符串
int passengeradd=0; //建立一个变量,用来存一共载客的数量
int tonnageadd=0; //建立一个变量,用来存一共载货的重量
int money = 0; //建立一个变量,用来存每天租车的费用
for(int j=0;j<num;j++){
System.out.println("请输入第"+(j+1)+"辆车的序号:");
switch(input.nextInt()){ //选择结构,选择需要租的车,并且将选中的车的信息存入相应的变量中
case 1 : zaike= zaike+Garage[0].name+" ";
passengeradd+=Garage[0].passenger;
money+=Garage[0].price;
break;
case 2 : zaike = zaike+Garage[1].name+" ";
passengeradd+=Garage[1].passenger;
money+=Garage[1].price;
break;
case 3 : zaike = zaike + Garage[2].name+" ";
zaihuo+=zaihuo+Garage[2].name+" ";
passengeradd+=Garage[2].passenger;
tonnageadd+=Garage[2].tonnage;
money+=Garage[2].price;
break;
case 4 :zaike = zaike+ Garage[3].name+" ";
passengeradd+=Garage[3].passenger;
money+=Garage[3].price;
break;
case 5:zaihuo = zaihuo+Garage[4].name+" ";
tonnageadd+=Garage[4].tonnage;
money+=Garage[4].price;
break;
case 6: zaihuo = zaihuo+Garage[5].name+" ";
tonnageadd+=Garage[5].tonnage;
money+=Garage[5].price;
break;
}
}
System.out.println("请输入租车天数:");
int day = input.nextInt();
System.out.println("您的账单:");
System.out.println("可载人的车有:");
System.out.println(zaike+" 共载人:"+passengeradd+"人");
System.out.println("可载货的车有:");
System.out.println(zaihuo+" 共载货:"+tonnageadd+"吨");
System.out.println("总价格:"+(money*day)+"元");}
else{
System.out.println("系统正在爆炸。");
}
}
}
车库
package text;
public abstract class Car {
public String name; //车名
public int number; //编号
public int passenger; //载客人数
public int price; //价钱
public int tonnage; //拉货吨数
public abstract void print(int i); //输出列表
}
客车类
package text;
public class PassengerCar extends Car {
@Override
public void print(int i) {
number = i + 1;
System.out.println(number+". "+name+" "+price+"元/天 "+"载人:"+passenger+"人");
}
public PassengerCar(String name,int passenger,int price){
this.name = name;
this.price = price;
this.passenger = passenger;
}
}
皮卡类
package text;
public class PickUp extends Car {
public void print(int i){
number = i + 1 ;
System.out.println(number+". "+name+" "+price+"元/天 "+"载人:"+passenger+"载货:"+tonnage+"人");
}
public PickUp(String name,int passenger,int tonnage,int price ){
this.name = name;
this.passenger = passenger;
this.tonnage = tonnage;
this.price = price;
}
}
货车类
package text;
public class Truck extends Car {
@Override
public void print(int i) {
// TODO 自动生成的方法存根
number = i + 1 ;
System.out.println(number+". "+name+" "+price+"元/天 "+"载货:"+tonnage+"人");
}
public Truck(String name,int tonnage,int price){
this.name = name;
this.tonnage = tonnage;
this.price = price;
}
}
热门评论
写的很好啊!思路好清晰。
加油 !!!十个字!!!!!!!!!!!
写的不错加油! Car里面我认为不用写那个抽象类, 你只需要把分类重写toString方法 然后自己编译好些什么就好了。不过这么写也没毛病。。