通过java实现根据封装的知识,编写程序,题目要求: 某汽车销售公司,销售各种小车,有品牌,型号,座位数,颜色,单价,上个月销售量等属性,及计算月销售金额方法,月销售总金额。 要求:(1)创建 Car 类封装上述属性与方法。 (2)创建测试类,实例化多部小车,键盘输入数据如图数据,输出相应内容
时间: 2023-05-22 19:03:12 浏览: 56
下面是 Car 类和测试类的代码实现:
Car.java
```java
public class Car {
// 成员变量
private String brand;
private String model;
private int seatNum;
private String color;
private double unitPrice;
private int lastMonthSales;
// 构造方法
public Car(String brand, String model, int seatNum, String color, double unitPrice, int lastMonthSales) {
this.brand = brand;
this.model = model;
this.seatNum = seatNum;
this.color = color;
this.unitPrice = unitPrice;
this.lastMonthSales = lastMonthSales;
}
// 计算月销售金额方法
public double calculateMonthlySales() {
return this.unitPrice * this.lastMonthSales;
}
// 自动生成 getter 和 setter 方法
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getSeatNum() {
return seatNum;
}
public void setSeatNum(int seatNum) {
this.seatNum = seatNum;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
public int getLastMonthSales() {
return lastMonthSales;
}
public void setLastMonthSales(int lastMonthSales) {
this.lastMonthSales = lastMonthSales;
}
}
```
Test.java
```java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 实例化多部小车
Car car1 = new Car("Volkswagen", "Jetta", 5, "Gray", 120000, 100);
Car car2 = new Car("Ford", "Focus", 5, "White", 110000, 80);
Car car3 = new Car("Toyota", "Corolla", 5, "Black", 130000, 120);
// 键盘输入数据
System.out.print("请输入品牌:");
String brand = scanner.nextLine();
System.out.print("请输入型号:");
String model = scanner.nextLine();
System.out.print("请输入座位数:");
int seatNum = scanner.nextInt();
scanner.nextLine(); // 把后面的回车换行读掉
System.out.print("请输入颜色:");
String color = scanner.nextLine();
System.out.print("请输入单价:");
double unitPrice = scanner.nextDouble();
System.out.print("请输入上个月销售量:");
int lastMonthSales = scanner.nextInt();
// 输出相应内容
System.out.printf("%s %s,%d座位,%s颜色,单价%.2f元,上个月销售量%d台,月销售金额为%.2f元。\n",
brand, model, seatNum, color, unitPrice, lastMonthSales,
new Car(brand, model, seatNum, color, unitPrice, lastMonthSales).calculateMonthlySales());
// 输出已有的小车信息
System.out.printf("已有的小车信息:\n%s,月销售金额为%.2f元。\n",
car1.getModel(), car1.calculateMonthlySales());
System.out.printf("%s,月销售金额为%.2f元。\n",
car2.getModel(), car2.calculateMonthlySales());
System.out.printf("%s,月销售金额为%.2f元。\n",
car3.getModel(), car3.calculateMonthlySales());
}
}
```
键盘输入数据如下:
```
请输入品牌:BMW
请输入型号:X5
请输入座位数:7
请输入颜色:Red
请输入单价:200000
请输入上个月销售量:60
```
输出结果如下:
```
BMW X5,7座位,Red颜色,单价200000.00元,上个月销售量60台,月销售金额为12000000.00元。
已有的小车信息:
Jetta,月销售金额为12000000.00元。
Focus,月销售金额为8800000.00元。
Corolla,月销售金额为15600000.00元。
```