**// Classe **
package exercicios.lista_exer;
public class CreditCard_Purchase implements Comparable<CreditCard_Purchase> {
private String description;
private double price;
public String getDescription() {
return description;
}
public double getPrice() {
return price;
}
public CreditCard_Purchase(String description, double price) {
this.description = description;
this.price = price;
}
@Override
public String toString() {
return description + " - " + String.format("%.2f", price);
}
@Override
public int compareTo(CreditCard_Purchase o) {
return Double.compare(this.price, o.getPrice());
}
}
**// Main **
package exercicios.lista_exer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class CreditCard_Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("How much do you have? : ");
double limit = input.nextDouble();
System.out.println(limit);
ArrayList<CreditCard_Purchase> purchases = new ArrayList<>();
int choice = -1;
while ( choice != 0 ) {
System.out.println("Main Menu");
System.out.println("1 - Buy a product");
System.out.println("0 - Exit");
System.out.println("Menu Choice: ");
choice = input.nextInt();
input.nextLine();
if (choice == 1) {
System.out.println("What is the product name?: ");
String product = input.nextLine();
System.out.println("how much is the price?: ");
double price = input.nextDouble();
if (price > limit) {
System.out.println("Sorry! You dont have money for buy this product!");
} else {
CreditCard_Purchase purchase = new CreditCard_Purchase(product, price);
purchases.add(purchase);
limit -= price;
System.out.println("Available Limit is: $" + limit);
}
} else if (choice == 0) {
break;
} else {
System.out.println("Sorry! Invalid menu option !!!");
System.out.println();
}
}
Collections.sort(purchases);
purchases.forEach(System.out::println);
System.out.println("Available Limit is: $" + String.format("%.2f", limit));
}
}