Надо немного исправить эту программу, чтоб в конце общая сумма показывалась выбранных телефонов без процентов, просто общая сумма, программа после покупки одного пользователя, сразу переходила на другого, а не закрывалась, и этому пользователю показывала уже количество телефонов уменьшенное после покупки того человека (всего только 10 клиентов может быть)
package application;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
public class Application {
public static void main(String[] args) {
var app = new Application();
JOptionPane.showMessageDialog(null, "Welcome to PhoneVend", "Phones",
JOptionPane.DEFAULT_OPTION);
String message;
String numbers;
while ((message = app.getPropositions()) != null && (numbers = app.getNumber()) != null)
try {
var numbr = Integer.parseInt(message.trim()) - 1;
var count = Integer.parseInt(numbers.trim());
app.addOrder(numbr, count);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
app.showOrders();
System.exit(0);
}
Phone[] phones;
Map orders;
public Application() {
phones = new Phone[4];
phones[0] = new Phone("iPhone 11 Pro", 800, 10);
phones[1] = new Phone("Galaxy S6", 600, 10);
phones[2] = new Phone("iPhone 12 Pro Max", 950, 9);
phones[3] = new Phone("iPhone SE", 450, 10);
orders = new HashMap<>(phones.length);
}
public String getNumber() {
UIManager.put("OptionPane.cancelButtonText", "Cancel");
UIManager.put("OptionPane.okButtonText", "Further");
return JOptionPane.showInputDialog(null, "Enter number of phones", "Phones",
JOptionPane.DEFAULT_OPTION);
}
public String getPropositions() {
String message = "";
for (int i = 0; i < phones.length; i++) {
message += String.format("%d) %s (%d)%n%s%n%n", i + 1, phones[i].getTitle(), orders.getOrDefault(i, 0),
phones[i]);
}
UIManager.put("OptionPane.cancelButtonText", "Buy");
UIManager.put("OptionPane.okButtonText", "Further");
return JOptionPane.showInputDialog(null, message, "Phones", JOptionPane.DEFAULT_OPTION);
}
public void showOrders() {
String message = "";
double price = 0;
int count;
for (int i = 0; i < phones.length; i++) {
if ((count = orders.getOrDefault(i, 0)) > 0) {
message += String.format("%s (%d) %n %s %n %n", phones[i].getTitle(), count, phones[i]);
price += count * phones[i].getPrice() * (1 + phones[i].getNumber() / 100);
}
}
message += String.format("Price for phone: €%.2f", price);
UIManager.put("OptionPane.okButtonText", "Buy");
JOptionPane.showMessageDialog(null, message, "For the buyer", JOptionPane.DEFAULT_OPTION);
JOptionPane.showMessageDialog(null, message, "For the seller", JOptionPane.DEFAULT_OPTION);
}
public void addOrder(int number, int count) {
if (number < 0 || number >= phones.length) {
throw new RuntimeException("Can't find Phone");
}
if (count < 0) {
throw new RuntimeException("The number of phone cannot be negative.");
}
orders.put(number, Math.max(0, count));
}
public static class Phone {
private String title;
private double price;
private double number;
public Phone(String title, double price, double number) {
this.title = title;
this.price = price;
this.number = number;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getNumber() {
return number;
}
public void setNumber(double number) {
this.number = number;
}
@Override
public String toString() {
return String.format("Price is €%.2f \n%.1f number of phones", price, number);
}
}
}
Ответы на вопрос
Оно?
package application;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
public class Application {
public static void main(String[] args) {
var app = new Application();
JOptionPane.showMessageDialog(null, "Welcome to PhoneVend", "Phones", JOptionPane.DEFAULT_OPTION);
String message;
String numbers;
int customerCount = 0;
while (customerCount < 10 && (message = app.getPropositions()) != null && (numbers = app.getNumber()) != null) {
try {
var numbr = Integer.parseInt(message.trim()) - 1;
var count = Integer.parseInt(numbers.trim());
app.addOrder(numbr, count);
customerCount++;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
app.showOrders();
System.exit(0);
}
Phone[] phones;
Map orders;
public Application() {
phones = new Phone[4];
phones[0] = new Phone("iPhone 11 Pro", 800, 10);
phones[1] = new Phone("Galaxy S6", 600, 10);
phones[2] = new Phone("iPhone 12 Pro Max", 950, 9);
phones[3] = new Phone("iPhone SE", 450, 10);
orders = new HashMap<>(phones.length);
}
public String getNumber() {
UIManager.put("OptionPane.cancelButtonText", "Cancel");
UIManager.put("OptionPane.okButtonText", "Further");
return JOptionPane.showInputDialog(null, "Enter number of phones", "Phones", JOptionPane.DEFAULT_OPTION);
}
public String getPropositions() {
String message = "";
for (int i = 0; i < phones.length; i++) {
message += String.format("%d) %s (%d)%n%s%n%n", i + 1, phones[i].getTitle(), orders.getOrDefault(i, 0),
phones[i]);
}
UIManager.put("OptionPane.cancelButtonText", "Buy");
UIManager.put("OptionPane.okButtonText", "Further");
return JOptionPane.showInputDialog(null, message, "Phones", JOptionPane.DEFAULT_OPTION);
}
public void showOrders() {
String message = "";
double price = 0;
int count;
for (int i = 0; i < phones.length; i++) {
if ((count = orders.getOrDefault(i, 0)) > 0) {
message += String.format("%s (%d) %n %s %n %n", phones[i].getTitle(), count, phones[i]);