From f7c307a2947def0b1d403145e225cbf45f2581c9 Mon Sep 17 00:00:00 2001 From: Giacomo Radaelli Date: Mon, 15 May 2023 18:38:39 +0200 Subject: [PATCH] Aggiunti esercizi pagine 516-525 --- .../Grafica/src/investment/BankAccount.java | 28 +++++++++++ .../src/investment/InvestmentViewer1.java | 39 ++++++++++++++++ .../src/investment/InvestmentViewer2.java | 46 +++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 NetBeans Projects/Grafica/src/investment/BankAccount.java create mode 100644 NetBeans Projects/Grafica/src/investment/InvestmentViewer1.java create mode 100644 NetBeans Projects/Grafica/src/investment/InvestmentViewer2.java diff --git a/NetBeans Projects/Grafica/src/investment/BankAccount.java b/NetBeans Projects/Grafica/src/investment/BankAccount.java new file mode 100644 index 0000000..b078135 --- /dev/null +++ b/NetBeans Projects/Grafica/src/investment/BankAccount.java @@ -0,0 +1,28 @@ +package investment; + +public class BankAccount { + private double balance; + + public BankAccount() { + balance = 0; + } + + public BankAccount(double initialBalance) { + balance = initialBalance; + } + + public void deposit(double amount) { + double newBalance = balance + amount; + balance = newBalance; + } + + public void withdraw(double amount) { + double newBalance = balance - amount; + balance = newBalance; + } + + public double getBalance() { + return balance; + } +} + diff --git a/NetBeans Projects/Grafica/src/investment/InvestmentViewer1.java b/NetBeans Projects/Grafica/src/investment/InvestmentViewer1.java new file mode 100644 index 0000000..41d5d8d --- /dev/null +++ b/NetBeans Projects/Grafica/src/investment/InvestmentViewer1.java @@ -0,0 +1,39 @@ +package investment; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JButton; +import javax.swing.JFrame; + +public class InvestmentViewer1 { + private static final int FRAME_WIDTH = 120; + private static final int FRAME_HEIGHT = 60; + + private static final double INTEREST_RATE = 10; + private static final double INITIAL_BALANCE = 1000; + + public static void main(String[] args) { + JFrame frame = new JFrame(); + frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + BankAccount account = new BankAccount(INITIAL_BALANCE); + + JButton button = new JButton("Aggiungi interessi"); + frame.add(button); + + class InterestListener implements ActionListener { + public void actionPerformed(ActionEvent event) { + double interest = account.getBalance() * INTEREST_RATE / 100; + account.deposit(interest); + System.out.println("saldo: " + account.getBalance()); + } + } + + ActionListener listener = new InterestListener(); + button.addActionListener(listener); + + frame.setVisible(true); + } +} + diff --git a/NetBeans Projects/Grafica/src/investment/InvestmentViewer2.java b/NetBeans Projects/Grafica/src/investment/InvestmentViewer2.java new file mode 100644 index 0000000..f237b37 --- /dev/null +++ b/NetBeans Projects/Grafica/src/investment/InvestmentViewer2.java @@ -0,0 +1,46 @@ +package investment; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; + +public class InvestmentViewer2 { + private static final int FRAME_WIDTH = 400; + private static final int FRAME_HEIGHT = 100; + + private static final double INTEREST_RATE = 10; + private static final double INITIAL_BALANCE = 1000; + + public static void main(String[] args) { + JFrame frame = new JFrame(); + frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + BankAccount account = new BankAccount(INITIAL_BALANCE); + + JButton button = new JButton("Aggiungi interessi"); + JLabel label = new JLabel("Saldo: " + account.getBalance()); + JPanel panel = new JPanel(); + + panel.add(button); + panel.add(label); + frame.add(panel); + + class InterestListener implements ActionListener { + public void actionPerformed(ActionEvent event) { + double interest = account.getBalance() * INTEREST_RATE / 100; + account.deposit(interest); + label.setText("Saldo: " + account.getBalance()); + } + } + + ActionListener listener = new InterestListener(); + button.addActionListener(listener); + + frame.setVisible(true); + } +} +