package carft;
/*
- 定义车的属性,
-
序号ID,名称name,价格value;
*/
public class Car {
private int id;
private String name;
private int value;/*
- 封装
*/
public int getId() {
return id;
}public void setId(int id) {
this.id = id;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public int getValue() {
return value;
}public void setValue(int value) {
this.value = value;
} - 封装
/*
- 重载
*/
public void printss(){
}
}
package carft;
/*
-
定义一个载客的车类
*/
public class Bus extends Car implements IPassager{Bus(int id,String name,int value,int number){
this.setId(id);
this.setName(name);
this.setValue(value);
this.setIPassage(number);}
private int number;
@Override
public int getIPassage() {
// TODO Auto-generated method stub
return number;
}@Override
public void setIPassage(int number) {
// TODO Auto-generated method stub
this.number=number;
}
public void printss(){
System.out.println(getId()+".\t"+getName()+"\t"+getValue()+"元/天\t"
+getIPassage()+"人\t");
}
}
package carft;
/* -
定义一个既可载客,又可载货的车类
*/
public class Pickup extends Car implements ILoad ,IPassager{Pickup(int id,String name,int value,int number,int weight){
this.setId(id);
this.setName(name);
this.setValue(value);
this.setIPassage(number);
this.setILoad(weight);
}
private int number;
private int weight;
/*- 封装
/
@Override
public int getIPassage() {
// TODO Auto-generated method stub
return number;
}
@Override
public void setIPassage(int number) {
// TODO Auto-generated method stub
this.number=number;
}
@Override
public int getILoad() {
// TODO Auto-generated method stub
return weight;
}
@Override
public void setILoad(int weight) {
// TODO Auto-generated method stub
this.weight=weight;
}
/ - 打印出车子的信息,重载父类
*/
public void printss(){
System.out.println(getId()+".\t"+getName()+"\t"+getValue()+"元/天\t"
+getIPassage()+"人\t"+getILoad()+"吨\t");
}
- 封装
}
package carft;
/*
-
定义一个载货的车类
*/
public class Truck extends Car implements ILoad{Truck(int id,String name,int value,int weight){
this.setId(id);
this.setName(name);
this.setValue(value);
this.setILoad(weight);}
private int weight;public int getWeight() {
return weight;
}public void setWeight(int weight) {
this.weight = weight;
}public void printss(){
System.out.println(getId()+".\t"+getName()+"\t"+getValue()+"元/天\t\t"
+getILoad()+"吨\t");
}@Override
public int getILoad() {
// TODO Auto-generated method stub
return weight;
}@Override
public void setILoad(int weight) {
// TODO Auto-generated method stub
this.weight=weight;
}
}
package carft;
/*
- 定义一个载货的接口
/
public interface ILoad {
int weight=0;
public int getILoad();
public void setILoad(int weight);
}
package carft;
/ - 定义一个载人的接口
-
载客量LoadPassager
/
public interface IPassager {
int number=0;
public int getIPassage();
public void setIPassage(int number);
}
package carft;
import java.util.;
public class Main {void goon(){
/* * 定义车型 */ Car[] cars={ new Bus(1,"奥迪A4",500,4), new Bus(2,"马自达6",400,4), new Pickup(3,"皮卡雪6",450,4,2), new Pickup(4,"金龙",800,2,100), new Truck(5,"松花江",400,4), new Truck(6,"依维柯",1000,20), }; /* * 界面输出租车信息 */ System.out.println("请问你是想要租车吗?1是;2否"); Scanner inputt=new Scanner(System.in); int n=inputt.nextInt(); if(n==1){ System.out.println("可选:\n序号\t车型\t价格\t\t载客\t载重"); for(int i=0;i<cars.length;i++){ cars[i].printss(); } System.out.println("请输入租车信息"); } else{ System.out.println("不要逗我了,哥!"); }
/*
-
设置一个租车序号数组
*/
System.out.println("请输入要租车的总数");
Scanner in1=new Scanner(System.in);int num1=in1.nextInt();
int strxu[]=new int[num1];for( int i=0;i<num1;i++){
int j=i+1;
System.out.println("请输入第"+j+"辆车的序号");Scanner in2=new Scanner(System.in); strxu[i]=in2.nextInt(); if(strxu[i]<0strxu[i]>6){ System.out.println("输入序号有误,请重新输入"); i--; }
}
/*- 设置一个租车时间数组
*/
int strday[]=new int[num1];
for(int i=0;i<num1;i++){
int j=i+1;
System.out.println("请输入租第"+j+"辆车的时间");
Scanner in3=new Scanner(System.in);
strday[i]=in3.nextInt();}
/*- 计算租车总金额
/
int[] price=new int[num1];
int[] sum=new int[num1];
int sumtotal=0;
for(int i=0;i<num1;i++){
int a=strxu[i];
price[i]=cars[a].getValue();
sum[i]=price[i]strday[i];
sumtotal+=sum[i];
System.out.println("第"+a+"辆车的单价为"+price[i]+"租车时间为"+strday[i]+
"该车租用总金额"+sum[i]);
}
System.out.println("租车总金额为"+sumtotal);
System.out.println("感谢光临");
- 设置一个租车时间数组
}
public static void main(String[] args){
Main ss=new Main();
ss.goon();}
-
}
热门评论
载人和载货量直接设置为父类Car的private属性然后封装多好,你这样还要封装三次啊。Car[]的使用倒是有意思,学到了。