研究了兩天的代碼,求大神指點(diǎn)
//父類
package com.tow;
public abstract class Car {
int number;
String name;
float price;
public Car(int number,String name,float price){
this.number=number;
this.name=name;
this.price=price;
}
? ? @Override
public String toString() {
return number+"\t"+name+"\t"+price+"\n";
}
public int getCarcapacity() {
// TODO Auto-generated method stub
return 0;
}
public int getTrackcapacity() {
// TODO Auto-generated method stub
return 0;
}
? ??
}
//子類1
package com.tow;
public class Cars extends Car {
private int Carcapacity;
public Cars(int number, String name, float price,int Carcapacity) {
super(number, name, price);
this.setCarcapacity(Carcapacity);
// TODO Auto-generated constructor stub
}
public int getCarcapacity() {
return Carcapacity;
}
public void setCarcapacity(int carcapacity) {
Carcapacity = carcapacity;
}
@Override
public String toString() {
return number+"\t"+name+"\t"+price+"\t\t"+Carcapacity+"\n";
}
}
//子類2
package com.tow;
public class Track extends Car {
private int Trackcapacity;
public Track(int number, String name, float price,int Trackcapacity) {
super(number, name, price);
this.setTrackcapacity(Trackcapacity);
// TODO Auto-generated constructor stub
}
public int getTrackcapacity() {
return Trackcapacity;
}
public void setTrackcapacity(int trackcapacity) {
Trackcapacity = trackcapacity;
}
@Override
public String toString() {
return number+"\t"+name+"\t"+price+"\t\t\t"+Trackcapacity+"\n";
}
}
//子類3
package com.tow;
public class Pickup extends Car {
private int Carcapacity;
private int Trackcapacity;
public Pickup(int number, String name, float price,int Carcapacity,int Trackcapacity) {
super(number, name, price);
this.setCarcapacity(Carcapacity);
this.setTrackcapacity(Trackcapacity);
// TODO Auto-generated constructor stub
}
public int getCarcapacity() {
return Carcapacity;
}
public void setCarcapacity(int carcapacity) {
Carcapacity = carcapacity;
}
public int getTrackcapacity() {
return Trackcapacity;
}
public void setTrackcapacity(int trackcapacity) {
Trackcapacity = trackcapacity;
}
@Override
public String toString() {
return number+"\t"+name+"\t"+price+"\t\t"+Carcapacity+"\t"+Trackcapacity+"\n";
}
}
//測(cè)試類
package com.tow;
import java.util.*;
public class Initail {
public static void main(String[] args) {
// TODO Auto-generated method stub
? ? @SuppressWarnings("resource")
Scanner input=new Scanner (System.in);
? ? System.out.println("##歡迎使用DADA租車系統(tǒng)##");
? ? System.out.println("請(qǐng)問(wèn)您是否租車:1.是,2.否");
? ? int choice=input.nextInt();
? ? switch(choice){
? ? case 1:
? ? System.out.println("下面是租車信息:");
? ? System.out.println("序號(hào)"+"\t"+"車型"+"\t"+"單價(jià)(天/元)"+"\t"+"載客以及載貨量(t)");
? ? Car[] news=new Car[5];
? ? news[0]=new Cars(1,"奧迪A8",800,5);
? ? news[1]=new Cars(2,"金龍大巴",700,50);
? ? news[2]=new Track(3,"金杯",400,45);
? ? news[3]=new Track(4,"東風(fēng)悅達(dá)",650,70);
? ? news[4]=new Pickup(5,"皮卡",500,4,60);
? ? System.out.println(Arrays.toString(news));
? ? System.out.print("請(qǐng)您輸入租賃天數(shù):");
? ? ?int day=input.nextInt();
? ? ?System.out.println("租用:"+day+"天");
? ? ?System.out.println("請(qǐng)您根據(jù)車型序號(hào)選擇車型,輸入“8”結(jié)束");
? ? ?int[] rent=new int[6];
? ? for(int i=1;i<6;i++){
? ?
? ? ? ? System.out.print("請(qǐng)輸入您要選擇的車型:");
? ? ? ? ? int score1=input.nextInt();
? ? ? ? ? if(score1==8){
? ? ? ? break;
? ? ? ? ? }else{
? ? ? ? ? ?System.out.print("請(qǐng)輸入您要組的數(shù)量:");
? ? ? ? ? ?int score2=input.nextInt();
? ? ? ? ? ?float sum=news[score1-1].price*day;
? ? ? ? ? ?System.out.println("車型:"+news[score1-1].name+"\t"+score2+"輛"+"\t"+"合計(jì)"+sum+"元");?
? ? ? ? ? ?float sum2=0;
? ? ? ? ? ?sum2=sum+sum2;
? ? ? ? ? ?System.out.println("總計(jì):"+sum2);
? ? ? ? ? ?rent[score1]=score2;
? ? }
? ? ? ? ??
? ? }
? ?
? ? System.out.println("如下是您的租車信息,請(qǐng)您核對(duì):");
? ? float sum=0;
? ? for(int i=0;i<6;i++){
? ? if(rent[i]!=0){
? ? System.out.println(news[i-1].name+":\t"+rent[i]+"輛"+"\t"+"單價(jià)(元/天):"+rent[i]*news[i-1].price+"\t\t"+"載客量:"+"\t"+rent[i]*news[i-1].getCarcapacity()+"人"+
? ? ? ? "\t"+"載重量:"+rent[i]*news[i-1].getTrackcapacity()+"噸"+"\t"+"合計(jì):"+rent[i]*(rent[i]*news[i-1].price)*day+"元");
? ? sum+=rent[i]*rent[i]*news[i-1].price*day;
? ? }
? ?
? ? }
? ? System.out.println("租車總費(fèi)用:"+sum+"元");
? ? break;
? ? case 2:
? ? System.out.println("歡迎使用,再見(jiàn)!");
? ? break;
? ? default:
? ? System.out.println("您的輸入有誤,請(qǐng)重新輸入!");
? ? }
? ??
}
}
2017-07-07
可以去面試去了