Practice calculating different types of car rentals

The code is as follows:

Vehicle class:

Public class vehicle {/ / return the total rent public double getsum (int days) {return 0;}

}

Car class:

/** car: car is a subclass of vehicle, Attribute: model (hatchback, hatchback, off-road) * hatchback: 300 per day; hatchback: 350 per day; off-road: 500 per day * / public class car extends vehicle {string motortype; / / model @ override public double getsum (int days) {switch (motortype) {case "hatchback": return 300 * days; case "hatchback": return 350 * days; case "SUV": return 500 * days;} return 0; }

}

Bus class:

/** bus: multi seat car, attribute: number of seats * number of seats < = 16: 600 * number of seats per day > 16: 800 * / public class bus extends vehicle {int seatnum; @ override public double getsum (int days) {if (seatnum > 16) return 800 * days; return 600 * days;}

}

Test demo class:

import java. util. Scanner; /* * According to the user's choice of different cars, Calculate the total rent and output the total rent * / public class demo {public static void main (string [] args) {scanner SC = new scanner (system. In); system.out.println ("please select car type:"); system.out.println ("\ T 1 car \ T2 bus"); int vehicletype = sc.nextint(); if (vehicletype = = 1) {/ / car system.out.println ("continue selection:"); system.out.println is selected( "\ T1 hatchback \ T2 hatchback \ t3suv"); int carType=sc.nextInt(); // Create a car type object car mycar = new car()// Set the car type according to the user's choice switch (cartype) {case 1: mycar. Motortype = "hatchback"; break; case 2: mycar. Motortype = "hatchback"; break; case 3: mycar. Motortype = "SUV"; break; default: mycar. Motortype = "hatchback"; break;} System. out. Println ("please enter the number of days you want to rent:"); int days=sc.nextInt(); System. out. Println ("rent is:" + mycar. Getsumrent (days));} Else if (vehicletype = = 2) {/ / bus system.out.println ("please select the number of seats in a multi seat car:"); system.out.println ("\ T1 less than 16 seats \ T2 more than 16 seats"); int seatsnum = sc.nextint(); / / create a bus object bus MYBUS = new bus(); mybus.seatnum = seatsnum; system.out.println ("please enter the number of days you want to rent:"); int days = sc.nextint(); system.out.println ("rent is:" +myBus. getSumRent(days)); } } }

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>