输入代码
父类:
package com.car;
public class Car {
public String name;//车名字
public int pice;//租一天的价格
public int capacity;//载几人
public void getList(){
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public int volume;//容量大小(载多重)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPice() {
return pice;
}
public void setPice(int pice) {
this.pice = pice;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
}
载人车:
package com.car;
public class Dolly extends Car{
public Dolly(String newName,int newPice,int newCapacity){
this.setName(newName);
this.setPice(newPice);
this.setCapacity(newCapacity);
}
public void getList(){
System.out.println(getName()+" "+getPice()+"/天"+" "+getCapacity()+"人");
}
}
载货车:
package com.car;
public class Truck extends Car{
public Truck(String newName,int newPice,int newVolume){
this.setName(newName);
this.setPice(newPice);
this.setVolume(newVolume);
}
public void getList(){
System.out.println(getName()+" "+getPice()+"/天"+" "+getVolume()+"吨");
}
}
皮卡车:
package com.car;
public class Pickup extends Car {
public Pickup(String newName,int newPice,int newCapacity,int newVolume){
this.setName(newName);
this.setPice(newPice);
this.setCapacity(newCapacity);
this.setVolume(newVolume);
}
public void getList(){
System.out.println(getName()+" "+getPice()+"/天"+" "+getCapacity()+"人 "+" "+getVolume()+"吨");
}
}
测试类:
package com.car;
import java.util.Scanner;
public class Master {
public static void main(String[] args) {
Car[] car = {new Dolly("奥迪A4",500,4),new Dolly("马自达6",400,4),
new Pickup("皮卡雪",450,4,2),new Dolly("金龙",800,20),
new Truck("松花江",800,4),new Truck("依维柯",1000,20)};
Scanner input = new Scanner(System.in);
System.out.println("欢迎使用dada租车系统");
System.out.println("您是否要租车:1 是,0 不是");
int choose = input.nextInt();
if (choose==1){
System.out.println("您可租用车的类型及其价目表:");
System.out.println("序号 "+"汽车名称"+" 租用价格(/天)"+" 容量");
for(int i=0;i<car.length;i++ ){
System.out.print(i+1+" ");
car[i].getList();
}
System.out.println("请输入您要租用车的数量:");
int number = input.nextInt();
int a=0,b=0,c=0,d=0,e=0,f=0;
for(int j=1;j<=number;j++){
System.out.println("请输入第"+j+"辆车的序号:");
int order = input.nextInt();
if(order==1){
a=a+1;
}
if(order==2){
b=b+1;
}
if(order==3){
c=c+1;
}
if(order==4){
d=d+1;
}
if(order==5){
e=e+1;
}
if(order==6){
f=f+1;
}
}
int totalCopacity=car[0].getCapacity()*a+car[1].getCapacity()*b+car[2].getCapacity()*c+car[3].getCapacity()*d;
int totalVolume=car[2].getVolume()*c+car[4].getVolume()*e+car[5].getVolume()*f;
int totalPice=car[0].getPice()*a+car[1].getPice()*b+car[2].getPice()*c+car[3].getPice()*d+car[4].getPice()*e+car[5].getPice()*f;
System.out.println("请输入租用天数:");
int days = input.nextInt();
System.out.println("您的账单:");
System.out.println("===可载人的车有:");
if(a!=0){
System.out.print(car[0].getName()+"\t");
}
if(b!=0){
System.out.print(car[1].getName()+"\t");
}
if(c!=0){
System.out.print(car[2].getName()+"\t");
}
if(d!=0){
System.out.print(car[3].getName()+"\t");
}
System.out.println("总载人数:"+totalCopacity);
System.out.println("可载货的车有:");
if(c!=0){
System.out.print(car[2].getName()+"\t");
}
if(e!=0){
System.out.print(car[4].getName()+"\t");
}
if(f!=0){
System.out.print(car[5].getName()+"\t");
}
System.out.println("总载货量为:"+totalVolume+"\t");
System.out.println("租车总价格:"+totalPice*days+"元");
}else{
System.out.println("感谢您的到访");
}
}
}
打开App,阅读手记
热门评论
你试下,五辆车,全选马自达6,账单里貌似只显示一辆马自达6,人数和钱没错。