Create interface Car: two methods getName () returns the car name, getPrice () method returns the car price
Create class BMW to implementation interface Car returns the name and price of BMW
Create class Maruti car to implement the interface Car returns the name and price of Maruti.
Create CarShop class: private member variable money is used to store the total income.
The member method sellCar has no return value and has a parameter, and the parameter is the Car interface.
The member method completes the function:
1. Output model: car name , get car price;
2. Cumulative total revenue money value
Create the main class: create an automobile sales shop object. Create BMW objects, Maruti objects
Call the sellCar method to sell the car. Output the total revenue of the sales shop.
public interface Car {
String getName();
double getPrice();
}
public class BMW implements Car{
@Override
public String getName() {
return "BMW";
}
@Override
public double getPrice() {
return 100;
}
}
public class Murati implements Car{
@Override
public String getName() {
return "Murati";
}
@Override
public double getPrice() {
return 90;
}
}
public class CarShop {
private double totalIncome;
public void sellCar(Car car) {
System.out.println(car.getName() + " " + car.getPrice());
totalIncome += car.getPrice();
}
public double getTotalIncome() {
return totalIncome;
}
}
public class Main {
public static void main(String[] args) {
CarShop carShop = new CarShop();
BMW bmw = new BMW();
Murati murati = new Murati();
carShop.sellCar(bmw);
carShop.sellCar(murati);
System.out.println("Total income: " + carShop.getTotalIncome());
}
}
Comments
Leave a comment