6 changed files with 155 additions and 0 deletions
@ -0,0 +1,43 @@ |
|||
public class BankAccount { |
|||
private final 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) { |
|||
System.out.println("取款失败!金额必须大于0"); |
|||
} else if (amount > balance) { |
|||
System.out.println("取款失败!余额不足"); |
|||
} else { |
|||
balance -= amount; |
|||
System.out.println("取款成功!当前余额:" + balance); |
|||
} |
|||
} |
|||
public void showAccountInfo() { |
|||
System.out.println("账户号:" + accountNumber + ",户主:" + ownerName + ",余额:" + balance); |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
public class BankAccountTest { |
|||
public static void main(String[] args) { |
|||
BankAccount account = new BankAccount("622848123456789", "张三"); |
|||
account.showAccountInfo(); |
|||
account.deposit(1000); |
|||
account.withdraw(300); |
|||
account.setOwnerName("李四"); |
|||
account.showAccountInfo(); |
|||
} |
|||
} |
|||
@ -0,0 +1,77 @@ |
|||
package com.rental; |
|||
public class Car { |
|||
private String licensePlate; |
|||
private String brand; |
|||
private String model; |
|||
private double dailyRent; |
|||
private boolean isRented; |
|||
private static int totalCars = 0; |
|||
public Car(String licensePlate, String brand, String model, double dailyRent) { |
|||
this.licensePlate = licensePlate; |
|||
this.brand = brand; |
|||
this.model = model; |
|||
this.dailyRent = dailyRent; |
|||
this.isRented = false; |
|||
totalCars++; |
|||
} |
|||
public Car(String licensePlate, String brand, String model) { |
|||
this(licensePlate, brand, model, 300.0); |
|||
} |
|||
public String getLicensePlate() { |
|||
return licensePlate; |
|||
} |
|||
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 double getDailyRent() { |
|||
return dailyRent; |
|||
} |
|||
public void setDailyRent(double dailyRent) { |
|||
if (dailyRent > 0) { |
|||
this.dailyRent = dailyRent; |
|||
} else { |
|||
System.out.println("Daily rent must be greater than 0, modification failed"); |
|||
} |
|||
} |
|||
public boolean isRented() { |
|||
return isRented; |
|||
} |
|||
public void rentCar() { |
|||
if (isRented) { |
|||
System.out.println("The car is already rented and cannot be rented again"); |
|||
} else { |
|||
isRented = true; |
|||
System.out.println("Car rented successfully"); |
|||
} |
|||
} |
|||
public void returnCar() { |
|||
if (!isRented) { |
|||
System.out.println("The car is not rented, no need to return"); |
|||
} else { |
|||
isRented = false; |
|||
System.out.println("Car returned successfully"); |
|||
} |
|||
} |
|||
public double calculateRent(int days) { |
|||
return dailyRent * days; |
|||
} |
|||
public static int getTotalCars() { |
|||
return totalCars; |
|||
} |
|||
public void displayInfo() { |
|||
System.out.println("License Plate: " + licensePlate |
|||
+ ", Brand: " + brand |
|||
+ ", Model: " + model |
|||
+ ", Daily Rent: " + dailyRent |
|||
+ ", Status: " + (isRented ? "Rented" : "Available")); |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
package com.rental; |
|||
public class TestCar { |
|||
public static void main(String[] args) { |
|||
Car car1 = new Car("XiangA12345", "BYD", "Han EV", 280.0); |
|||
Car car2 = new Car("XiangB67890", "Tesla", "Model 3"); |
|||
Car car3 = new Car("XiangC00001", "Toyota", "Camry", 350.0); |
|||
System.out.println("=== Initial Car Information ==="); |
|||
car1.displayInfo(); |
|||
car2.displayInfo(); |
|||
car3.displayInfo(); |
|||
System.out.println("\n=== Test Rent/Return ==="); |
|||
car1.rentCar(); |
|||
car1.rentCar(); |
|||
car1.returnCar(); |
|||
car1.returnCar(); |
|||
System.out.println("\n=== Calculate Rent ==="); |
|||
double total = car2.calculateRent(5); |
|||
System.out.println("car2 5-day rent: " + total); |
|||
System.out.println("\n=== Test Invalid Rent ==="); |
|||
car3.setDailyRent(-100); |
|||
System.out.println("car3 rent: " + car3.getDailyRent()); |
|||
System.out.println("\n=== Total Cars ==="); |
|||
System.out.println("Total: " + Car.getTotalCars()); |
|||
} |
|||
} |
|||
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
Loading…
Reference in new issue