以前只学过C,刚看完Java两季,第一次写这么长程序,希望能和大家互相交流一下。
创建父类Car
public class Car {
String name;
int num;
double rentPrice;
public String getName() {
return name;
}
public double getRentPrice() {
return rentPrice;
}
}
创建支持载货的接口ICargoCapacity
public interface ICargoCapacity {
public double CargoCapacity();
}
创建支持载人的接口IPeopleCapacity
public interface IPeopleCapacity {
public int PeopleCapacity();
}
创建子类Audi
public class Audi extends Car implements IPeopleCapacity {
int num=1;
String name="奥迪A4";
double rentPrice=500;
public String getName() {
return name;
}
public double getRentPrice() {
return rentPrice;
}
@Override
public int PeopleCapacity() {
// TODO Auto-generated method stub
int peopleCapacity=4;
return peopleCapacity;
}
public String toString(){
return num+". "+name+" "+rentPrice+"/天 载人:"+this.PeopleCapacity()+"人";
}
}
创建子类Mazda
public class Mazda extends Car implements IPeopleCapacity {
int num=2;
String name="马自达6";
double rentPrice=400;
public String getName() {
return name;
}
public double getRentPrice() {
return rentPrice;
}
@Override
public int PeopleCapacity() {
// TODO Auto-generated method stub
int peopleCapacity=4;
return peopleCapacity;
}
public String toString(){
return num+". "+name+" "+rentPrice+"/天 载人:"+this.PeopleCapacity()+"人";
}
}
创建子类Pika
public class Pika extends Car implements IPeopleCapacity, ICargoCapacity {
int num=3;
String name="皮卡雪6";
double rentPrice=450;
public String getName() {
return name;
}
public double getRentPrice() {
return rentPrice;
}
@Override
public double CargoCapacity() {
// TODO Auto-generated method stub
double cargoCapacity=2;
return cargoCapacity;
}
@Override
public int PeopleCapacity() {
// TODO Auto-generated method stub
int peopleCapacity=4;
return peopleCapacity;
}
public String toString(){
return num+". "+name+" "+rentPrice+"/天 载人:"+this.PeopleCapacity()+"人"+"载货:"+this.CargoCapacity()+"吨";
}
}
创建子类Jinlong
public class Jinlong extends Car implements IPeopleCapacity {
int num=4;
String name="金龙";
double rentPrice=800;
public String getName() {
return name;
}
public double getRentPrice() {
return rentPrice;
}
@Override
public int PeopleCapacity() {
// TODO Auto-generated method stub
int peopleCapacity=20;
return peopleCapacity;
}
public String toString(){
return num+". "+name+" "+rentPrice+"/天 载人:"+this.PeopleCapacity()+"人";
}
}
创建子类Songhuajiang
public class Songhuajiang extends Car implements ICargoCapacity {
int num=5;
String name="松花江";
double rentPrice=400;
public String getName() {
return name;
}
public double getRentPrice() {
return rentPrice;
}
@Override
public double CargoCapacity() {
// TODO Auto-generated method stub
int cargoCapacity=4;
return cargoCapacity;
}
public String toString(){
return num+". "+name+" "+rentPrice+"/天 载货:"+this.CargoCapacity()+"吨";
}
}
创建子类Yiweike
public class Yiweike extends Car implements ICargoCapacity {
int num=6;
String name="依维柯";
double rentPrice=1000;
public String getName() {
return name;
}
public double getRentPrice() {
return rentPrice;
}
@Override
public double CargoCapacity() {
// TODO Auto-generated method stub
double cargoCapacity=20;
return cargoCapacity;
}
public String toString(){
return num+". "+name+" "+rentPrice+"/天 载货:"+this.CargoCapacity()+"吨";
}
}
主程序
import java.util.Scanner;
public class Initial {
//打印租车列表
public static void Print(Car car[]){
System.out.println("您可租车的类型及价目表:\n序号 汽车名称 租金 容量");
for(int i=0;i<car.length;i++){
System.out.println(car[i].toString());
}
System.out.println("请输入您要租汽车的数量:");
}
public static void main(String[] args) {
System.out.println("欢迎使用答答租车系统:\n您是否要租车:1是 0否");
Scanner input=new Scanner(System.in);
if(input.nextInt()==1){
Car car[]={new Audi(),new Mazda(),new Pika(),new Jinlong(),new Songhuajiang(),new Yiweike()};
Print(car);
getCar(car);
}
else{
input.close();
System.out.println("退出系统");
}
}
//统计所选车的信息
public static void getCar(Car car[]){
Scanner input=new Scanner(System.in);
int totalPeople=0;
double totalCargo=0,totalPrice=0;
String peopleCar=" ",cargoCar=" ";
for(int i=1,carNums=input.nextInt();i<=carNums;i++){
System.out.println("请输入第"+i+"辆车的序号:");
int Num=input.nextInt();
//通过接口判断该车能否载人
if(car[Num-1] instanceof IPeopleCapacity){
totalPeople+=((IPeopleCapacity)car[Num-1]).PeopleCapacity();
peopleCar=peopleCar+car[Num-1].getName()+" ";
}
//通过接口判断该车能否载货
if(car[Num-1] instanceof ICargoCapacity){
totalCargo+=((ICargoCapacity)car[Num-1]).CargoCapacity();
cargoCar=cargoCar+car[Num-1].getName()+" ";
}
totalPrice+=car[Num-1].getRentPrice();
}
System.out.println("请输入租车天数:");
totalPrice*=input.nextInt();
input.close();
System.out.println("您的账单:\n***可载人的车有:\n"+peopleCar+"\t共载人:"+totalPeople+"人");
System.out.println("***载货的车有:\n"+cargoCar+"\t共载货:"+totalCargo+"吨");
System.out.println("***租车总价格:"+totalPrice+"元");
}
}
热门评论
新手不太懂
Print(car);
getCar(car);
的作用是?
你类里面的属性不是private修饰的还要建立get方法干嘛啊
创建好多类 建议用继承啊