Java 编程练习:书包类和书籍类实现
// 书包类 Bag public class Bag { private String color; private int width; private int height; private Book[] books; private int bookCount;
public Bag(String color, int width, int height) {
this.color = color;
this.width = width;
this.height = height;
this.books = new Book[10];
this.bookCount = 0;
}
// 放入一本书
public void putBook(Book book) {
if (bookCount < 10) {
books[bookCount] = book;
bookCount++;
} else {
System.out.println('书包已满!');
}
}
// 获取当前书包中书的数量
public int getBookCount() {
return bookCount;
}
// 将书包中最后一本书取出
public Book takeOutLastBook() {
if (bookCount > 0) {
bookCount--;
Book book = books[bookCount];
books[bookCount] = null;
return book;
} else {
System.out.println('书包已空!');
return null;
}
}
// 判断书包是否为空
public boolean isEmpty() {
return bookCount == 0;
}
// 判断某一个书是否在书包中
public boolean hasBook(Book book) {
for (int i = 0; i < bookCount; i++) {
if (books[i].equals(book)) {
return true;
}
}
return false;
}
// 将另外一个书包中的全部书添加到本书包中
public void addBooksFromAnotherBag(Bag anotherBag) {
while (!anotherBag.isEmpty() && bookCount < 10) {
putBook(anotherBag.takeOutLastBook());
}
}
// 计算书包中书的总重量
public int getTotalWeight() {
int totalWeight = 0;
for (int i = 0; i < bookCount; i++) {
totalWeight += books[i].getWeight();
}
return totalWeight;
}
// 获取书包中所有书的颜色
public String[] getAllColors() {
String[] colors = new String[bookCount];
for (int i = 0; i < bookCount; i++) {
colors[i] = books[i].getColor();
}
return colors;
}
}
// 书籍类 Book public class Book { private String name; private String color; private double price; private int weight;
public Book(String name, String color, double price, int weight) {
this.name = name;
this.color = color;
this.price = price;
this.weight = weight;
}
public String getName() {
return name;
}
public String getColor() {
return color;
}
public double getPrice() {
return price;
}
public int getWeight() {
return weight;
}
// 获取书包的书名,颜色,价格和重量信息
public String getInfo() {
return '书名:' + name + ',颜色:' + color + ',价格:' + price + '元,重量:' + weight + '克';
}
// 重写equals方法,用于判断书籍是否相同
@Override
public boolean equals(Object obj) {
if (obj instanceof Book) {
Book book = (Book) obj;
return name.equals(book.getName()) && color.equals(book.getColor()) && price == book.getPrice() && weight == book.getWeight();
}
return false;
}
}
// 测试类1 import java.util.Random;
public class Test1 { public static void main(String[] args) { Random random = new Random(); Book[] books = new Book[10]; for (int i = 0; i < 10; i++) { String name = String.valueOf((char) (random.nextInt(26) + 65)); String color = new String[]{'红', '绿', '蓝'}[random.nextInt(3)]; double price = random.nextDouble() * 100 + 50; int weight = random.nextInt(101) + 100; books[i] = new Book(name, color, price, weight); } Bag bag = new Bag('黑色', 30, 40); for (int i = 0; i < 10; i++) { bag.putBook(books[i]); } System.out.println('书包中书的数量:' + bag.getBookCount()); System.out.println('书包中最后一本书:' + bag.takeOutLastBook().getInfo()); System.out.println('书包是否为空:' + bag.isEmpty()); Book book = new Book('A', '红', 50, 150); System.out.println('书包中是否有书:' + bag.hasBook(book)); Bag anotherBag = new Bag('白色', 25, 35); for (int i = 0; i < 5; i++) { anotherBag.putBook(books[i]); } System.out.println('另外一个书包中书的数量:' + anotherBag.getBookCount()); bag.addBooksFromAnotherBag(anotherBag); System.out.println('添加后书包中书的数量:' + bag.getBookCount()); System.out.println('书包中书的总重量:' + bag.getTotalWeight()); String[] colors = bag.getAllColors(); System.out.print('书包中所有书的颜色:'); for (int i = 0; i < colors.length; i++) { System.out.print(colors[i] + ' '); } } }
// 测试类2 import java.util.Random;
public class Test2 { public static void main(String[] args) { Random random = new Random(); Book[] books = new Book[15]; for (int i = 0; i < 15; i++) { String name = String.valueOf((char) (random.nextInt(26) + 65)); String color = new String[]{'红', '黄', '蓝'}[random.nextInt(3)]; double price = random.nextDouble() * 100 + 50; int weight = random.nextInt(101) + 100; books[i] = new Book(name, color, price, weight); } Bag bag1 = new Bag('红色', 35, 45); Bag bag2 = new Bag('黄色', 40, 50); Bag bag3 = new Bag('蓝色', 30, 40); for (int i = 0; i < 15; i++) { int num = random.nextInt(3); if (num == 0) { bag1.putBook(books[i]); } else if (num == 1) { bag2.putBook(books[i]); } else { bag3.putBook(books[i]); } } System.out.println('红色书包中书的数量:' + bag1.getBookCount() + ',重量:' + bag1.getTotalWeight()); System.out.println('黄色书包中书的数量:' + bag2.getBookCount() + ',重量:' + bag2.getTotalWeight()); System.out.println('蓝色书包中书的数量:' + bag3.getBookCount() + ',重量:' + bag3.getTotalWeight()); }
原文地址: https://www.cveoy.top/t/topic/nY3x 著作权归作者所有。请勿转载和采集!