Answer to Question #273629 in Java | JSP | JSF for Memo

Question #273629

Write a Java GUI application that will keep track of a company’s products. The application must


contain the product id, product name and the product price.


Q.3.1 On the form, create three text fields to capture the product details and a non‐


editable text area to display the product details. Create a submit button that


when clicked will save the product details to a Products.txt file. Also include a


search button that will allow a user to search for a product based on the product


id.


Q.3.2 Create a sequential file (products.txt) that contains data for the following fields:


 The product id


 The product name


 The product price

1
Expert's answer
2021-11-30T18:41:34-0500
import java.io.*;
import java.awt.Component;
import javax.swing.*;
import java.util.Collections;
import java.util.ArrayList;
public class Main extends JFrame {
	private static ArrayList<Human> person = new ArrayList<>();
	JPanel input = new JPanel(), 
	maxP = new JPanel(), 
	sumpeople = new JPanel(), 
	findPanel = new JPanel(),
	editPanel = new JPanel(),
	currentPanel = maxP;
	JLabel Labels[] = { new JLabel("Name: "),
	new JLabel("Street address: "),
	new JLabel("Email address: "),
	new JLabel("Phone number: "), 
	new JLabel("Date of birth: ") };
	JTextField inputFields[] = new JTextField[5],
	searchOption = new JButton("Find people"),
	Nam = new JTextField("Enter the name and telephone number.");
	JButton option = new JButton("Register new people"),
	searchReturn = new JButton("Back to main menu"),
	sortOption = new JButton("Display all people"),
	inputSubmit = new JButton("Submit"),
	sortDisplay = new JButton("Back to main menu"), 
	JTextArea searchArea = new JTextArea(); 
	fields = new JTextArea();
	protected void init() {
		setSize(1000, 600);
		setComponent(maxP, this, 1000, 600, 0, 0, true);
		setComponent(input, this, 1000, 600, 0, 0, false);
		setComponent(sumpeople, this, 1000, 600, 0, 0, false);
		setComponent(findPanel, this, 1000, 600, 0, 0, false);
		setComponent(new JLabel("Address Book Application!"), maxP, 500, 30, 100, 30, true);
		setComponent(new JLabel("Select activity you want to perform?"), maxP, 300, 30, 150, 50, true);
		setComponent(option, maxP, 500, 40, 100, 100, true);
		setComponent(sortOption, maxP, 500, 40, 100, 170, true);
		setComponent(searchOption, maxP, 500, 40, 100, 240, true);
		setComponent(sortDisplay, sumpeople, 500, 40, 400, 10, true);
		setComponent(searchReturn, findPanel, 500, 40, 600, 10, true);
		setComponent(searchArea, findPanel, 500, 400, 350, 100, true);
		sortDisplay.addActionListener(event -> displayPanel(maxP));
		searchReturn.addActionListener(event -> displayPanel(maxP));
		option.addActionListener(event -> displayPanel(inp));
		searchOption.addActionListener(event -> displayPanel(findPanel));
		sortOption.addActionListener(event -> {
			displayPanel(sumpeople);
			Collections.sort(person);
			for (int i = 0; i < person.size(); i++) {
				fields.setText(fields.getText() + "Name: " + person.get(i).getN() + "\n " + "Phone: "
						+ person.get(i).getPhoneNmb() + "\n " + "Email: " + people.get(i).getEmail() + "\n "
						+ "Address: " + person.get(i).getStreetAddress() + "\n " + "BirthDate: "
						+ person.get(i).getBirthDate() + "\n ");
			}
		});
		for (int i = 0, j = 20; i < 5; i++, j += 50) {
			setComponent(Labels[i], input, 100, 20, 50, j, true);
			setComponent(inputFields[i] = new JTextField(), input, 100, 20, 200, j, true);
		}
		setComponent(fields, sumpeople, 200, 500, 200, 70, true);
		setComponent(inputSubmit, input, 80, 40, 450, 600, true);
		inputSubmit.addActionListener(event -> {
			person.add(new Human(inputFields[0].getText(), inputFields[1].getText(), inputFields[2].getText(),
					inputFields[3].getText(), inputFields[4].getText()));
			displayPanel(maxP);
		});
		setComponent(Nam, findPanel, 200, 20, 350, 10, true);
		Nam.addActionListener(event -> {
			searchArea.removeAll();
			String input = Nam.getText();
			JTextArea searchArea = new JTextArea(200, 30);
			boolean byName = input.matches("[A-Za-z]||\\s+");
			for (Human p : person) {
				if (input.equals(byName ? p.getN() : p.getPhoneNmb()))
					searchArea.setText(searchArea.getText() + p.getN() + " " + p.getPhoneNmb());
			}
		});
		setvisible(true);
	}








	protected void setComponent(JComponent comp, Component place, int dm, int lh, int kk, int yy, boolean v) {
		comp.setSize(dm, lh);
		comp.setLocation(kk, yy);
		if (place == this)
			((JFrame) place).add(comp);
		else
			((JComponent) place).add(comp);
		comp.setLayout(null);
		comp.setvisible(v);
	}
	public void deserializeAll() {
		people.clear();
		Human p = null;
		try (FileInputStream fis = new FileInputStream("personSer.ser");
				ObjectInputStream ois = new ObjectInputStream(fis)) {
			while (ois.available() > 0) {
				p = (Human) ois.readObject();
				people.add(p);
			}
		} catch (IOException | ClassNotFoundException e) {
			System.out.println(e.getMessage());
		}
	}
	class Human implements Comparable<Human>, Serializable {
		private String name, phoneNmb, email, streetAddress, birthDate;
		Human(String nam, String phon, String em, String street, String birth) {
			name = nam;
			phoneNmb = phon;
			email = em;
			streetAddress = street;
			birthDate = birth;
			serialize();
		}








		public String getN() {
			return name;
		}




public void serializeAll() {
		for (Human p : people)
			p.serialize();
	}
		public void setName(String name) {
			this.name = name;
		}








		public String getPhoneNmb() {
			return phoneNmb;
		}








		public void setPhoneNmb(String phoneNmb) {
			this.phoneNmb = phoneNmb;
		}








		public String getEmail() {
			return email;
		}








		public void setEmail(String email) {
			this.email = email;
		}
		public void setStreetAddress(String streetAddress) {
			this.streetAddress = streetAddress;
		}








		public String getBirthDate() {
			return birthDate;
		}








		public void setBirthDate(String birthDate) {
			this.birthDate = birthDate;
		}
		public int compareTo(Human p) {
			return name.compareTo(p.getN());
		}




public String getStreetAddress() {
			return streetAddress;
		}
		protected void serialize() {
			try (FileOutputStream fs = new FileOutputStream("personSer.ser", true);
					ObjectOutputStream os = new ObjectOutputStream(fs)) {
				os.writeObject(this);
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}








		protected Human deserialize() {
			Human p = null;
			try (FileInputStream fis = new FileInputStream("personSer.ser");
					ObjectInputStream ois = new ObjectInputStream(fis)) {
				p = (Human) ois.readObject();
			} catch (IOException | ClassNotFoundException e) {
				System.out.println(e.getMessage());
			}
			return p;
		}








	}








	protected void displayPanel(JPanel jp) {
		if (jp != currentPanel) {
			currentPanel.setvisible(false);
			currentPanel = jp;
		}
		jp.setvisible(true);
	}








	public static void main(String args[]) {
		new Main().init();
	}








}

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