Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize car Rental system by Java

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "Java how to achieve car rental system", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Java how to achieve car rental system" this article.

Car rental:

It is divided into two types: passenger cars and cars:

Less than 20 passenger cars: 500 a day, more than 20: 900 a day.

Cars are divided into luxury and ordinary: luxury 600 a day, ordinary 200 a day.

Effect picture:

The code is as follows:

Motor vehicles:

Package busTest;/* vehicle category * / public abstract class MotoVehicle {private String carNumber; / / license plate number private String carBrand; / / license plate / / Construction method public MotoVehicle () {} public MotoVehicle (String carNumber, String carBrand) {this.carNumber = carNumber; this.carBrand = carBrand;} / / get/set public String getCarNumber () {return carNumber } public void setCarNumber (String carNumber) {this.carNumber = carNumber;} public String getCarBrand () {return carBrand;} public void setCarBrand (String carBrand) {this.carNumber = carNumber;} / * method for calculating leases * / public abstract int calRent (int days);}

Passenger cars:

Package busTest;public class Bus extends MotoVehicle {private int setCount; / / number of seats / / initialize the object public Bus (String carNUmber, String brand, int setCount) {super (carNUmber, brand) by constructor; this.setCount = setCount } @ Override public int calRent (int days) {/ / determine the rental amount based on the number of seats if (this.setCount < 20) {return days * 500;} else {return days * 900;}} public void showBusInfo (int days) {System.out.println ("*") System.out.println ("\ t license plate number:" + super.getCarNumber ()); System.out.println ("\ t car brand:" + super.getCarBrand ()); System.out.println ("\ t seats:" + this.setCount); System.out.println ("\ t rental days:" + days); System.out.println ("\ t amount:" + calRent (days));}}

Car category:

Package busTest;public class Car extends MotoVehicle {private String type; / / car type normal / luxury / / initialize the object public Car (String carNUmber, String brand, String type) {super (carNUmber, brand) by constructor; this.type = type } @ Override public int calRent (int days) {/ / Price is determined by type if ("luxury car" .equals (type)) {return days * 600;} else {return days * 200;}} public void showCarInfo (int days) {System.out.println ("*") System.out.println ("\ t license plate number:" + super.getCarNumber ()); System.out.println ("\ t car brand:" + super.getCarBrand ()); System.out.println ("\ t car type:" + this.type); System.out.println ("\ t rental days:" + days); System.out.println ("\ t amount:" + calRent (days));}}

Car rental customers:

Package busTest;/* customer class * / import java.util.Scanner;public class Customer {private String name; private int sum = 0; / / when I'm not sure whether my shopping cart is a car or a bus, create an object array of MotoVehicle [] motos = new MotoVehicle [10]; Scanner input = new Scanner (System.in) with the parent type. Public void showMenu () {/ / defines a parent vehicle object, which can be received below as MotoVehicle moto = null; System.out.println ("* car rental system *"); String answer; do {System.out.println ("1, rental bus 2, rental car") System.out.print ("Please enter the number:"); int num = input.nextInt (); if (num = = 1) {/ / create the leased bus object moto = rentBus () } else if (num = = 2) {/ / create leased car object moto = rentCar ();} for (int I = 0; I < motos.length; itemized +) {if (motos [I] = = null) {motos [I] = moto; break }} System.out.print ("do you want to continue the lease? : YBO "); answer = input.next ();} while (!" n ".equals (answer)); System.out.print (" Please enter your name: "); this.name = input.next (); System.out.print (" days leased: "); int days = input.nextInt () / / calculate the rental amount according to the number of days calTotalRent (days); / / display the rental information showInfo (days);} private void showInfo (int days) {System.out.println ("- car rental information -") For (int I = 0; I < motos.length; iTunes +) {MotoVehicle moto = this.motos [I]; if (moto! = null) {if (moto instanceof Bus) {Bus bus = (Bus) moto; bus.showBusInfo (days) } else if (moto instanceof Car) {Car car = (Car) moto; car.showCarInfo (days);} System.out.println ("\ t customer:" + this.name + "\ t Total amount:" + sum) System.out.println ("- -");} private void calTotalRent (int days) {int total = 0 For (MotoVehicle moto: motos) {if (moto! = null) {int rent = moto.calRent (days); total + = rent; / / Total amount} this.sum = total / / copy the total amount to the global variable sum}} / / car private MotoVehicle rentCar () {System.out.print ("Please enter car brand:"); String brand = input.next (); System.out.print ("Please enter car license plate number:"); String carNumber = input.next () System.out.print ("Please choose car type [1, luxury car 2, regular car:]"); int choise = input.nextInt (); String type; if (choise = = 1) {type = "luxury car";} else {type = "ordinary";} return new Car (carNumber, brand, type) } / / bus private MotoVehicle rentBus () {System.out.print ("Please enter bus brand:"); String brand = input.next (); System.out.print ("Please enter bus license plate number:"); String carNumber = input.next (); System.out.print ("Please enter the number of bus seats:"); int seatCount = input.nextInt () Return new Bus (carNumber, brand, seatCount);}}

Test class:

Package busTest;public class TestMain {public static void main (String [] args) {Customer customer = new Customer (); customer.showMenu ();}} these are all the contents of the article "how to implement car Rental system in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report