Click here to Skip to main content
15,887,421 members
Home / Discussions / Java
   

Java

 
AnswerRe: Deep copy of List<> without knowing specific list type Pin
Manish K. Agarwal27-May-18 21:32
Manish K. Agarwal27-May-18 21:32 
QuestionStudent needing assistance while loop Pin
Victor McNeal21-Apr-18 9:15
Victor McNeal21-Apr-18 9:15 
AnswerRe: Student needing assistance while loop Pin
Richard MacCutchan21-Apr-18 20:56
mveRichard MacCutchan21-Apr-18 20:56 
QuestionJava JUnit test error Pin
CHUCHU YAO20-Apr-18 13:23
CHUCHU YAO20-Apr-18 13:23 
AnswerRe: Java JUnit test error Pin
Richard MacCutchan20-Apr-18 20:43
mveRichard MacCutchan20-Apr-18 20:43 
AnswerRe: Java JUnit test error Pin
jschell21-Apr-18 6:31
jschell21-Apr-18 6:31 
GeneralRe: Java JUnit test error Pin
Richard MacCutchan21-Apr-18 20:58
mveRichard MacCutchan21-Apr-18 20:58 
QuestionJAVA Swing GUI Invoice - problem with correct actionPerformed Pin
Member 1378720218-Apr-18 20:59
Member 1378720218-Apr-18 20:59 
Hello.
I am trying to prepare a window program GUI to display invoices from the system add a new invoice (and add invoice items as part of the new invoice), modify the unclosed invoice and view the details of the invoice.
The program should be written in Swing without the use of window editors. (only code). I have problem with buttons to adds new invoice with some positions.

my concept
[img]https://i.imgur.com/Ph7vndL.png[/img]


classes:

public class TestInvoice
Java
package zda;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SpringLayout;

public class TestInvoice extends JFrame
{

	public static void main(String[] args)
	{
		Contractor k1 = new Contractor("Some company \nsome adress, adress", "123-123-123");

		Invoice f1 = new Invoice(k1);
		Invoice f2 = new Invoice(k1);


		f1.addPosition("Kredki 3szt", Invoice.Measure.QTY, 5, 3.2, Invoice.VAT.s23);
		f1.addPosition("Flamastry 6szt", Invoice.Measure.QTY, 5, 4.59, Invoice.VAT.s23);
		f1.addPosition("Plastelina 12 kolorów", Invoice.Measure.QTY, 2, 8.22, Invoice.VAT.s23);
		f1.addPosition("Ołówki 3szt", Invoice.Measure.QTY, 1, 6.00, Invoice.VAT.s23);
		f1.addPosition("Ołówkek HB", Invoice.Measure.QTY, 5, 1.2, Invoice.VAT.s08);

		System.out.println(f1);

		f2.addPosition("Kredki 3szt", Invoice.Measure.QTY, 5, 3.2, Invoice.VAT.s23);
		f2.addPosition("Flamastry 6szt", Invoice.Measure.QTY, 5, 4.59, Invoice.VAT.s23);
		f2.addPosition("Plastelina 12 kolorów", Invoice.Measure.QTY, 2, 8.22, Invoice.VAT.s23);
		f2.close();
		f2.addPosition("Ołówki 3szt", Invoice.Measure.QTY, 1, 6.00, Invoice.VAT.s23);
		f2.addPosition("Ołówkek HB", Invoice.Measure.QTY, 5, 1.2, Invoice.VAT.s08);

		System.out.println(f2);
		System.out.println(Math.round(2022.0000000000002) / 100.0);

		new TestInvoice();

		// ----------------------------------------------------------------------------------------
	}

	private InvoiceManager manager;

