From 78ef91b9f0593e5810d40e924319f2de1fb7275c Mon Sep 17 00:00:00 2001 From: Xingzhimeng <3408335915@qq.com> Date: Wed, 25 Mar 2026 21:30:11 +0800 Subject: [PATCH] w3 --- w3/BankAccount.java | 45 +++++++++++++++++++++++++++++++++++++++++++ w3/Car.java | 47 +++++++++++++++++++++++++++++++++++++++++++++ w3/TeskBank.java | 8 ++++++++ w3/TestCar.java | 6 ++++++ 4 files changed, 106 insertions(+) create mode 100644 w3/BankAccount.java create mode 100644 w3/Car.java create mode 100644 w3/TeskBank.java create mode 100644 w3/TestCar.java diff --git a/w3/BankAccount.java b/w3/BankAccount.java new file mode 100644 index 0000000..75c6d88 --- /dev/null +++ b/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("取款失败,余额不足或金额非法"); + } + } +} \ No newline at end of file diff --git a/w3/Car.java b/w3/Car.java new file mode 100644 index 0000000..ea20bad --- /dev/null +++ b/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; + } + } +} \ No newline at end of file diff --git a/w3/TeskBank.java b/w3/TeskBank.java new file mode 100644 index 0000000..0bca89d --- /dev/null +++ b/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); + } +} \ No newline at end of file diff --git a/w3/TestCar.java b/w3/TestCar.java new file mode 100644 index 0000000..59d18e6 --- /dev/null +++ b/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); + } +} \ No newline at end of file