答答租車程序
<Car類>
package com.imooc;
public class Car {
?? ?//車輛編號
?? ?private int number;
?? ?//車輛名稱
?? ?private String name;
?? ?//車輛租金
?? ?private float price;
?? ?
?? ?public int getNumber() {
?? ??? ?return number;
?? ?}
?? ?public void setNumber(int number) {
?? ??? ?this.number = number;
?? ?}
?? ?public String getName() {
?? ??? ?return name;
?? ?}
?? ?public void setName(String name) {
?? ??? ?this.name = name;
?? ?}
?? ?public float getPrice() {
?? ??? ?return price;
?? ?}
?? ?public void setPrice(float price) {
?? ??? ?this.price = price;
?? ?}
?? ?@Override
?? ?public String toString() {
?? ??? ?return "序號:" + number + '\t'+ "名稱:" + name + '\t' + "價(jià)格:" + price + '\t';
?? ?}
}
<PassengerCar類>
package com.imooc;
public class PassengerCar extends Car{
?? ?//載客量
?? ?private int passenger;
?? ?public int getPassenger() {
?? ??? ?return passenger;
?? ?}
?? ?public void setPassenger(int passenger) {
?? ??? ?this.passenger = passenger;
?? ?}
?? ?
?? ?public PassengerCar(int newNumber,String newName,float newPrice,int passenger) {
?? ??? ?super.setName(newName);
?? ??? ?super.setNumber(newNumber);
?? ??? ?super.setPrice(newPrice);
?? ??? ?this.setPassenger(passenger);
?? ?}
?? ?@Override
?? ?public String toString() {
?? ??? ?return super.toString()+"載客量:" + passenger;
?? ?}
}
<TrunkCar類>
package com.imooc;
public class TruckCar extends Car{
?? ?//載貨量
?? ?private double capacity;
?? ?public double getCapacity() {
?? ??? ?return capacity;
?? ?}
?? ?public void setCapacity(double capacity) {
?? ??? ?this.capacity = capacity;
?? ?}
?? ?public TruckCar(int newNumber,String newName,float newPrice,double capacity) {
?? ??? ?super.setName(newName);
?? ??? ?super.setNumber(newNumber);
?? ??? ?super.setPrice(newPrice);
?? ??? ?this.setCapacity(capacity);
?? ?}
?? ?@Override
?? ?public String toString() {
?? ??? ?return super.toString()+"載貨量:" + capacity;
?? ?}
}
<PickCar類>
package com.imooc;
public class PickCar extends Car {
?? ?//載貨量
?? ?private double capacity;
?? ?public double getCapacity() {
?? ??? ?return capacity;
?? ?}
?? ?public void setCapacity(double capacity) {
?? ??? ?this.capacity = capacity;
?? ?}
?? ?//載客量
?? ?private int passenger;
?? ?public int getPassenger() {
?? ??? ?return passenger;
?? ?}
?? ?public void setPassenger(int passenger) {
?? ??? ?this.passenger = passenger;
?? ?}
?? ?
?? ?public PickCar(int newNumber,String newName,float newPrice,int passenger,double capacity) {
?? ??? ?super.setName(newName);
?? ??? ?super.setNumber(newNumber);
?? ??? ?super.setPrice(newPrice);
?? ??? ?this.setCapacity(capacity);
?? ??? ?this.setPassenger(passenger);
?? ?}
?? ?@Override
?? ?public String toString() {
?? ??? ?return super.toString()+"載貨量:" + capacity + '\t' + "載客量:" + passenger;
?? ?}
}
<Initial類>
package com.imooc;
import java.util.Scanner;
public class Initial {
?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?//創(chuàng)建汽車對象
?? ??? ?Car[] cars = new Car[5];
?? ??? ?cars[0] = new TruckCar(1,"重型卡車 ",460,4.5);
?? ??? ?cars[1] = new TruckCar(2,"輕型卡車 ",300,3.5);
?? ??? ?cars[2] = new PassengerCar(3,"大客車",500,45);
?? ??? ?cars[3] = new PassengerCar(4,"小客車",400,30);
?? ??? ?cars[4] = new PickCar(5,"皮卡",450,6,3.5);
?? ??? ?
?? ??? ?System.out.println("歡迎使用答答租車系統(tǒng)!");
?? ??? ?System.out.println("請問您是否需要租車?" + '\t' + "1.是" + '\t' + "2.否");
?? ??? ?Scanner s = new Scanner(System.in);
?? ??? ?int in = s.nextInt();
?? ??? ?//判斷是否租車
?? ??? ?if(in == 1) {
?? ??? ??? ?//輸出汽車列表
?? ??? ??? ?System.out.println("-------------------------------");
?? ??? ??? ?System.out.println("您可租車的類型及其價(jià)目表:");
?? ??? ??? ?for(Car c:cars) {
?? ??? ??? ??? ?System.out.println(c.toString());
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?//客戶選擇車輛數(shù)目
?? ??? ??? ?System.out.println("-------------------------------");
?? ??? ??? ?System.out.println("請選擇您租賃汽車的數(shù)量:");
?? ??? ??? ?int num = s.nextInt();
?? ??? ??? ?//客戶選擇車輛型號
?? ??? ??? ?Car[] rentCars = new Car[num];
?? ??? ??? ?for(int i = 1; i <= num ;i++) {
?? ??? ??? ??? ?System.out.println("請選擇您第" + i + "輛車型號(請輸入車輛序號):");
?? ??? ??? ??? ?int j = s.nextInt();
?? ??? ??? ??? ?rentCars[i-1] = cars[j-1];
?? ??? ??? ?}
?? ??? ??? ?//客戶選擇租賃時(shí)間
?? ??? ??? ?System.out.println("請輸出您租賃時(shí)間:");
?? ??? ??? ?int time = s.nextInt();
?? ??? ??? ?
?? ??? ??? ?//計(jì)算金額
?? ??? ??? ?double totalprice = 0;
?? ??? ??? ?for(int i = 0; i < num; i++) {
?? ??? ??? ??? ?totalprice += rentCars[i].getPrice()*time;
?? ??? ??? ?}
?? ??? ??? ?//計(jì)算總載貨量、總載客量
?? ??? ??? ?double totalcapacity = 0;
?? ??? ??? ?int totalpassenger = 0;
?? ??? ??? ?for(int i = 0; i < num; i++) {
?? ??? ??? ??? ?if(rentCars[i] instanceof PassengerCar) {
?? ??? ??? ??? ??? ?totalpassenger += ((PassengerCar)rentCars[i]).getPassenger();
?? ??? ??? ??? ?}
?? ??? ??? ??? ?if(rentCars[i] instanceof TruckCar) {
?? ??? ??? ??? ??? ?totalcapacity += ((TruckCar)rentCars[i]).getCapacity();
?? ??? ??? ??? ?}
?? ??? ??? ??? ?if(rentCars[i] instanceof PickCar) {
?? ??? ??? ??? ??? ?totalpassenger += ((PickCar)rentCars[i]).getPassenger();
?? ??? ??? ??? ??? ?totalcapacity += ((PickCar)rentCars[i]).getCapacity();
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?//輸出租賃信息
?? ??? ??? ?System.out.println("-------------------------------");
?? ??? ??? ?System.out.println("租賃車輛列表如下:");
?? ??? ??? ?for(Car c1:rentCars) {
?? ??? ??? ??? ?System.out.println(c1.toString());
?? ??? ??? ?}
?? ??? ??? ?System.out.println("總載客量為:" + totalpassenger);
?? ??? ??? ?System.out.println("總載貨量為:" + totalcapacity);
?? ??? ??? ?System.out.println("租賃時(shí)間為:" + time + "天");
?? ??? ??? ?System.out.println("您共需支付:" + totalprice);
?? ??? ?}
?? ?}
}
2019-05-05
看看我的
//leixing類
package zuche;
import java.util.Scanner;
public class leixing {
?public void enmu() {
??System.out.println("您可租車的類型及價(jià)目表:");
??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元/天???????????? 載人:4人??? ");
?}
?
?public void cust() {
??System.out.println("請輸入你要租車的數(shù)量:");
??Scanner s = new Scanner(System.in);
??int in = s.nextInt();
??int[] a = new int[in];
??int sum = 0;
??int person = 0;
??int zl = 0;
??for(int i=1; i<=in; i++)
??{
???System.out.println("請輸入第"+i+"輛車的序號");
???int j = s.nextInt();
???switch(j) {
???case 1:
????sum += 500;
????person +=4;
????zl += 0;
????break;
???case 2:
????sum += 400;
????person +=4;
????zl += 0;
????break;
???case 3:
????sum += 450;
????person +=4;
????zl += 2;
????break;
???case 4:
????sum += 800;
????person +=20;
????zl += 0;
????break;
???case 5:
????sum += 400;
????person += 0;
????zl += 20;
????break;
???case 6:
????sum += 1000;
????person += 4;
????zl += 0;
????break;
???default:
????System.out.println("你的輸入有誤,請重新輸入");
????int z = s.nextInt();
???}
???//sum = sum*3;
???System.out.println();
??}
??System.out.println("請輸入租車的天數(shù):");
??int k = s.nextInt();
??System.out.println();
??System.out.println();
??System.out.println("您的賬單:");
??System.out.println("可載貨"+zl+"噸");
??System.out.println("可載"+person+"人");
??System.out.println("總金額為:\n"+sum*k);
?}
}
測試類testcar
package zuche;
import java.util.Scanner;
public class testche {
?public static void main(String[] args) {
??leixing te = new leixing();
??System.out.println("是否租車:\n"+"1.是????????????????????????????? 2.否");
??Scanner hh = new Scanner(System.in);
??int iii = hh.nextInt();
??if(iii==1) {
???te.enmu();
???te.cust();
??}
??else
??{
???System.out.println("退出系統(tǒng)");
???return ;
??}
?}
}
2019-02-12
練個(gè)手,老哥們看看有什么可以優(yōu)化的地方