	public TestInvoice()
	{
		manager = new InvoiceManager(5);

		setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

		JPanel leftPanel = new JPanel();
		leftPanel.setSize(400, 500);

		GroupLayout gl = new GroupLayout(leftPanel);
		gl.setAutoCreateGaps(true);
		gl.setAutoCreateContainerGaps(true);
		leftPanel.setLayout(gl);

		SpringLayout sl = new SpringLayout();
		JPanel rightPanel = new JPanel(sl);
		rightPanel.setSize(400, 500);

		JSplitPane sp = new JSplitPane();
		sp.setRightComponent(rightPanel);
		sp.setLeftComponent(leftPanel);
		sp.setPreferredSize(new Dimension(600, 600));

		JLabel lPositionsName = new JLabel("name");
		JLabel lPositionsMeasure = new JLabel("Measure");
		JLabel lPositionsQty = new JLabel("qty");
		JLabel lPositionsPrice = new JLabel("price");
		JLabel lPositionsTax = new JLabel("podatek");

		JTextField tfName = new JTextField("", 20);
		JFormattedTextField tfMeasure = new JFormattedTextField(NumberFormat.getNumberInstance());
		JFormattedTextField tfQty = new JFormattedTextField(NumberFormat.getNumberInstance());
		JFormattedTextField tfPrice = new JFormattedTextField(NumberFormat.getNumberInstance());
		JFormattedTextField tfTax = new JFormattedTextField(NumberFormat.getNumberInstance());

		gl.setVerticalGroup(gl.createSequentialGroup()
				.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lPositionsName)
						.addComponent(tfName))
				.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lPositionsMeasure)
						.addComponent(tfMeasure))
				.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lPositionsQty)
						.addComponent(tfQty))
				.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lPositionsPrice)
						.addComponent(tfPrice))
				.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lPositionsTax)
						.addComponent(tfTax)));

		gl.setHorizontalGroup(gl.createSequentialGroup()
				.addGroup(gl.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(lPositionsName)
						.addComponent(lPositionsMeasure).addComponent(lPositionsQty).addComponent(lPositionsPrice)
						.addComponent(lPositionsTax))
				.addGroup(gl.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(tfName)
						.addComponent(tfMeasure).addComponent(tfQty).addComponent(tfPrice).addComponent(tfTax)));

		JList<Invoice> list = new JList<Invoice>();
		list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		list.setLayoutOrientation(JList.VERTICAL);
		JScrollPane listScroller = new JScrollPane(list);
		listScroller.setPreferredSize(new Dimension(250, 80));
		rightPanel.add(listScroller);

		JLabel error = new JLabel("");
		error.setForeground(Color.red);

		JButton b = new JButton("save");
		b.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				String name = tfName.getText();

				int Measure = ((Number) tfMeasure.getValue()).intValue();
				int qty = ((Number) tfQty.getValue()).intValue();
				int price = ((Number) tfPrice.getValue()).intValue();
				int tax = ((Number) tfTax.getValue()).intValue();

			}

		});

		JButton o = new JButton("read");
		o.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				list.setListData(manager.getList());
			}
		});

		JButton u = new JButton("delete");
		u.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				List<Invoice> listt = list.getSelectedValuesList();

				for (Invoice w : listt)
				{
					manager.removeInvoice(w);
				}
				list.setListData(manager.getList());
			}
		});

		getContentPane().add(sp);
		getContentPane().add(error);
		getContentPane().add(b);
		getContentPane().add(o);
		getContentPane().add(u);

		pack();
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}
}



public class Invoice
Java
package zda;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;


public class Invoice
{

	private Contractor client;
	private LocalDate dateOfIssue;

	private String nr;

	private static class Identifier
	{
		static int currentNumber = 0;
		static int currentMonth = 0;
		static int currentYear = 0;

		static String generateNr()
		{
			LocalDate ld = LocalDate.now();

			int year = ld.getYear();
			int month = ld.getMonthValue();
			if (year > currentYear)
			{
				currentYear = year;
				currentMonth = ld.getMonthValue();
				currentNumber = 0;
			} else if (month > currentMonth)
			{
				currentMonth = month;
				currentNumber = 0;
			}

			return (++currentNumber) + "/" + currentMonth + "/" + currentYear;

		}
	}

	public enum Measure
	{
		QTY, M, L, KG, M2
	}

	public enum VAT
	{
		s23(.23), s08(.08), s05(.05), s00(.0);

		public double rate;
		public String str;

		VAT(double rate)
		{
			this.rate = rate;
			this.str = String.format("%.0f%%", 100 * rate);
		}
	}

	private class Position
	{
		private int position;
		private String name;
		private Measure mmasure;
		private double quantity;
		private double unitPriceWithoutTax;
		private VAT tax;

		public Position(String name, Measure mmasure, double quantity, double price, VAT tax)
		{
			this.name = name;
			this.mmasure = mmasure;
			this.quantity = quantity;
			this.unitPriceWithoutTax = price;
			this.tax = tax;
			this.position = positions.size() + 1;
		}

		public double getValue()
		{
			return quantity * unitPriceWithoutTax;
		}

		public double getTax()
		{
			double d = quantity * unitPriceWithoutTax * tax.rate;
			d = Math.round(d * 100) / 100.;
			return d;
		}

		@Override
		public String toString()
		{
			return String.format("%5d | %30s | %10s | %10s | %10s | %10s | %10.2f", position, name, mmasure, quantity,
					unitPriceWithoutTax, tax.str, getValue() + getTax());
		}
	}

	public static String heading()
	{
		return String.format("%5s | %30s | %10s | %10s | %10s | %10s | %10s", "LP.", "name", "mmasure", "quantity",
				"Unit price.", "tax", "price with tax");
	}

	private ArrayList<Position> positions = new ArrayList<Position>();

	public void addPosition(String name, Measure m, double quantity, double price, VAT tax)
	{
		if (closed)
			return;

		Position p = new Position(name, m, quantity, price, tax);
		positions.add(p);
		double d = subtotals.get(tax);
		d += p.getTax();
		subtotals.put(tax, d);
		sumWithTax += p.getValue();
		sumPayment += p.getValue() + p.getTax();
	}

	private boolean closed = false;

	public void close()
	{
		closed = true;
	}

	private HashMap<VAT, Double> subtotals = new HashMap<>();
	private double sumWithTax = 0;
	private double sumPayment = 0;

