Xingzhimeng 3 weeks ago
parent
commit
78ef91b9f0
  1. 45
      w3/BankAccount.java
  2. 47
      w3/Car.java
  3. 8
      w3/TeskBank.java
  4. 6
      w3/TestCar.java

45
w3/BankAccount.java

@ -0,0 +1,45 @@
public class BankAccount {
private String accountNumber;
private String ownerName;
private double balance;
public BankAccount(String accountNumber, String ownerName) {
this.accountNumber = accountNumber;
this.ownerName = ownerName;
this.balance = 0.0;
}
public String getAccountNumber() {
return accountNumber;
}
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("存款成功!余额:" + balance);
} else {
System.out.println("存款金额必须大于0");
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("取款成功!余额:" + balance);
} else {
System.out.println("取款失败,余额不足或金额非法");
}
}
}

47
w3/Car.java

@ -0,0 +1,47 @@
public class Car {
private String brand;
private String color;
private double price;
public Car() {
this.brand = "未知";
this.color = "白色";
this.price = 100000;
}
public Car(String brand, String color, double price) {
this.brand = brand;
this.color = color;
if (price > 0) {
this.price = price;
} else {
this.price = 100000;
}
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
if (price > 0) {
this.price = price;
}
}
}

8
w3/TeskBank.java

@ -0,0 +1,8 @@
public class TestBank {
public static void main(String[] args) {
BankAccount acc = new BankAccount("62281234", "张三");
acc.deposit(1000);
acc.withdraw(300);
acc.withdraw(1000);
}
}

6
w3/TestCar.java

@ -0,0 +1,6 @@
public class TestCar {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car("比亚迪", "灰色", 150000);
}
}
Loading…
Cancel
Save