練習(xí)題:關(guān)于繼承內(nèi)含forEach循環(huán)。

一個銷售員(信息包括姓名年齡),銷售車輛(車輛包含品牌和價格,多輛銷售),打印銷售員銷售多輛汽車的詳細報表(包含提成),提成為百分之零點五。

銷售員Seller.java

  • 思路,銷售員肯定要定義一個姓名變量,一個年齡變量,一個提成變量。
  • 肯定要有提成計算方法和輸出打印方法。
  • 多輛銷售需要一個車輛數(shù)組。
package javaDemo2;

import java.util.Arrays;

public class Seller {
    String name;
    int age;
    public Car[] car;
    double salary;
    
    public double getSalary() {//獲取提成方法
        int totalPrice = 0;
        for(int i = 0; i < this.car.length; i++) {
            totalPrice+= this.car[i].price;
        }
        return totalPrice*0.05;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }  
    public Seller(String name, int age, Car[] car) {//構(gòu)造方法
        super();
        this.name = name;
        this.age = age;
        this.car = car;
    }
    public Car[] getCar() {
        return car;
    }
    public void setCar(Car[] car) {
        this.car = car;
    }
    @Override
    public String toString() {// 打印方法
        String carsDetail = "";
        for(int i = 0; i < this.car.length; i++) {
            carsDetail += "\n品牌:" + this.car[i].brand + ",單價:" + this.car[i].price;
        }
        return "Seller [name=" + name + ", age=" + age + ", 銷售了:" + carsDetail + "\n總提成:"+ this.getSalary() + "]";
    }
    
}


汽車Car.java

package javaDemo2;

public class Car {
    String brand;
    float price;
    public Car() {
        super();
    }
    public Car(String brand, float price) {
        super();
        this.brand = brand;
        this.price = price;
    }
    @Override
    public String toString() {
        return "Car [brand=" + brand + ", price=" + price + "]";
    }
    
}


銷售結(jié)果檢驗:Constructor.java

package javaDemo2;

import java.util.Arrays;

public class Constructor {

    public static void main(String[] args) {
        
        Car c1 = new Car("法拉利Ferrari", 2000000);
        Car c2 = new Car("蘭博基尼Lamborghini", 3000000);
        Car[] cars = {c1, c2};
        Seller sellerA = new Seller("張三", 18, cars);
        System.out.println(sellerA);
        
        // forEach循環(huán)
        System.out.println("forEach循環(huán)打?。?);
        for(Car a : cars) {
            System.out.println(a);
        }
    }

}

執(zhí)行結(jié)果

image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 宣紙,紙之絕品。機械無為,唯中國之巧匠能為之,驚艷世界。 薄如蟬翼白勝雪, 柔似綢緞韌仿帛。 宣紙一絕中國匠, 大...
    北園主閱讀 442評論 3 6
  • 昨天早上看了一則泰國廣告,我,30歲了,又何妨?小時候,父母就告訴我們:不要害怕,有他們在,長大之后:卻開...
    顏健美閱讀 204評論 0 0
  • Chapter 3 Tuesday 20:00 距離詭宅50米的小甜品店里 A大靈異協(xié)會的偽·會員們在椴松的帶領(lǐng)下...
    二栞閱讀 723評論 0 1

友情鏈接更多精彩內(nèi)容