	public Invoice(Contractor c)
	{
		nr = Identifier.generateNr();
		client = c;
		dateOfIssue = LocalDate.now();

		for (VAT v : VAT.values())
		{
			subtotals.put(v, 0.);
		}
	}

	@Override
	public String toString()
	{
		String invoice = "---------------------------------------------\n";
		invoice += (closed ? "C-" : "O-") + "invoice no." + nr + "\n";
		invoice += "from date " + dateOfIssue.format(DateTimeFormatter.ofPattern("d-M-y")) + "\n";
		invoice += "\nFor: \n" + client + "\n\n";
		invoice += heading() + "\n";
		for (Position p : positions)
		{
			invoice += p.toString() + "\n";
		}
		invoice += "\n";
		invoice += String.format("%-15s: %10.2f\n", "Sum without tax", sumWithTax);
		for (HashMap.Entry<VAT, Double> e : subtotals.entrySet())
		{
			if (e.getValue() > 0)
				invoice += String.format("%-15s: %10.2f\n", "SUM " + e.getKey().str, e.getValue());
		}
		invoice += String.format("%27s\n", "+ ----------");
		invoice += String.format("%-15s: %10.2f\n", "sum to pay", sumPayment);
		invoice += "---------------------------------------------\n";
		return invoice;
	}

}



public class InvoiceManager
Java
package zda;

import java.util.ArrayList;

public class InvoiceManager
{

	private int maxNumber = 10;
	private ArrayList<Invoice> listOfInvoices = new ArrayList<Invoice>();
	
	public void addInvoice(Invoice w) throws IllegalArgumentException{
		if(listOfInvoices.size()< maxNumber)
			listOfInvoices.add(w);
		else 
			throw new IllegalArgumentException("No space for a new invoice");
	}
	public void removeInvoice(Invoice w){
		listOfInvoices.remove(w);
	}
	public Invoice[] getList(){
		return (Invoice[]) listOfInvoices.toArray(new Invoice[listOfInvoices.size()]);
	}
	
	public InvoiceManager(int max){
		maxNumber = max;
	}
	
}



public class Contractor
Java
package zda;

public class Contractor
{
	String data;
	String id;

	public Contractor(String data, String id)
	{
		this.data = data;
		this.id = id;
	}

	@Override
	public String toString()
	{
		return data + "\n" + "NIP: " + id;
	}

}

QuestionRe: JAVA Swing GUI Invoice - problem with correct actionPerformed Pin
Richard MacCutchan18-Apr-18 21:51
mveRichard MacCutchan18-Apr-18 21:51 
AnswerRe: JAVA Swing GUI Invoice - problem with correct actionPerformed Pin
Member 1378720219-Apr-18 0:54
Member 1378720219-Apr-18 0:54 
GeneralRe: JAVA Swing GUI Invoice - problem with correct actionPerformed Pin
Richard MacCutchan19-Apr-18 1:02
mveRichard MacCutchan19-Apr-18 1:02 
AnswerRe: JAVA Swing GUI Invoice - problem with correct actionPerformed Pin
snoopy1118-Apr-18 23:17
snoopy1118-Apr-18 23:17 
JokeRe: JAVA Swing GUI Invoice - problem with correct actionPerformed Pin
Peter_in_278019-Apr-18 0:39
professionalPeter_in_278019-Apr-18 0:39 
Questiondesign a class Pin
Member 1365742917-Apr-18 21:18
professionalMember 1365742917-Apr-18 21:18 
GeneralRe: design a class Pin
Richard MacCutchan17-Apr-18 22:41
mveRichard MacCutchan17-Apr-18 22:41 
Questionjdbc connection Pin
Member 137686618-Apr-18 7:26
Member 137686618-Apr-18 7:26 
AnswerRe: jdbc connection Pin
Richard MacCutchan8-Apr-18 21:51
mveRichard MacCutchan8-Apr-18 21:51 
Questioni need help fast.i have only 2 days left Pin
Member 137677117-Apr-18 11:35
Member 137677117-Apr-18 11:35 
Questionhow to protect jar file from decompilation Pin
Renu Meena15-Mar-18 18:44
Renu Meena15-Mar-18 18:44 
AnswerRe: how to protect jar file from decompilation Pin
Richard MacCutchan15-Mar-18 22:09
mveRichard MacCutchan15-Mar-18 22:09 
GeneralRe: how to protect jar file from decompilation Pin
Renu Meena15-Mar-18 23:02
Renu Meena15-Mar-18 23:02 
GeneralRe: how to protect jar file from decompilation Pin
Richard MacCutchan16-Mar-18 1:30
mveRichard MacCutchan16-Mar-18 1:30 
GeneralRe: how to protect jar file from decompilation Pin
Renu Meena18-Mar-18 20:34
Renu Meena18-Mar-18 20:34 
GeneralRe: how to protect jar file from decompilation Pin
Richard MacCutchan18-Mar-18 22:32
mveRichard MacCutchan18-Mar-18 22:32 
GeneralRe: how to protect jar file from decompilation Pin
jschell17-Mar-18 6:16
jschell17-Mar-18 6:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.