有些知識點在這課程中沒有體現,需要翻資料慢慢體會
public abstract class Vehicle {
public String name;
public int rent;
public Vehicle() {
}
public Vehicle(String name,int rent) {
this.name=name;
this.rent=rent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public abstract String displayCar();
}
public class FamilyCar extends Vehicle{
private int personsCap;
public FamilyCar(String name,int rent,int personsCap) {
this.name=name;
this.rent=rent;
this.personsCap=personsCap;
}
public int getPersonsCap() {
return personsCap;
}
public void setPersonsCap(int personsCap) {
this.personsCap=personsCap;
}
public String displayCar() {
return "\t"+this.name+"\t"+this.rent+"元/天"+"\t\t"+"載人:"+this.personsCap+"人";
}
}
public class Trunk extends Vehicle{
private int cagoCap;
public Trunk(String name,int rent,int cagoCap) {
this.name=name;
this.rent=rent;
this.cagoCap=cagoCap;
}
public int getCagoCap() {
return cagoCap;
}
public void setCagoCap(int cagoCap) {
this.cagoCap = cagoCap;
}
@Override
public? String displayCar() {
// TODO Auto-generated method stub
return "\t"+this.name+"\t"+this.rent+"元/天"+"\t\t"+"載貨:"+this.cagoCap+"噸";
}
}
public class Pickup extends Vehicle{
private int personsCap;
private int cagoCap;
public Pickup(String name,int rent,int personsCap,int cagoCap) {
this.name=name;
this.rent=rent;
this.personsCap=personsCap;
this.cagoCap=cagoCap;
}
public int getPersonsCap() {
return personsCap;
}
public void setPersonsCap(int personsCap) {
this.personsCap = personsCap;
}
public int getCagoCap() {
return cagoCap;
}
public void setCagoCap(int cagoCap) {
this.cagoCap = cagoCap;
}
@Override
public String displayCar() {
// TODO Auto-generated method stub
return "\t"+this.name+"\t"+this.rent+"元/天"+"\t\t"+"載人:"+this.personsCap+"人,"+"載貨:"+this.cagoCap+"噸";
}
}
import java.util.Scanner;
public class Initial {
public static void main(String[] args) {
//對象數組
Vehicle veh1[]= {
new FamilyCar("奧迪",230,5),
new FamilyCar("寶馬",290,5),
new FamilyCar("奔馳",350,7),
new Trunk("沃爾沃",430,5),
new Trunk("東風",590,6),
new Trunk("長安",350,4),
new Pickup("沃爾沃",330,2,3),
new Pickup("東風",290,2,4),
new Pickup("長安",190,2,2)
};
//判斷顯示清單
System.out.println("歡迎使用噠噠租車系統");
System.out.println("您是否要租車:1是0否");
Scanner sc=new Scanner(System.in);
int input=sc.nextInt();
if(input==1) {
System.out.println("歡迎使用噠噠租車系統,您可租車的類型及其價目表:");
System.out.println("序號\t汽車名稱\t租金\t\t容量");
for(int i=0;i<veh1.length;i++) {
int count =i+1;
System.out.println(count+veh1[i].displayCar());
}
}
else {
System.out.println("退出系統,謝謝!");
System.exit(0);
};
//輸入
System.out.println("請輸入您要租車的數量:");
Scanner input1=new Scanner(System.in);
int carAmount=input1.nextInt();
int totalPrice=0;
int totalPersonCap=0;
int totalCagoCap=0;
int p=0;
int q=0;
//創建數組存放篩選值
Vehicle[] sumPersonCap=new Vehicle[carAmount];
Vehicle[] sumCagoCap=new Vehicle[carAmount];
for(int j=1;j<=carAmount;j++) {
System.out.println("請輸入第"+j+"輛車的序號");
Scanner input2=new Scanner(System.in);
int carNum=input2.nextInt();
totalPrice+=veh1[carNum-1].getRent();
if(veh1[carNum-1] instanceof FamilyCar) {
totalPersonCap+=((FamilyCar)veh1[carNum-1]).getPersonsCap();
sumPersonCap[p++]=veh1[carNum-1];
}
//
if(veh1[carNum-1] instanceof Trunk) {
totalCagoCap+=((Trunk)veh1[carNum-1]).getCagoCap();
sumCagoCap[q++]=veh1[carNum-1];
}
if(veh1[carNum-1] instanceof Pickup) {
totalPersonCap+=((Pickup)veh1[carNum-1]).getPersonsCap();
totalCagoCap+=((Pickup)veh1[carNum-1]).getCagoCap();
sumPersonCap[p++]=veh1[carNum-1];
sumCagoCap[q++]=veh1[carNum-1];
}
}
//輸出結果
System.out.println("***請輸入租車的天數:");
Scanner input3=new Scanner(System.in);
int rentDay=input3.nextInt();
totalPrice*=rentDay;
System.out.println("***可載人的車有:");
for(int k=0;k<p;k++) {
System.out.print(sumPersonCap[k].getName()+"\t");
}
System.out.println("共載人:"+totalPersonCap+"人");
System.out.println("***可載貨的車有:");
for(int l=0;l<q;l++) {
System.out.print(sumCagoCap[l].getName()+"\t");
}
System.out.println("共載貨:"+totalCagoCap+"噸");
System.out.println("***租車的總價格:"+totalPrice+"元");
}
}
2020-01-19
大神好,代碼里面的 name 和rent 為何是public 不是private的呢?
2019-12-26
大神?。。。?/p>