package com.chen1;?import java.util.Scanner;?public class Demo2 {? ? ? ? ?? ? public static void main(String[] args) {? ? //創(chuàng)建可租車列表? ? Car[] carsForRent={new PassengerCar("奧迪A4",500,4),new PassengerCar("馬自達(dá)6",400,4),? ? new PassengerCar("金龍",800,20),new Pickup("皮卡雪",450,2,4),? ? new Trunk("松花江",400,4),new Trunk("大阿肥",1000,20)};? ? System.out.println("歡迎使用達(dá)達(dá)租車系統(tǒng)");? ? System.out.println("您是否需要租車:1 是 \t其他任意鍵 否");? //獲取并保存輸入的值(一或者其他任意鍵)? ? Scanner scan=new Scanner(System.in);? ?String input=scan.next();? //判斷是否進(jìn)入租車系統(tǒng)? ?if(input.equals("1")){ ? System.out.println("你可租車類型及價(jià)目表"); ? System.out.println("序號\t汽車名稱\t租金 ? ? ? ? ? ? ? ? \t ? 容量");? int i=0;? for(Car currentCar:carsForRent){ //用instancreof判斷是否屬于汽車子類 ?if(currentCar instanceof PassengerCar){ ?i++; ?System.out.println(""+i+"\t"+currentCar.getName()+"\t"+currentCar.getRent()+"元/天\t"+((PassengerCar)currentCar).getpeopleCapacity()+"人\t" ? );} ?if(currentCar instanceof Trunk){ ?i++; ?System.out.println(""+i+"\t"+currentCar.getName()+"\t"+currentCar.getRent()+"元/天\t"+((Trunk)currentCar).getcargoCapacity()+"噸\t");} ?if(currentCar instanceof Pickup){ ?i++; ?System.out.println(""+i+"\t"+currentCar.getName()+"\t"+currentCar.getRent()+"元/天\t"+((Pickup)currentCar).getCargoCapacity()+"噸\t" ? +((Pickup)currentCar).getPeopleCapacity()+"人\t");}? }??? System.out.println("請輸入您要租車的數(shù)量:");? //獲取并保存輸入的值(租車的數(shù)量)? Scanner inputnum=new Scanner(System.in);?int number=inputnum.nextInt();// 輸入每輛租車的序號for(int j=1;j<=number;j++){System.out.println("請輸入第"+j+"臺車的序號:");Scanner inputnum1=new Scanner(System.in);int number1=inputnum1.nextInt();}//輸入租車天數(shù)System.out.println("請輸入租車天數(shù):");Scanner inputnum2=new Scanner(System.in);int days=inputnum2.nextInt();//租車賬單System.out.println("您的賬單:");double total=0;Car car=new PassengerCar();total=car.getRent()*days;? System.out.println("***租車總價(jià)格:"+total);?}? ?? ?else { ? System.out.println("您已退出達(dá)達(dá)租車系統(tǒng)!");? ?}??? ? ? ? ? ??? ? }? ? }?//汽車類class Car{ //成員屬性String name;?double carCapacity;?double rent;?//獲取名字public String getName() { return name;}//設(shè)定名字public void setName(String name) { this.name = name;}//獲取汽車容量public double getCarCapacity() { return carCapacity;}//設(shè)定汽車容量public void setCarCapacity(double carCapacity) { this.carCapacity = carCapacity;}//獲取租金public double getRent() { return rent;}//設(shè)定租金public void setRent(double rent) { this.rent = rent;} }//子類卡車?yán)^承父類汽車class Trunk extends Car{ //卡車容量屬性封裝? ? private double cargoCapacity;// ?構(gòu)造方法 ?,自動調(diào)用構(gòu)造方法完成對新對象的初始化 public Trunk(String name,double rent,double cargoCapacity){ this.name=name; this.rent=rent; this.cargoCapacity=cargoCapacity; } //獲取卡車載貨容量 public double getcargoCapacity(){ return cargoCapacity; } }//子類客車?yán)^承父類汽車class PassengerCar extends Car{ //客車載入容量屬性封裝 private double peopleCapacity; //構(gòu)造方法,完成對新對象的屬性初始化public PassengerCar(String name,double rent,double peopleCapacity){ this.name=name; this.rent=rent; this.peopleCapacity=peopleCapacity; }?//獲取客車載入的容量public double getpeopleCapacity(){ return peopleCapacity;}}//子類class Pickup extends Car{ private double cargoCapacity; private double peopleCapacity; public Pickup(String name,double rent,double cargoCapacity,double peopleCapacity){ this.name=name; this.rent=rent; this.cargoCapacity=cargoCapacity; this.peopleCapacity=peopleCapacity; } public double getCargoCapacity() { return cargoCapacity; } public double getPeopleCapacity() { return peopleCapacity; } }
跪求大神解答
qq_82年的彬鍋鍋_03446807
2016-06-25 18:19:33