Java图书馆通过多态实现计算罚金
时间: 2023-05-19 19:01:16 浏览: 303
Java图书馆可以通过多态实现计算罚金。在图书馆中,每本书都有一个归还日期,如果读者没有按时归还书籍,就需要支付罚金。通过多态,可以在父类中定义计算罚金的方法,然后在子类中实现具体的计算方式,这样可以方便地扩展不同类型的罚金计算方式。
相关问题
Java图书馆通过多态实现计算罚金代码
非常感谢您的提问。以下是一个简单的Java代码示例,用于计算罚金:
```
public class LibraryItem {
private String title;
private int daysLate;
public LibraryItem(String title, int daysLate) {
this.title = title;
this.daysLate = daysLate;
}
public double calculateFine() {
return daysLate * 0.25;
}
}
public class Book extends LibraryItem {
private String author;
public Book(String title, int daysLate, String author) {
super(title, daysLate);
this.author = author;
}
}
public class DVD extends LibraryItem {
private String director;
public DVD(String title, int daysLate, String director) {
super(title, daysLate);
this.director = director;
}
@Override
public double calculateFine() {
return daysLate * 0.5;
}
}
public class Main {
public static void main(String[] args) {
LibraryItem[] items = new LibraryItem[2];
items[0] = new Book("The Great Gatsby", 3, "F. Scott Fitzgerald");
items[1] = new DVD("The Godfather", 5, "Francis Ford Coppola");
for (LibraryItem item : items) {
System.out.println("Title: " + item.title);
System.out.println("Days late: " + item.daysLate);
System.out.println("Fine: $" + item.calculateFine());
System.out.println();
}
}
}
```
这个示例使用了多态,因为 `Book` 和 `DVD` 类都继承了 `LibraryItem` 类,并且都重写了 `calculateFine` 方法,以便根据物品类型计算罚款金额。在 `Main` 类中,我们创建了一个 `LibraryItem` 数组,并将其中的两个元素分别设置为 `Book` 和 `DVD` 对象。然后,我们遍历该数组,并对每个元素调用 `calculateFine` 方法,以计算罚款金额。
ava图书馆通过多态实现计算罚金代码,分为儿童书籍,成人书籍,音像制品,读者可一次借阅多本书籍和音像制品代码
非常感谢您的提问。以下是 Java 图书馆通过多态实现计算罚金的代码:
```
public abstract class Item {
protected String title;
protected int playingTime;
protected boolean gotIt;
protected int daysOverdue;
public Item(String title, int playingTime) {
this.title = title;
this.playingTime = playingTime;
this.gotIt = false;
this.daysOverdue = 0;
}
public void setGotIt(boolean gotIt) {
this.gotIt = gotIt;
}
public void setDaysOverdue(int daysOverdue) {
this.daysOverdue = daysOverdue;
}
public abstract double calculateFine();
public String toString() {
String result = title + " (" + playingTime + " mins)";
if (gotIt) {
result += "*";
}
return result;
}
}
public class Book extends Item {
private String author;
public Book(String title, String author, int playingTime) {
super(title, playingTime);
this.author = author;
}
public double calculateFine() {
double fine = 0.0;
if (daysOverdue > 0) {
fine = daysOverdue * 0.25;
}
return fine;
}
public String toString() {
return super.toString() + " by " + author;
}
}
public class Audio extends Item {
private String artist;
public Audio(String title, String artist, int playingTime) {
super(title, playingTime);
this.artist = artist;
}
public double calculateFine() {
double fine = 0.0;
if (daysOverdue > 0) {
fine = daysOverdue * 0.50;
}
return fine;
}
public String toString() {
return super.toString() + " by " + artist;
}
}
public class Library {
private ArrayList<Item> items;
public Library() {
items = new ArrayList<Item>();
}
public void addItem(Item item) {
items.add(item);
}
public void printCatalog() {
for (Item item : items) {
System.out.println(item);
}
}
public void calculateFine() {
for (Item item : items) {
double fine = item.calculateFine();
if (fine > 0.0) {
System.out.println(item + " fine: $" + fine);
}
}
}
}
```
读者可以一次借阅多本书籍和音像制品,可以在 Library 类中添加一个 ArrayList<Loan> loans,Loan 类中包含 Item 和借阅日期等信息。calculateFine() 方法中需要遍历 loans,计算每个 Loan 的罚金。
阅读全文