Clase Card
package models;
public class Card {
private double value;
public Card(double value) {
this.value = value;
}
public void descontar(double value) {
this.value -= value;
}
public double getValue() {
return value;
}
}
Clase Product
package models;
public class Product {
String description;
double price;
public Product(String description, double precio) {
this.description = description;
this.price = precio;
}
public String getDescription() {
return description;
}
public double getPrice() {
return price;
}
}
Clase Sale
package models;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Sale {
private final List<Product> productList;
double total;
public Sale() {
productList = new ArrayList<>();
total = 0;
}
public void addProduct (Product product) {
productList.add(product);
total += product.getPrice();
}
public double getTotal() {
return this.total;
}
public void showShopping() {
System.out.println("""
**************************
COMPRAS REALIZADAS
"""
);
productList.sort(Comparator.comparing(Product::getPrice));
productList.forEach(product -> System.out.printf("""
%s - %.1f
""",
product.getDescription(),
product.getPrice())
);
System.out.println("""
**************************
"""
);
}
}
Clase Main
import models.Sale;
import models.Product;
import models.Card;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
showHeader();
Scanner scanner = new Scanner(System.in);
Sale sale = new Sale();
double cardLimit = getCardLimit(scanner);
Card card = new Card(cardLimit);
double purchaseValue;
String purchaseDescription;
String continuar = "1";
do {
try {
System.out.print("Escriba la descripción de la compra: ");
purchaseDescription = scanner.next();
System.out.print("Escriba el valor de la compra: ");
purchaseValue = scanner.nextDouble();
if (!(card.getValue() > sale.getTotal() + purchaseValue)) {
System.out.println("Saldo insuficiente!");
break;
}
Product newProduct = new Product(purchaseDescription, purchaseValue);
sale.addProduct(newProduct);
card.descontar(newProduct.getPrice());
System.out.println("Compra realizada!");
System.out.print("Escriba 1 para continuar, cualquier otra tecla para salir: ");
continuar = scanner.next();
} catch (InputMismatchException _) {
System.out.println("\n¡Ingrese un valor válido, intenta de nuevo!\n");
scanner.nextLine();
}
} while (continuar.equals("1"));
sale.showShopping();
System.out.printf("Saldo de la tarjeta: %.1f%n", card.getValue());
}
private static double getCardLimit(Scanner scanner) {
double cardLimit = 0;
do {
try {
System.out.print("Escriba el límite de la tarjeta: ");
cardLimit = scanner.nextDouble();
} catch (InputMismatchException _) {
System.out.println("\n¡Ingrese un valor válido, intenta de nuevo!\n");
scanner.nextLine();
}
} while (cardLimit == 0);
return cardLimit;
}
private static void showHeader() {
System.out.print("""
************************************
Welcome to shopping list app
************************************
"""
);
}
}
¡Gracias por leer!