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
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
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
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
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;
}
}
|