純萌新 我感覺我的代碼好繁瑣 有很多地方需要優(yōu)化 改進(jìn)
package project4;
public abstract class Car {
public abstract void price();
}
package project4;
public class Aodi extends Car {
? ? public int num = 1;
? ? public String name = "奧迪A4";
? ? public int rent = 500;
? ? public int manned = 4;
@Override
public void price() {
// TODO Auto-generated method stub
? ? ? ?System.out.println( +num+". ? "+name+" ? "+rent+"/天 ? 載人:" +manned + "人" );
}
}
package project4;
public class Mazida extends Car {
public int num = 2;
? ? public String name = "馬自達(dá)6";
? ? public int rent = 400;
? ? public int manned = 4;
? ? @Override
public void price() {
// TODO Auto-generated method stub
? ? System.out.println( +num+". ? "+name+" ?"+rent+"/天 ? 載人:" +manned + "人" );
}
}
package project4;
public class Pikaxue extends Car {
public int num = 3;
? ? public String name = "皮卡雪6";
? ? public int rent = 450;
? ?public ?int manned = 4;
? ? public float cargo = 2.0f;
? ? @Override
public void price() {
// TODO Auto-generated method stub
? ? System.out.println( +num+". ? "+name+" ?"+rent+"/天 ? 載人:" +manned + "載貨:"+ cargo +"噸" );
}
}
package project4;
public class Jinlong extends Car {
public int num = 4;
? ? public String name = "金龍";
? ? public int rent = 800;
? ? public int manned = 20;
@Override
public void price() {
// TODO Auto-generated method stub
System.out.println( +num+". ? "+name+" ? ? "+rent+"/天 ? 載人:" +manned + "人" );
}
}
package project4;
public class Songhuajiang extends Car {
public int num = 5;
? ? public String name = "松花江";
? ? public int rent = 400;
? ? public float cargo = 4.0f;
? ? @Override
public void price() {
// TODO Auto-generated method stub
? ? System.out.println( +num+". ? "+name+" ? "+rent+"/天 ? 載貨:" +cargo + "噸" );
}
}
package project4;
public class Yiweike extends Car {
public int num = 6;
? ? public String name = "依維柯";
? ? public int rent = 1000;
? ? public float cargo = 20.0f;
@Override
public void price() {
// TODO Auto-generated method stub
System.out.println( +num+". ? "+name+" ?"+rent+"/天 ? 載貨:" +cargo + "噸" );
}
}
package project4;
import java.util.Scanner;
public class Initclass {
public static void main(String[] args) {
// TODO Auto-generated method stub
? ? ? ? ?System.out.println("歡迎使用達(dá)達(dá)租車系統(tǒng)");
? ? ? ? ?System.out.println("您是否要租車:1是 0否");
? ? ? ? ?Scanner input = new Scanner(System.in);//鍵盤輸入
? ? ? ? ?int a = input.nextInt();
? ? ? ? ?if(a==0)?
? ? ? ? ?{
? ? ? ? ? ? ?System.out.println("謝謝您的光臨"); ? ? ? ??
? ? ? ? ?}
? ? ? ? ?else if(a==1)
? ? ? ? ?{
? ? ? ? System.out.println("您可租車的類型及其價(jià)目表:");
? ? ? ? System.out.println("序號 汽車名稱 租金 ? ? 容量");
? ? ? ? Aodi car1 = new Aodi();
? ? ? ? car1.price();
? ? ? ? Mazida car2 = new Mazida();
? ? ? ? car2.price();
? ? ? ? Pikaxue car3 = new Pikaxue();
? ? ? ? car3.price();
? ? ? ? Jinlong car4 = new Jinlong();
? ? ? ? car4.price();
? ? ? ? Songhuajiang car5 = new Songhuajiang();
? ? ? ? car5.price();
? ? ? ? Yiweike car6 = new Yiweike();
? ? ? ? car6.price();
? ? ? ? System.out.println("請輸入您要租汽車的數(shù)量");
? ? ? ? int n = input.nextInt();
? ? ? ? int[] numbers = new int[100];//建立數(shù)組保存汽車序號
? ? ? ? ?
? ? ? ? for(int i=1;i<=n;i++)//循環(huán)輸入客戶所選汽車序號
? ? ? ? {
? ? ? ? ?
? ? ? ? System.out.println("請輸入第"+i+"輛車的序號");
? ? ? ? int numb = input.nextInt();
? ? ? ? if(numb<0||numb>6)
? ? ? ? {
? ? ? ? System.out.println("輸入序號錯(cuò)誤");
? ? ? ? break;
? ? ? ? }
? ? ? ? numbers[i] = numb;
? ? ? ? }
? ? ? ? System.out.println("請輸入租車天數(shù)");
? ? ? ? int day = input.nextInt();
? ? ? ? System.out.println("您的賬單:");
? ? ? ? System.out.println("***可載人的車有:");
? ? ? ? int j;
? ? ? ? int ma=0;
? ? ? ? int money=0;
? ? ? ? for(j=1;j<=n;j++)
? ? ? ? {
? ? ? ?
? ? ? ? if(numbers[j] == car1.num)
? ? ? ? {
? ? ? ? ma = ma + car1.manned;
? ? ? ? System.out.print(car1.name);
? ? ? ? money = money +car1.rent * day;
? ? ? ? }
? ? ? ? if(numbers[j] == car2.num)
? ? ? ? {
? ? ? ? ma = ma + car2.manned;
? ? ? ? ? ? System.out.print(car2.name);
? ? ? ? ? ? money = money +car2.rent * day;
? ? ? ? }
? ? ? ? if(numbers[j] == car3.num)
? ? ? ? {
? ? ? ? ma = ma + car3.manned;
? ? ? ? ? ? System.out.print(car3.name);
? ? ? ? ? ? money = money +car3.rent * day;
? ? ? ? }
? ? ? ? if(numbers[j] == car4.num)
? ? ? ? {
? ? ? ? ma = ma + car4.manned;
? ? ? ? ? ? System.out.print(car4.name);
? ? ? ? ? ? money = money +car4.rent * day;
? ? ? ? }
? ? ? ? System.out.print(" ");
? ? ? ?
? ? ? ? }
? ? ? ? System.out.println("共載人:"+ ma +"人");
? ? ? ? System.out.println("***載貨的車有:");
? ? ? ? int k;
? ? ? ? float weight=0;
? ? ? ? ? ? ?for(k=1;k<=n;k++)
? ? ? ? ? ? ?{
? ? ? ? ? ? if(numbers[k] == car3.num)
? ? ? ? ? ? {
? ? ? ? ? ? weight = weight + car3.cargo;
? ? ? ? ? ? System.out.print(car3.name);
? ? ? ? ? ? }
? ? ? ? ? ? if(numbers[k] == car5.num)
? ? ? ? ? ? {
? ? ? ? ? ? weight = weight + car5.cargo;
? ? ? ? ? ? System.out.print(car5.name);
? ? ? ? ? ? money = money + car5.rent * day;
? ? ? ? ? ? }
? ? ? ? ? ? if(numbers[k] == car6.num)
? ? ? ? ? ? {
? ? ? ? ? ? weight = weight + car6.cargo;
? ? ? ? ? ? System.out.print(car6.name);
? ? ? ? ? ? money = money + car6.rent * day;
? ? ? ? ? ? }
? ? ? ? ? ? System.out.print(" ");
? ? ? ? ? ? ?
? ? ? ? ? ? ?}
? ? ? ? ? ? ?System.out.println("共載貨:"+weight);
? ? ? ? ? ? ?
? ? ? ? ? ? System.out.println("***租車總價(jià)值:"+ money + "元");?
? ? ? ? ?}
? ? ? ? System.out.println("歡迎您的下次光臨");
? ? ? ? ?
? ? }
}
2018-08-05
import java.util.Scanner;
class Car
{
?private String name;//名稱
?private int rent;//金額
?private int manned;//載人
?private int cargo;//載客
?public void setName(String name)
?{?this.name= name;}
?public String getName()
?{?return name;}
?public void setRent(int rent)
?{?this. rent= rent;}
?public int getRent()
?{?return rent;}
?public void setManned(int manned)
?{?this. manned= manned;}
?public int getManned()
?{?return manned;}
?public void setCargo(int cargo)
?{?this. cargo= cargo;}
?public int getCargo()
?{?return cargo;}
? void show(){}
}
class PassengerCar extends Car//載人車
{
?PassengerCar(String name,int rent,int manned)
?{
??this.setName(name);
??this.setRent(rent);
??this.setManned(manned);
?}
?public void show()//載人車的輸出信息
?{
??System.out.println(getName()+"\t\t"+getRent()+"元/天\t載人"+getManned()+"人");
?}
}
class Pickup extends Car//載人載貨車
{
?Pickup(String name,int rent,int manned,int cargo)
?{
??this.setName(name);
??this.setRent(rent);
??this.setManned(manned);
??this.setCargo(cargo);
?}
?public void show()//載人載貨車的輸出信息
?{
??System.out.println(getName()+"\t\t"+getRent()+"元/天\t載人"+getManned()+"人 載貨:"+getCargo()+"噸");
?}
}
class Truck extends Car//載貨車
{
?Truck(String name,int rent,int cargo)
?{
??this.setName(name);
??this.setRent(rent);
??this.setCargo(cargo);
?}
?public void show()//載貨車的輸出信息
?{
??System.out.println(getName()+"\t\t"+getRent()+"元/天\t載貨:"+getCargo()+"噸");
?}
}
class RentCarsTest3
{
?public static void main(String[] args)
?{
??Car[] car={new PassengerCar("奧迪4",500,4),new PassengerCar("馬自達(dá)6",400,4),
?????new Pickup("皮卡雪6",450,4,2),new PassengerCar("金龍",800,20),
?????new Truck("松花江",400,4),new Truck("依維河",1000,20)};//創(chuàng)建對象數(shù)組,并初始化
??
??entering(car);
??carRental(car);
??
??
?}
?public static void vehicle(Car[] car)//打印車輛信息
?{
??System.out.println("\n序號\t汽車名稱\t租金\t\t容量");
??for (int x=0;x<car.length ;x++ )
??{?
???System.out.print(x+1+".\t");
???car[x].show();
??}
?}
?public static void entering(Car[] car)//判斷是否租車
?{
??int x=0;
??System.out.println("歡迎使用噠噠租車系統(tǒng):\n您是否要租車:1、是 0否");
??do
??{??
???Scanner p=new Scanner(System.in);
???int entering=p.nextInt();
???if (entering==1)
???{
????vehicle(car);
????break;
???}
???else if (entering!=0)
???{
????System.out.println("您的輸入有誤!請重新輸入:\n您是否要租車:1、是 0否");
????x=1;
???}
???else
???{?System.out.println("謝謝您的使用");
????System.exit(0);
???}
??}while (x==1);
?}
?public static void carRental(Car[] car)//租車情況
?{
??int rentnum=0,mannednum=0,cargonum=0;//總價(jià)格,載人數(shù),載客數(shù)
??Scanner b=new Scanner(System.in);
??System.out.println("請輸入要租汽車的數(shù)量:");
??int number=b.nextInt();//車輛數(shù)量
??int[] sequence=new int[number];//車輛序號數(shù)組
??for (int x=0;x<number ;x++ )
??{
???int y=x+1;
???System.out.println("請輸入要租的第"+y+"輛車的序號");
???Scanner c=new Scanner(System.in);
???sequence[x]=c.nextInt();//存儲用戶輸入的序號
??}
??System.out.println("請輸入要租汽車的天數(shù):");
??int day=b.nextInt();//天數(shù)
???
???//打印賬單
??System.out.println("您的賬單:\n***可載人的車有:");
??for (int x=0;x<number;x++ )
??{
???if (car[sequence[x]-1] instanceof PassengerCar||car[sequence[x]-1] instanceof Pickup)//判斷?car[sequence[x]-1] 是不是PassengerCar類型,或者是不是Pickup類型
???{
????System.out.print(car[sequence[x]-1].getName()+"\t");
????mannednum+=car[sequence[x]-1].getManned();
???}
??}
??System.out.println("總載人:"+mannednum+"人\n***可載貨的車有:");
??for (int x=0;x<number;x++ )
??{
???if (car[sequence[x]-1] instanceof Truck||car[sequence[x]-1] instanceof Pickup)??
???{
????System.out.print(car[sequence[x]-1].getName()+"\t");
????cargonum+=car[sequence[x]-1].getCargo();
???}
???rentnum+=car[sequence[x]-1].getRent();
??}
??rentnum=rentnum*day;
??System.out.println("總載貨:"+cargonum+"噸\n***租車總價(jià)格:"+rentnum);??
?}
}
2018-07-22
萌新不在是萌新了
2018-07-18
學(xué)的比我好多了。
2018-07-18
思路很清晰。