一个销售员(信息包括姓名年龄),销售车辆(车辆包含品牌和价格,多辆销售),打印销售员销售多辆汽车的详细报表(包含提成),提成为百分之零点五。
销售员Seller.java
- 思路,销售员肯定要定义一个姓名变量,一个年龄变量,一个提成变量。
- 肯定要有提成计算方法和输出打印方法。
- 多辆销售需要一个车辆数组。
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) {//构造方法
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 + "]";
}
}
销售结果检验: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循环
System.out.println("forEach循环打印:");
for(Car a : cars) {
System.out.println(a);
}
}
}
执行结果
image.png
网友评论