//Application类
package com.imooc.java3;
import java.util.*;
public class Application {
public static Car[] cars ={new Track(“东风”,800.0f,30.0f),new Audi(“奥迪Q7”,400.0f,5)
,new Piup(“皮卡”,500.0f,2,10.0f)};
public static int len=cars.length;
private static boolean isNeeded(){
System.out.println("您是否要租车:1.是 0.否 ");
@SuppressWarnings("resource")
Scanner in =new Scanner(System.in);
int x= in.nextInt();
if(x==1) return true;
else return false;
}
public static void main(String[] args) {
System.out.println("欢迎使用答答租车:");
boolean flag =isNeeded();
if(flag){
showCar();
chooseCar();
}
else{
System.exit(0);
}
}
private static void showCar() {
System.out.println("您可租车的类型及项目表:\n序号\t汽车名称 \t租金\t\t容量");
for(int i=0;i<len;i++){
if(cars[i].getName()=="皮卡"){
System.out.println(i+1+".\t"+cars[i].getName()+"\t"+cars[i].getPrice()+"元/天\t载人:"+cars[i].getCpeople()+"人 载重:"+cars[i].getCthing()+"吨");
}else if(cars[i].getName()=="东风"){
System.out.println(i+1+".\t"+cars[i].getName()+"\t"+cars[i].getPrice()+"元/天\t载重:"+cars[i].getCthing()+"吨");
}
else{
System.out.println(i+1+".\t"+cars[i].getName()+"\t"+cars[i].getPrice()+"元/天\t载人:"+cars[i].getCpeople()+"人");
}
}
}
private static void chooseCar(){
int count[] = {1,2,3};
@SuppressWarnings("unused")
int sum=0;;
for(int i=0;i<len;i++){
count[i]=0;;
}
System.out.println("请输入租车的数量:");
@SuppressWarnings("resource")
Scanner in1 =new Scanner(System.in);
int n = in1.nextInt();
while(n!=0){
System.out.println("请输入租车的序号:");
@SuppressWarnings("resource")
Scanner in2 =new Scanner(System.in);
int x= in2.nextInt();
if(x==1){
count[0]++;
}else if(x==2){
count[1]++;
}
else{
count[2]++;
}
n--;
}
System.out.println("请输入租车的天数:");
@SuppressWarnings("resource")
Scanner in2 =new Scanner(System.in);
int day = in2.nextInt();
System.out.println("车型\t数量\t租车天数\t单价\t租金");
for(int i=0;i<len;i++){
if(count[i]!=0){
System.out.println(cars[i].getName()+" \t"+count[i]+" \t"+day+" \t"+cars[i].getPrice()+"元\t"+calMoney(count[i],day)*cars[i].getPrice()+"元");
sum+=calMoney(count[i],day)*cars[i].getPrice();
}
else continue;
}
System.out.println("总金额为:"+sum+"元");
}
public static int calMoney(int count,int day){
return count*day;
}
}
//Car类
package com.imooc.java3;
public class Car{
private String name;
private float price;
private int cpeople;
private float cthing;
public int getCpeople() {
return cpeople;
}
public void setCpeople(int cpeople) {
this.cpeople = cpeople;
}
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;
}
public float getCthing() {
return cthing;
}
public void setCthing(float cthing) {
this.cthing = cthing;
}
}
//卡车类Track
package com.imooc.java3;
public class Track extends Car {
private String name;
private float price;
private float cthing;
public Track(String name, float price, float cthing) {
this.name=name;
this.price=price;
this.cthing=cthing;
}
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;
}
public float getCthing() {
return cthing;
}
public void setCthing(float cthing) {
this.cthing = cthing;
}
}
//奥迪类Audi
package com.imooc.java3;
public class Audi extends Car {
private String name;
private float price;
private int cpeople;
public Audi(String name, float price, int cpeople) {
this.name=name;
this.price=price;
this.cpeople=cpeople;
}
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;
}
public int getCpeople() {
return cpeople;
}
public void setCpeople(int cpeople) {
this.cpeople = cpeople;
}
}
//皮卡类Piup
package com.imooc.java3;
public class Piup extends Car {
private String name;
private float price;
private int cpeople;
private float cthing;
public Piup(String name, float price, int cpeople,float cthing){
this.name=name;
this.price=price;
this.cpeople=cpeople;
this.cthing=cthing;
}
public int getCpeople() {
return cpeople;
}
public void setCpeople(int cpeople) {
this.cpeople = cpeople;
}
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;
}
public float getCthing() {
return cthing;
}
public void setCthing(float cthing) {
this.cthing = cthing;
}
}