// Использую алгоритм бульбашки но видаєш ошибку в методе: public void sortQuantity
package com.company.prodacts_v3;
public class Product {
private String name;
private int quantity;
private double price;
public Product() {
this.quantity = 1;
this.price = 1;
}
public Product(String name, int quantity, double price) {
this.name = name;
this.quantity = quantity;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
private final String format = "%s. Quantity = %,d. One bottle = %,.3f$\n";
public double getPriceTypeProduct() {
double costOfAlcoholOrder = getQuantity() * getPrice();
return costOfAlcoholOrder;
}
public void showInfo() {
System.out.printf(format, getName(), getQuantity(), getPrice());
}
public void showPriceTypeProduct() {
System.out.printf("\t\tPrice = %,.3f$\n", getPriceTypeProduct());
}
}
package com.company.prodacts_v3;
public class Runner {
public static void main(String[] args) {
Product[] products = new Product[5];
products[0] = new Product("Beer", 12, 45.55);
products[1] = new Product("Vodka", 16, 5.23);
products[2] = new Product("Vine", 18, 8.12);
products[3] = new Product("Beer-1", 2, 9.85);
products[4] = new Product("Beer-2", 42, 125.55);
for(int i = 0; i < products.length; i++) {
products[i].showInfo();
products[i].showPriceTypeProduct();
}
sortQuantity(products);
// Ето пример алгоритма:
int[] arr = {2, -5, 7, -4, 8};
int tmp ;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] < arr[j]) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}// Ето пример алгоритма
public static void sortQuantity(Product[] products) {
int quantitys = 0;
for(int i = 0; i < products.length; i++) {
for(int j = i + 1; j < products.length; j++) {
if(products[i].getQuantity() < products[j].getQuantity()) {
quantitys = products[i].getQuantity();
products[i].getQuantity() = products[j].getQuantity(); // Error!
products[j].getQuantity() = quantitys; // Error!
}
}
}
for(int i = 0; i < products.length; i++) {
System.out.println(products[i].getQuantity());
}
}
}