DESCRIÇÃO DO TRABALHO JÁ PRONTO:
O trabalho consiste em implementar um pequeno sistema que emula um "Cofrinho de moedas" em Java. Crie um menu em que é oferecido ao usuário: Adicionar moedas de diferentes valores e países em seu cofrinho Remover moedas específicas do cofrinho. Listar todas as moedas que estão dentro do cofrinho Calcular quanto dinheiro existe no cofrinho convertido para Real
.
.
Trabalho já pronto e editável. Se alguém tiver interesse só avisar
import java.util.ArrayList;
import java.util.Scanner;
public class CoinTray {
private ArrayList coins;
private double totalValue;
public CoinTray() {
coins = new ArrayList<>();
totalValue = 0.0;
}
public void addCoin(Coin coin) {
coins.add(coin);
totalValue += coin.getValue();
}
public void removeCoin(Coin coin) {
if (coins.contains(coin)) {
coins.remove(coin);
totalValue -= coin.getValue();
} else {
System.out.println("Coin not found in tray.");
}
}
public void listCoins() {
for (Coin coin : coins) {
System.out.println(coin);
}
}
public double getTotalValue() {
return totalValue;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
CoinTray tray = new CoinTray();
while (true) {
System.out.println("1. Add coin");
System.out.println("2. Remove coin");
System.out.println("3. List coins");
System.out.println("4. Calculate total value");
System.out.println("5. Exit");
int choice = scanner.nextInt();
if (choice == 1) {
System.out.print("Enter coin value: ");
double value = scanner.nextDouble();
System.out.print("Enter coin country: ");
String country = scanner.next();
Coin coin = new Coin(value, country);
tray.addCoin(coin);
} else if (choice == 2) {
System.out.print("Enter coin value: ");
double value = scanner.nextDouble();
System.out.print("Enter coin country: ");
String country = scanner.next();
Coin coin = new Coin(value, country);
tray.removeCoin(coin);
} else if (choice == 3) {
tray.listCoins();
} else if (choice == 4) {
double totalValue = tray.getTotalValue();
System.out.println("Total value: " + totalValue);
} else if (choice == 5) {
break;
}
}
}
}
class Coin {
private double value;
private String country;
public Coin(double value, String country) {
this.value = value;
this.country = country;
}
public double getValue() {
return value;
}
public String getCountry() {
return country;
}
@Override
public String toString() {
return "Coin value: " + value + " | Coin country: " + country;
}
}
Para escrever sua resposta aqui, entre ou crie uma conta
Análise e Desenvolvimento de Sistemas
•UNINTER
Compartilhar