Programming using java: Classes (Graphical User Interface) Objectives: • Use of
ID: 3708874 • Letter: P
Question
Programming using java: Classes (Graphical User Interface)
Objectives:
• Use of Classes
• User of Methods
• Use of GUI (Graphical User Interface)
• Use of class inharitance
Scenarios:
1. Expanding on the bank account class, create a graphical user interface that will allow the user to either deposit or withdraw money from the account and display the balance. You need to create your GUI. Below is an idea of how the solution should look like:
2. Modify the BankAccount class previously created to add two methods: deposit and withdraw. These will be used to deposit of withdraw money from the bank account. Extend the BankAccount class to create two types of bank accounts: a checking account and a
savings account. These will inherit the properties of bank account class, but have the
following unique behavior:
• When money get deposited into the savings account, an automatic 1% interest
is added to the deposited amount
• The savings account does not allow withdraw
• When money get withdrawn from the checking account,a withdrawing fee of $1 is applied.
To test the functionalities of the BankAccount, CheckingAccount and SavingsAccount classes a class called BankTest will be created. This class will have a main method which will be used to
• Create at least one instance of each bank account type, i.e BankAccount,
CheckingAccount and SavingsAccount
• Deposit/withdraw money from these accounts
• Display (output) the balance and interest values from these accounts
Explanation / Answer
As per your requirement the below one is solution please follow it step by step
Solution for scenario 1
Below is the working module of bank,Kindly run UseBank.java class for output
Class1: UseBankClass for update the balance
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cheggquestions;
/**
*
* @author Prashant Tomer
*/
public class UseBankClass {
int balance;
public int deposit(int mon)
{
balance=balance+mon;
return balance;
}
public int withdraw(int mon)
{
if(balance>0)
balance=balance-mon;
return balance;
}
}
Class 2: GUI class Swing
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cheggquestions;
/**
*
* @author Prashant Tomer
*/
public class UseBank extends javax.swing.JFrame {
int userinput;
UseBankClass bank=new UseBankClass();
int updatebalance;
/**
* Creates new form UseBank
*/
public UseBank() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jLabel1.setText("Amount");
jButton1.setText("Withdraw");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Deposit");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel3.setText("Balance");
jLabel4.setText("$");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(40, 40, 40)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(43, 43, 43)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
.addComponent(jLabel3)
.addGap(109, 109, 109))))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(46, 46, 46))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(93, 93, 93)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(88, 88, 88)
.addComponent(jLabel3)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(115, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
userinput= Integer.parseInt(jTextField1.getText());
updatebalance=bank.withdraw(userinput);
jLabel4.setText("$"+Integer.toString(updatebalance));
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
userinput= Integer.parseInt(jTextField1.getText());
updatebalance=bank.deposit(userinput);
jLabel4.setText("$"+Integer.toString(updatebalance));
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(UseBank.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(UseBank.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(UseBank.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(UseBank.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new UseBank().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
Solution for 2 scenario
BankAccount.java
package banktest;
import java.util.Scanner;
public class BankAccount {
public float amount,w_amount;
public void deposit()
{
System.out.println("Please enter amount to deposit:");
Scanner sc1 = new Scanner(System.in);
amount = sc1.nextFloat();
System.out.println("The amount deposited by you is:" + amount);
}
public void withdrawal()
{
System.out.println("Please enter the withdrawal amount:");
Scanner sc2 = new Scanner(System.in);
w_amount = sc2.nextFloat();
amount = amount - w_amount;
System.out.println("Your withdrawal amount is:" + w_amount);
System.out.println("Your remaining deposit amount is:" + amount);
}
}
_____________________________________________________________________________________________
Saving_account.java
package banktest;
import java.util.Scanner;
public class Saving_account extends BankAccount {
public float i_amount,total_deposit;
public void deposit()
{
System.out.println("Please enter amount to deposit:");
Scanner sc1 = new Scanner(System.in);
amount = sc1.nextFloat();
i_amount = (float) (0.01 * amount);
total_deposit = amount + i_amount;
System.out.println("Your deposit amount is:" + amount);
System.out.println("Interest applied to the deposit amount:" + i_amount);
System.out.println("Your total deposit is:" + total_deposit);
}
public void withdrawal()
{
System.out.println("Withdrawal is not allowed with Saving account");
}
}
_______________________________________________________________________________________________
Checking_account.java
package banktest;
import java.util.Scanner;
public class Checking_account extends BankAccount{
public float withdrawing_fee=1,total_wamount;
public void withdrawal()
{
System.out.println("Please enter the withdrawal amount:");
Scanner sc2 = new Scanner(System.in);
w_amount = sc2.nextFloat();
total_wamount = w_amount + withdrawing_fee;
amount = amount - total_wamount;
System.out.println("Your withdrawal amount is:" + w_amount);
System.out.println("Withdrawing fees applied is:" + withdrawing_fee);
System.out.println("Total withdrawal amount is:" + total_wamount);
System.out.println("Your remaining deposit amount is:" + amount);
}
}
______________________________________________________________________________________________
BankTest.java
package banktest;
public class BankTest {
public static void main(String[] args) {
System.out.println("Deposit in a basic Bank Account");
BankAccount ba = new BankAccount();
ba.deposit();
ba.withdrawal();
System.out.println("_______________________________________________________");
System.out.println("Deposit in a Savings Account");
Saving_account sa = new Saving_account();
sa.deposit();
sa.withdrawal();
System.out.println("________________________________________________________");
System.out.println("Deposit in a Checking Account");
Checking_account ca = new Checking_account();
ca.deposit();
ca.withdrawal();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.