qq_慕工程0061192
2019-04-04 12:26
/*
* 父类
* */
package DDlease;
public abstract class Car {
public int sum;//总金额
public abstract void money(int number,int days);//计算总金额
}
/*
* 载人客车子类
* */
package DDlease;
public class CarToPeople extends Car {
String CarName[][] = new String[][]
{{"1.奥迪A4限载4人 ","500元/天"},{"2.马自达限载4人 ","400/天"},{"3.金龙限载20人 ","800元/天"}};//客车数组
public void print() {
for(int i = 0; i<CarName.length; i++) {
for(int j = 0;j<2;j++) {
System.out.print(CarName[i][j]);
}
System.out.println();
}
}
public void money(int number,int days) {//计算总金额
if(number == 1) {
sum = days*500;
System.out.println("总金额为:"+sum+"元");
}
if(number == 2) {
sum = days*400;
System.out.println("总金额为:"+sum+"元");
}
if(number == 3) {
sum = days*800;
System.out.println("总金额为:"+sum+"元");
}
}
}
/*
* 载货车子类
* */
package DDlease;
public class CarToThing extends Car {
String CarName[][] = new String[][]
{{"4.松花江限载4T ","400元/天"},{"5.依维柯限载4T ","1000元/天"}};//货车数组
public void print() {
for(int i = 0; i<CarName.length; i++) {
for(int j = 0;j<2;j++) {
System.out.print(CarName[i][j]);
}
System.out.println();
}
}
@Override
public void money(int number,int days ) {
if(number == 4) {
sum = days*400;
System.out.println("总金额为:"+sum+"元");
}
if(number == 5) {
sum = days*1000;
System.out.println("总金额为:"+sum+"元");
}
}
}
/*
* 皮卡子类
* */
package DDlease;
public class CarToBoth extends Car {
String CarName[] = new String[]
{"6.皮卡雪限载4人2T","450元/天"};//皮卡载人4载货2T
public void print() {
System.out.println(CarName[0]+" "+CarName[1]);
System.out.println();
}
@Override
public void money(int number,int days) {
// TODO Auto-generated method stub
if(number == 6) {
sum = days*450;
System.out.println("总金额为:"+sum+"元");
}
}
}
/*
* 测试类
* */
package DDlease;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("欢迎进入DD租车,是否租车:1租车2退出");
int a = input.nextInt();
if(a == 1) {
/*
* 打印基本信息
* */
System.out.println("这是我们公司的全部车型及价格和数据:");
CarToPeople car1 = new CarToPeople();//载客车
CarToThing car2 = new CarToThing();//载货车
CarToBoth car3 = new CarToBoth();//皮卡
car1.print();car2.print();car3.print();
/*
* 开始租车
* */
System.out.println("请输入想租车的序号和天数:");
int number= input.nextInt();
int days = input.nextInt();
if(number==1||number==2||number==3) {
car1.money(number,days);
}
if(number==4||number==5) {
car2.money(number,days);
}
if(number==6) {
car3.money(number,days);
}
System.out.println("租车完成,祝您生活愉快!");
}else {
input.close();
System.out.println("欢迎下次光临!再见!");
}
}
}
运行没有问题~
for(int j = 0;j<2;j++) 这行代码是什么意思?
if(number==1||number==2||number==3) 判断条件可以写成 number <=3
我没仔细看你的代码,初学者你能实现这个功能就很棒啦!
比你的更傻 所有代码在main函数里就实现了
比你的更傻子 子父类都没用到 哇
代码也长的跟个鸹貔一样···
main函数中间的判断跟个傻子一样···(太羞耻了)
Java入门第二季 升级版
530655 学习 · 6091 问题
相似问题