仿照着Java入门第二季 答答租车系统做了一个租车系统
感觉这个妹子敲厉害啊,第一次写就写的这么好。
自己学了这么久的java一点长进都木有
嘤嘤嘤,人与人之间的差距怎么这么大。
没用到接口和泛型,因为实在不会.......
所以定义了抽象父类Car
package com.carrent;
/***********************************************************************
* Module: Car.java
* Author: jmh
* Purpose: Defines the Class Car
***********************************************************************/
import java.util.*;
/** @pdOid 0297049e-bed2-4b40-ab64-3d6cde5eff95 */
public abstract class Car {
private String name;
private int price;
public Car(String name,int price){
this.name=name;
this.price=price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return this.getName()+"\t" +this.getPrice() ; }
}
然后三个子类继承父类,再扩充方法和变量
//客车
package com.carrent;
import java.util.*;
public class Bus extends Car {
private int people;
public Bus(String name, int price,int people) {
super(name, price);
this.people=people;
// TODO Auto-generated constructor stub
}
public int getPeople() {
return people;
}
public void setPeople(int people) {
this.people = people;
}
@Override
public String toString() {
return super.toString() + "\t载人:" + this.getPeople()+ "人"; }
}
//货车
package com.carrent;
import java.util.*;
/** @pdOid f7031a2c-e185-4148-8115-786c306def38 */
public class Trunk extends Car {
private int goods;
public Trunk(String name, int price,int goods) {
super(name, price);
this.goods=goods;
// TODO Auto-generated constructor stub
}
public int getGoods() {
return goods;
}
public void setGoods(int goods) {
this.goods = goods;
}
@Override
public String toString() {
return super.toString() + "\t载货:" + this.getGoods() + "吨"; }
}
//皮卡车
package com.carrent;
import java.util.*;
public class Pickup extends Car {
private int people;
private int goods;
public Pickup(String name, int price,int people,int goods) {
super(name, price);
this.people=people;
this.goods=goods;
}
public int getPeople() {
return people;
}
public void setPeople(int people) {
this.people = people;
}
public int getGoods() {
return goods;
}
public void setGoods(int goods) {
this.goods = goods;
}
@Override
public String toString() {
return super.toString() + "\t载人:" + this.getPeople()+ "人"+ "\t载货:" + this.getGoods()+ "吨"; }
}
异常处理学的不大好,其实我希望是输入错误以后能够重新输入而不是直接停止运行的,但是不知道该怎么做......
主程序入口:
package com.carrent;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class main {
static int carnums=6;
public static void main(String args[]){
System.out.print("是否租车:");
System.out.println("【1】是【0】否");
Scanner input = new Scanner(System.in);
int n=3;
try{
n=input.nextInt();
}catch(Exception e){
System.out.println("输入错误!请重新输入。");
}
if(n==1){
//输出价目表1
System.out.println("你可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金\t容量");
Car[] car=initcar();
System.out.println("-----------------------");
int[][] num=new int[2][car.length];
int price=0;
for( int i=0;i<car.length;i++){
System.out.println("序号"+(i+1) +" "+ car[i].toString());
System.out.println("请输入要租第"+(i+1)+"种车的数量:(不租车的话请输入0)");
Scanner carnum = new Scanner(System.in);
try{
num[0][i]=carnum.nextInt();
}catch(Exception e){
System.out.println("输入错误!退出系统...");
}
if(num[0][i]==0) continue;
System.out.println("请输入要租车的天数:");
Scanner day = new Scanner(System.in);
try{
num[1][i]=day.nextInt();
}catch(Exception e){
System.out.println("输入错误!退出系统...");}
price=price+num[0][i]*num[1][i]*car[i].getPrice();
}
//打印订单
printOrder(car,num);
}
else if(n==0){
System.out.print("感谢您的使用!");
}
}
public static Car[] initcar(){
Car[] car =new Car[carnums];
car[0]=new Bus("奥迪A4", 500, 4);
car[1]=new Bus("马自达6", 400, 4);
car[2]=new Pickup("皮卡雪6", 450, 4, 2);
car[3]=new Bus("金龙", 800, 20);
car[4]=new Trunk("松花江", 400, 4);
car[5]=new Trunk("依维柯", 1000, 20);
//将数组中的值转换成string类型再打印输出
for (int i1 = 0; i1 < car.length; i1++) {
System.out.println((i1+1) + "\t" + car[i1].toString());
}
return car;
}
public static void printOrder(Car[] carList, int[][] carNumber)
{
int rentSum = 0,rent=0;
System.out.println();
System.out.println("--------------------------------------");
System.out.println("您的订单:");
System.out.println("汽车名称\t数量\t天数\t每日租金\t总租金");
for(int i = 0; i < carList.length; i++)
{
for(int j = 0; j < carNumber[1].length; j++)
{
rent=carList[i].getPrice()*carNumber[1][i]*carNumber[0][i];
}
rentSum+=rent;
System.out.println(carList[i].getName() + "\t" + carNumber[0][i] + "\t" + carNumber[1][i] + "\t" + carList[i].getPrice() + "\t" +rent );
}
System.out.println("------------------------------------");
System.out.println("\t\t\t总金额(¥):" +rentSum);
}
}
最后运行结果
点击查看更多内容
8人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