學(xué)過C語言,這個小項目我也寫了,但是習(xí)慣性用函數(shù)的方式去寫,不習(xí)慣用接口和抽象類。。。。
package 達(dá)達(dá)租車;
public class Vehicle {
?String []str={"奧迪A4","馬自達(dá)6","皮卡雪6","金龍 ","松花江","依維柯 ?"};
?int []p={500,400,450,800,400,1000};
?
? ? ?void information(){
? ? System.out.println("你可租車的類型及其價格表:");
? ? System.out.println("序號 ? ? ? ? ? ?汽車名稱 ? ? ? ? ? ? ? ?租金 ? ? ? ? ? ? ? ? ? ? ? ?容量 ? ");
? ? System.out.println("1. ? ?奧迪A4 ? ? 500元/天 ? ? ? ? ? 載人:4人 ? ? ?");
? ? System.out.println("2. ? ?馬自達(dá)6 ? ? 400元/天 ? ? ? ? ? 載人:4人 ? ? ?");
? ? System.out.println("3. ? ?皮卡雪6 ? ? 450元/天 ? ? ? ? ? 載人:4人 ? 載貨:2噸 ? ");
? ? System.out.println("4. ? ?金龍 ? ? ? ? ? 800元/天 ? ? ? ? ? 載人:20人 ? ? ?");
? ? System.out.println("5. ? ?松花江 ? ? ? ?400元/天 ? ? ? ? ? 載貨:4噸 ? ? ");
? ? System.out.println("6. ? ?依維柯 ? ? ? ?1000元/天 ? ? ? ?載貨:20噸 ? ? ?");
? ? ?}
? ? ?
? ? ?void showcar1(int a,int b[]){
? ? System.out.println("***可載人的車有:");
? ? ?
? ? for(int i=0;i<a;i++){
? ? if(b[i]<5)
? ? System.out.print(str[b[i]-1]+" ? "); ? ? ?
? ? }
? ? ?
? ? ?}
? ? ?
? ? ?void showcar2(int a,int b[]){
? ? System.out.println("\n***可載貨的車有:");
? ? for(int i=0;i<a;i++){
? ? if(b[i]>5||b[i]==3)
? ? System.out.print(str[b[i]-1]+" ? ");
? ?
? ? }
? ? ?}
? ? ? ? ?
? ? ?
? ? ?
? ? ?void bill(int a,int d,int b[]){ ? ?
? ? int m=0;
? ? for(int i=0;i<a;i++){
? ? ? ? m=m+(p[b[i]-1]*d);
? ? }
? ? System.out.println("\n***租車總價格:"+m);
? ? ?
? ??
? ? ?}
}
package 達(dá)達(dá)租車;
import java.util.*;
public class Carrental {
public static void main(String[] args) {
// TODO Auto-generated method stub
? ? ? ? Vehicle v=new Vehicle();
? ? ? ??
? ? ? ? Scanner reader=new Scanner(System.in);
? ? ? ? System.out.println("歡迎使用達(dá)達(dá)租車系統(tǒng):");
? ? ? ? System.out.println("你是否要租車:1是 ? 0否");
? ? ? ??
? ? ? ? int s=reader.nextInt();
? ? ? ? if(s==1){
? ? ? ? ? ?v.information();
? ? ? ? ? ?System.out.println("請輸入您要租汽車的數(shù)量:");
? ? ? ? ? ?int a=reader.nextInt();
? ? ? ? ? ?int b[]=new int[a];
? ? ? ? ? ?for(int i=0;i<a;i++){
? ? ? ? ? System.out.println("請輸入第"+(i+1)+"輛車的序號:");
? ? ? ? ? b[i]=reader.nextInt();
? ? ? ? ? ?}
? ? ? ? ? ?System.out.println("請輸入租車天數(shù):");
? ? ? ? ? ?int d=reader.nextInt();
? ? ? ? ? ?System.out.println("您的賬單:");
? ? ? ? ? ?v.showcar1(a,b);
? ? ? ? ? ?v.showcar2(a,b); ??
? ? ? ? ? ?v.bill(a,d,b);
? ? ? ? }
? ? ? ? else
? ? ? ? System.exit(0);
}
}
2015-12-16
那就要習(xí)慣用面向?qū)ο笏枷?,函?shù)方式的代碼太多太亂,太復(fù)雜,結(jié)構(gòu)非常難懂,不便于管理。