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