感覺我的接口用處不是很大,有沒有辦法優(yōu)化一下
package newpackage;
/**
?*
?* @author Administrator
?*/
public class car {
? ?private String name="";
? ? private double money=0;
? ? public car(){}
? ? public car(String name,double money)
? ? {
? ? this.name=name;
? ? this.money=money;
? ? }
? ? public void useway() { System.out.println("車"); }
? ? public String getname(){return name;}
? ? public double getmoney(){return money;}
? ? public String toString()
? ? {
? ? return "name:"+name+" ?"+"money:"+money;
? ? }
}
package newpackage;
/**
?*
?* @author Administrator
?*/
public interface takepeople {
? ? public int getpeoplenum();
}
package newpackage;
/**
?*
?* @author Administrator
?*/
public interface takegoods {
? ? public int getgoodsnum();
}
package newpackage;
/**
?*
?* @author Administrator
?*/
public class 奧迪A4 extends car implements takepeople{
? private int peoplenum=0;
? ? public 奧迪A4()
? ? {
? ? super();
? ? }
? ?public 奧迪A4(String name,double money,int x)
? ?{
? ?super(name,money);
? ?peoplenum=x;
? ?}
? ?public int getpeoplenum()
? ?{
? return peoplenum;
? ?}
? ? public String toString()
? ? {
? ? return "車名:奧迪A4"+super.toString()+"載人量:"+peoplenum;
? ? }
? ? ? }
package newpackage;
/**
?*
?* @author Administrator
?*/
public class 馬自達(dá)6 extends car implements takepeople{
? ? ?private int peoplenum=0;
? ? public 馬自達(dá)6()
? ? {
? ? super();
? ? }
? ?public 馬自達(dá)6(String name,double money,int x)
? ?{
? ?super(name,money);
? ?peoplenum=x;
? ?}
? ?public int getpeoplenum()
? ?{
? return peoplenum;
? ?}
? ?public String toString()
? ? {
? ? return "車名:馬自達(dá)6"+super.toString()+"載人量:"+peoplenum;
? ? }
}
package newpackage;
/**
?*
?* @author Administrator
?*/
public class 松花江貨車 extends car implements takegoods {
? ? private int goodsnum=0;
? ? public 松花江貨車()
? ? {
? ? super();
? ? }
? ?public 松花江貨車(String name,double money,int x)
? ?{
? ?super(name,money);
? ?goodsnum=x;
? ?}
? ?public int getgoodsnum()
? ?{
? return goodsnum;
? ?}
? ? public String toString()
? ? {
? ? return "車名:松花江貨車"+super.toString()+"載貨量:"+goodsnum;
? ? }
}
package newpackage;
/**
?*
?* @author Administrator
?*/
public class 皮卡雪6 extends car implements takegoods,takepeople{
? ? ?private int peoplenum=0;
? private int goodsnum=0;
?public 皮卡雪6()
? ? {
? ? super();
? ? }
? public 皮卡雪6(String name,double money,int peoplenum,int goodsnum )
? ?{
? ?super(name,money);
? ?this.peoplenum=peoplenum;
? ?this.goodsnum=goodsnum;
? ?}
public int getgoodsnum()
? ?{
? return goodsnum;
? ?}
?public int getpeoplenum()
? ?{
? return peoplenum;
? ?}
? public String toString()
? ? {
? ? return "車名:松花江貨車"+super.toString()+"載人量"+peoplenum+"載貨量:"+goodsnum;
? ? }
}
package newpackage;
/**
?*
?* @author Administrator
?*/
public class 金龍 ?extends car implements takegoods {
? ? ?private int goodsnum=0;
? ? public 金龍 ()
? ? {
? ? super();
? ? }
? ?public 金龍 (String name,double money,int x)
? ?{
? ?super(name,money);
? ?goodsnum=x;
? ?}
? ?public int getgoodsnum()
? ?{
? return goodsnum;
? ?}
? ? public String toString()
? ? {
? ? return "車名:松花江貨車"+super.toString()+"載貨量:"+goodsnum;
? ? }
}
package newpackage;
import java.util.*;
/**
?*
?* @author Administrator
?*/
public class initail {
? ? public static void main(String[] atgs)
? ? {
? ? ? ?奧迪A4 aodi=new 奧迪A4("奧迪A4",500,4);
? ? ? ?松花江貨車 shjhc=new ? 松花江貨車("松花江貨車",400,4);
? ? ? ?皮卡雪6 pikaxue=new 皮卡雪6("皮卡雪6",450,4,2);
? ? ? ?金龍 jinglong=new 金龍("金龍",800,20);
? ? ? ?馬自達(dá)6 mazida=new 馬自達(dá)6("馬自達(dá)6",400,4);
? ? ? ?System.out.println("歡迎使用達(dá)達(dá)租車系統(tǒng)");
? ? ? ?Scanner in=new Scanner(System.in);
? ? ? ? System.out.println("您是否要租車:1是 0否");
? ? ? ?int input=in.nextInt();
? ?if(input==0)
? ? ? ?return;
? ? System.out.println("您可租車的類型和價目表");
? ? System.out.println("1"+aodi);
? ? System.out.println("2"+shjhc);
? ? System.out.println("3"+pikaxue);
? ? System.out.println("4"+jinglong);
? ? System.out.println("5"+mazida);
System.out.println("請輸入你要租車的數(shù)量");
? ?int inp=in.nextInt();
? ?double money=0;
? ? int sumday=0;
? ?for(int i=0;i<inp;i++)
? ?{
? ? ? ?System.out.println("請輸入車的序號和天數(shù)");
? ?
? ? ? ? ? ?int nex=in.nextInt();
? ? ? ? ? ?int day=in.nextInt();
? ? ? ? ??
? ? ? ? ? ?sumday=sumday+day;
? ? ? ? while(nex<0||nex>5)
? ? ? ? {
? ? ? ? ?System.out.println("請重新輸入車的序號");
? ? ? ? ? nex=in.nextInt();
? ? ? ? }
? ? ? ? ? ??
? ? ? ? ? ? ? ?switch(nex)
? ?{
? ? ? ? ? ?case 1:
? ? ? ? ? money=money+aodi.getmoney()*day;
? ? ? ? ? ?break;
? ? ? ? ? ?case 2:
? ? ? ? ? ?money=money+shjhc.getmoney()*day;
? ? ? ? ? ?break;
? ? ? ? ? ? case 3:
? ? ? ? ? ? money=money+pikaxue.getmoney()*day;
? ? ? ? ? ? break;
? ? ? ? ? ? ?case 4:
? ? ? ? ? ? money=money+jinglong.getmoney()*day;
? ? ? ? ? ? break;
? ? ? ? ? ? case 5:
? ? ? ? ? ? ? money=money+mazida.getmoney()*day;
? ? ? ? ? ? ? break;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? break;
? ? ?} ?
? ? ? ? ? ?}
? ?
? ? ? ? ? ? ? ?
? ?
? ? ?
? ? System.out.println("all money="+money+"yuan"+"sumday="+sumday);
? ? ?
? ? } ? ??
? ??
? ? ??
? ?}
? ? ? ? ??
??
2016-03-28
代碼大致功能實現(xiàn)了,有些細(xì)節(jié)還沒弄好,我想先請教下在結(jié)構(gòu)上有沒有可以優(yōu)化的地方。先上main函數(shù):
package project;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Aodi tp1=new Aodi(1,"奧迪A4",500,4);
Mazida tp2=new Mazida(2,"馬自達(dá)6",400,4);
Pika tp3=new Pika(3,"皮卡雪6",450,4,2);
Jinglong tp4=new Jinglong(4,"金龍",800,20);
Songhuajiang tp5=new Songhuajiang(5,"松花江",400,4);
Yiweike tp6=new Yiweike(6,"依維柯",1000,20);
/*先將各個子類實例化,并且賦上初值*/
System.out.println("您是否要租車?1.是 0.否");
Scanner input=new Scanner(System.in);
int a=input.nextInt();
if(a==0){
System.out.println("謝謝");}
else{
? ?System.out.println("租車列表為:\n"+tp1.toString()+"\n"+
? ? ? ? ? tp2.toString()+"\n"+tp3.toString()+"\n"+tp4.toString()+"\n"+
? ? ? ?tp5.toString()+"\n"+tp6.toString());
? ?/*將租車的信息輸出*/
System.out.println("請輸入租車數(shù)量:");
int b=input.nextInt();
int[]c;
c=new int[b];
System.out.println("請輸入序號:");
for(int i=0;i<c.length;i++)
c[i]=input.nextInt();
/*把選擇的車的序號賦給c[]數(shù)組*/
String str1="",str2="";
int sum1=0;int sum2=0;int sum3=0;
for(int i=0;i<c.length;i++){
int d=c[i];
switch (d){
case 1: {sum1=sum1+tp1.zaike;sum3=sum3+tp1.rent;break;}
case 2: {sum1=sum1+tp2.zaike;sum3=sum3+tp2.rent;break;}
case 3: {sum1=sum1+tp3.zaike;sum2=sum2+tp3.zaihuo;sum3=sum3+tp3.rent;break;}
case 4: {sum1=sum1+tp4.zaike;sum3=sum3+tp4.rent;break;}
case 5: {sum2=sum2+tp5.zaihuo;sum3=sum3+tp5.rent;break;}
case 6: {sum2=sum2+tp6.zaihuo;sum3=sum3+tp6.rent;break;}
}
/*swtich語句主要用于計算載人總?cè)藬?shù)sum1,載貨總噸數(shù)sum2,和租金總數(shù)*/
str1=str1+tp1.say(d)+tp2.say(d)+ tp3.say(d)+ tp4.say(d);
str2=str2+tp3.say(d)+ tp5.say(d)+ tp6.say(d);
/*用于輸出選擇的車的型號,定義一個字符變量str1,代表可以載人的車的型號。
?* 因為調(diào)用了子類say語句,str1變量處于for循環(huán)內(nèi),所以每次循環(huán)(d值改變)
?* 都可以給str1添加一個租車的字符,str2同理*/
if (i==c.length-1){
System.out.println("可以載人的車為:"+str1+" "+"載人人數(shù)為:"+sum1);
System.out.println("可以載貨的車為:"+str2+ " "+"載貨噸數(shù)為:"+sum2+"\n"+"總租金為"+sum3);}
/*i==c.length-1代表循環(huán)已經(jīng)結(jié)束,租車的型號都已經(jīng)賦給了str1,str2,sum也都
* 計算完畢,故可以輸出了*/
}
}
}
}
父類Car:
子類奧迪:
子類馬自達(dá):
子類皮卡雪:
后面的幾個子類格式都是一樣的,就不一個個寫了,顯得太亂。其實我覺得應(yīng)該在父類里面寫一個say()方法,讓子類重寫,但是在調(diào)用的時候貌似并不能簡化多少,因為在main方法里還是要將每個子類逐一實例化,不知道大家有沒有可以優(yōu)化的地方,求賜教。
2016-03-20
個人覺得你完全可以將接口中的方法在父類中設(shè)定為抽象類,車都有這種特性的,只不過有的子類實現(xiàn),有的子類不實現(xiàn)