Answer to Question #197990 in Java | JSP | JSF for Danish

Question #197990

For this assignment, you are required to understand the Problem Frames and then model the following requirements using problem frames:

For an e-commerce store, an item is added to the shopping cart, as soon as, the "Add-to-Cart" button is pressed, however, whether the item can be processed for payment, will depend on whether the item is in inventory or not, when the customer is trying to make the payment. If the item is not in inventory, when the customer is trying to pay for it, then the customer should be informed, and the item is removed from the customer's cart.


1
Expert's answer
2021-05-25T00:09:02-0400
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import javax.swing.*;


@SuppressWarnings("serial")
public class ShoppingApp extends JFrame {
	private static int ids, pos;
	private static double shopCash;
	private static HashMap<Integer, Item> items = new HashMap<>();
	private static ButtonGroup bgr = new ButtonGroup();
	private static HashMap<JRadioButton, Item> buttons = new HashMap<>();
	private static HashSet<Item> cart = new HashSet<>();


	public void init() {
		setSize(1000, 500);
		for (Item it : items.values()) {
			JLabel jlb = new JLabel(it.name);
			add(jlb);
			jlb.setLocation(30, 70 + pos * 50);
			jlb = new JLabel(it.getPrice() + "");
			jlb.setLocation(200, 70 + pos * 50);
			add(jlb);
			if (it.icon != null) {
				jlb = new JLabel(it.icon);
				jlb.setLocation(300, 70 + pos * 50);
				add(jlb);
				JRadioButton rb = new JRadioButton();
				rb.setLocation(400, 70 + pos * 50);
				bgr.add(rb);
				add(rb);
				buttons.put(rb, it);
				pos++;
				JButton addButton = new JButton("Add to Cart");
				addButton.setSize(80, 30);
				addButton.setLocation(400, 20);
				addButton.addActionListener((ActionEvent e) -> {
					for (JRadioButton r : buttons.keySet())
						if (r.isSelected()) {
							Item item = buttons.get(r);
							if (cart.contains(item))
								JOptionPane.showMessageDialog(this,
										item.getName() + " is already added to your product cart.");
							else {
								cart.add(item);
								JOptionPane.showMessageDialog(this,
										item.getName() + " is successfully addedd in your product cart.");
							}
							break;
						}
				});
			}
		}
		setVisible(true);
	}


	public void buyItem(Item it, Customer cst, int a) {
		int amt = getAmount(it.getId());
		if (amt < 1) {
			JOptionPane.showMessageDialog(this, "Sorry, the product " + "it.name" + " is out of stock.\n"
					+ "It will be removed from your product cart.");
			cart.remove(it);
			return;
		} else if (amt < a) {
			a = amt;
			JOptionPane.showMessageDialog(this, "Sorry, the item  " + "it.name" + "  is not in the quantity you want.\n"
					+ "We can offer it only in the amount of " + a + " units");
		}
		Item it2 = it.clone();
		it2.amount = a;
		cst.addProperty(it2);
		cst.setCash(cst.getCash() - it.getPrice() * a);
		shopCash += it.getPrice() * a;
		it.amount -= a;
		JOptionPane.showMessageDialog(this, "The purchase of the " + it.name + " was successful");
	}


	public int getAmount(int id) {
		if (items.containsKey(id))
			return items.get(id).getAmount();
		return 0;
	}


	class Item {
		private String name, origin, manufactureDate;
		private int id, amount;
		private double price;
		private ImageIcon icon;


		Item(String n, double p, int a) {
			name = n;
			price = p;
			amount = a;
			if (p < 0)
				price = 0;
			if (a < 0)
				amount = 0;
			id = ids++;
			items.put(id, this);
		}


		@Override
		public Item clone() {
			Item it2 = new Item(name, price, amount);
			return it2;
		}


		public String getName() {
			return name;
		}


		public void setName(String n) {
			name = n;
		}


		public double getPrice() {
			return price;
		}


		public void setPrice(double p) {
			price = p;
		}


		public int getAmount() {
			return amount;
		}


		public void setAmount(int a) {
			amount = a;
		}


		public int getId() {
			return id;
		}
	}


	static class Customer {
		private String name;
		private ArrayList<Item> property = new ArrayList<>();
		private double cash;


		public void setCash(double c) {
			cash = c;
		}


		public double getCash() {
			return cash;
		}


		public void addProperty(Item it) {
			property.add(it);
		}


	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS