I have two objects derived from an Account class stored in an ArrayList. I am trying to call overridden methods with a basic for loop but I can't get it to work consistently.
import java.util.ArrayList;
import java.util.Scanner;
public class InheritanceTest
{
private static class Account
{
//Constructor
Account()
{
}
private void processDeposit()
{
double deposit = 0;
System.out.println("Please enter the deposit amount: ");
deposit = input.nextDouble();
balance += deposit;
}
private void processWithdrawal()
{
double withdrawal = 0;
System.out.println("Please enter the amount to be drawn from the account: ");
withdrawal = input.nextDouble();
balance -= withdrawal;
}
public String getAccountName() {
System.out.println("Creating " + "account, enter name: ");
accountName = input.nextLine();
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public int getAcctID() {
System.out.println("Your " + "account number is " + acctID);
return acctID;
}
public void setAcctID(int acctID) {
this.acctID = acctID;
acctID++;
}
}
//
private static class CheckingAccount extends Account
{
public String getAccountName() {
System.out.println("Creating checking account, enter name: ");
accountName = input.nextLine();
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
private void processWithdrawal()
{
double withdrawal = 0;
System.out.println("Please enter the amount to be drawn from the account: ");
withdrawal = input.nextDouble();
if (withdrawal > balance)
{
balance -= 10;
System.out.println("Overdraft fee has been charged");
} else
balance -= withdrawal;
}
}
//
private static class SavingsAccount extends Account
{
public String getAccountName() {
System.out.println("Creating savings account, enter name: ");
accountName = input.nextLine();
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
private void processWithdrawal()
{
double withdrawal = 0;
System.out.println("Please enter the amount to be drawn from the account: ");
withdrawal = input.nextDouble();
if(withdrawal > balance)
{
System.out.println("Insufficient funds");
} else
{
balance -= withdrawal;
}
}
}
//
public static void main(String[] args)
{
ArrayList<Account> Bank = new ArrayList<Account>();
CheckingAccount checking1 = new CheckingAccount();
Bank.add(checking1);
SavingsAccount savings = new SavingsAccount();
Bank.add(savings);
for(int i = 0; i<Bank.size(); i++)
{
Bank.get(i).getAccountName();
Bank.get(i).setAccountName(Bank.get(i).accountName);
Bank.get(i).getAcctID();
Bank.get(i).setAcctID(Bank.get(i).acctID);
}
for (int i =0; i<Bank.size(); i++)
{
System.out.println("Processing Account: " + Bank.get(i).acctID);
Bank.get(i).processDeposit();
System.out.println("Balance: " + Bank.get(i).balance);
Bank.get(i).processWithdrawal();
System.out.println("Balance: " + Bank.get(i).balance);
Bank.get(i).processWithdrawal();
Bank.get(i).displayAccount(Bank.get(i).acctID, Bank.get(i).accountName, Bank.get(i).balance);
}
}
}
The first loop calls the overridden getAccountName methods just fine, yet the second loop only seems to be calling the processWithdrawal method from the base class. Can anyone point me in the direction of finding out why this might be happening?
private void processWithdrawal()
make it public
public void processWithdrawal()
When you would like to override a method i.e. leverage the feature of Dynamic Polymorphism the method should be protected/public.
A private method is restricted only to that specific class in which it is declared which wouldnt make Overriding possible.
Related
So I'm supposed to design and implement an ADT that represents a credit card. It should include customer name, account number, due date, reward points, and account balance. There should be operations for a credit card charge, a cash advance, a payment, the addition of interest to the balance, and the display of the statistics of the bank account. However when I run my program,
I keep getting an error saying "Class "creditCard" does not have a main method."
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;
public class creditCard {
private int customerName;
private int accountNumber;
private int dueDate;
private int rewardPoints;
private int accountBalance;
static Scanner input = new Scanner(System.in);
double creditLimit = 500;
int user = 0;
public int getCustomerName() {
return customerName;
}
public void setCustomerName(int customerName) {
this.customerName = customerName;
}
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public int getDueDate() {
return dueDate;
}
public void setDueDate(int dueDate) {
this.dueDate = dueDate;
}
public int getRewardPoints() {
return rewardPoints;
}
public void setRewardPoints(int rewardPoints) {
this.rewardPoints = rewardPoints;
}
public int getAccountBalance() {
return accountBalance;
}
public void setAccountBalance(int accountBalance) {
this.accountBalance = accountBalance;
}
public String charge(float amount) {
if (amount > creditLimit) {
System.out.println("You have exceeded your credit card limit and no charge can be made");
}
if (creditLimit - amount > 0) {
System.out.println("You have exceeded your credit card limit and no charge can be made");
}
creditLimit = creditLimit - amount;
System.out.println("A charge of" + amount + "has been made.");
String charge = "A charge of" + amount + "has been made.";
return charge;
}
public String advance(float amount) {
if (amount > creditLimit) {
System.out.println("You have exceeded your credit card limit and no charge can be made");
}
if (creditLimit - amount > 0) {
System.out.println("You have exceeded your credit card limit and no charge can be made");
}
creditLimit = creditLimit - amount;
System.out.println("You have withdrawn $ " + amount);
String advance = "You have withdrawn $ " + amount;
return advance;
}
public String payment(float amount) {
creditLimit = creditLimit + amount;
System.out.println("You have a credit limit of $ " + creditLimit);
String payment = "You have a credit limit of $ " + creditLimit;
return payment;
}
public String interest() {
creditLimit = (creditLimit * 0.02) + creditLimit;
String interest = "An interest of" + creditLimit * 0.02 + "has been added to your balance";
return interest;
}
public void statistics(String charge, String advance, String payment, String interest) {
System.out.println(charge);
System.out.println(advance);
System.out.println(payment);
System.out.println(interest);
}
public void showMenu() {
while (user!= 5) {
System.out.println("Choose Charge(1) \nAdvance(2) \nPayment(3) \nInterest(4) \ncStatistics(5)");
user = input.nextInt();
switch (user) {
case 1:
float amount;
System.out.println("Enter the amount: ");
amount = input.nextFloat();
String charge = charge(amount);
break;
case 2:
float withdrawalAmount;
System.out.println("Enter amount to withdraw: ");
withdrawalAmount = input.nextFloat();
String advance = advance(withdrawalAmount);
break;
case 3:
float paymentAmount;
System.out.println("Enter payment.");
paymentAmount = input.nextFloat();
String payment = payment(paymentAmount);
break;
case 4:
String interest = interest();
break;
//case 5:
//statistics(charge, advance, payment, interest);
//break;
}
}
}
public void main(String[] args) {
showMenu();
}
}
make the main method static.
public static void main(String[] args) {
showMenu();
}
or better create another entry point class and put your main there.
public class Runner {
public static void main(String[] args) {
CreditCard creditCard= new CreditCard();
creditCard.showMenu();
// Do something else
}
I have problem on how to print the polymorphic object in the array, where inheritance is applicable. I know how to store it in the single array, but when it comes to print the object, I am totally stuck.
Here is my main class:
public class AccountArray {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n, i = 0;
int numAcc;
double balance, rate;
Account[] bank = new Account[2];
do {
System.out.print("\n[1]Current || [2]Saving >> ");
n = in.nextInt();
System.out.println("------------");
if (n == 1) {
bank[i] = new Current();
System.out.println("#Current#");
System.out.println("------------");
System.out.print("Please enter account number: ");
numAcc = in.nextInt();
bank[i].setAccountNumber(numAcc);
System.out.print("Enter balance: ");
balance = in.nextDouble();
bank[i].setAccountBalance(balance);
i++;
} else if (n == 2) {
bank[i] = new Saving();
System.out.println("#Saving#");
System.out.println("------------");
System.out.print("Please enter account number: ");
numAcc = in.nextInt();
bank[i].setAccountNumber(numAcc);
System.out.print("Enter balance: ");
balance = in.nextDouble();
bank[i].setAccountBalance(balance);
System.out.print("Interest rate: ");
rate = in.nextDouble();
((Saving) bank[i]).setInterest(rate);
System.out.println("---------------------------------------");
i++;
}
} while (i < bank.length);
//Output
System.out.println("### Data Output ###");
for (int d = 0; d < bank.length; d++) {
System.out.println("--------------------");
System.out.println("Account Number: \t" + bank[d].getAccountNumber());
System.out.println("Balance : \t" + bank[d].getAccountBalance());
if (bank[d] == new Saving()) {
System.out.println("Interest rate : " + ((Saving) bank[d]).getInterest());
}
}
}
Here is my Account class with abstract class:
public abstract class Account {
private int numAcc;
private double balance;
public Account() {
}
public Account(int numAcc, double balance) {
this.numAcc = numAcc;
this.balance = balance;
}
public int getAccountNumber() {
return numAcc;
}
public void setAccountNumber(int numAcc) {
this.numAcc = numAcc;
}
public double getAccountBalance() {
return balance;
}
public void setAccountBalance(double balance) {
this.balance = balance;
}
public abstract void display();
}
Here is the Saving Class
public class Saving extends Account {
private double interest;
public Saving(int numAcc, double balance, double interest) {
super(numAcc, balance);
this.interest = interest;
}
public Saving() {
}
public void setInterest(double interest) {
this.interest = interest;
}
public double getInterest() {
return interest;
}
#Override
public void display() {
System.out.println("Saving Account Information");
System.out.println("account number: " + getAccountNumber());
System.out.println("balance: RM " + getAccountBalance());
System.out.println("interest: " + getInterest() + " %");
}
}
This is my current class:
public class Current extends Account {
public Current(int accNum, double balance) {
super(accNum, balance);
}
public Current() {
}
#Override
public void display() {
System.out.println("Current Account Information");
System.out.println("account number: " + getAccountNumber());
System.out.println("balance: RM " + getAccountBalance());
}
}
Other approach is to override toString() method of each Object.
bank[d] == new Saving() this is wrong and you try if(bank[d] instance of Saving)
i have a program that create an array of Account object by asking the user to enter their properties than the system will make of these inserted input an array.
this is my code
Account.java
package question1;
import java.util.Date;
public class Account {
public int AccountNum;
public double BALANCE;
public Date OPENDATE;
public String OwnerName;
public Account() {
// TODO Auto-generated constructor stub
}
public Account(int accnum, double balance, Date opendate, String ownername) {
this.AccountNum = accnum;
this.BALANCE = balance;
this.OPENDATE = opendate;
this.OwnerName = ownername;
}
public int getAccountNum() {
return AccountNum;
}
public void setAccountNum(int accountNum) {
AccountNum = accountNum;
}
public double getBALANCE() {
return BALANCE;
}
public void setBALANCE(double bALANCE) {
BALANCE = bALANCE;
}
public Date getOPENDATE() {
return OPENDATE;
}
public void setOPENDATE(Date oPENDATE) {
OPENDATE = oPENDATE;
}
public String getOwnerName() {
return OwnerName;
}
public void setOwnerName(String ownerName) {
OwnerName = ownerName;
}
public double yearlyInterest(double balace) {
return balace;
}
}
Bank.java
package question1;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class Bank {
public static void main(String[] args) {
List<Account> accounts = new ArrayList<Account>(4);
Scanner sc = new Scanner(System.in);
while(!sc.hasNext()){
System.out.println("enter your balance");
int b = sc.nextInt();
System.out.println("enter your name");
String s = sc.nextLine();
}
}
}
i do not know how to continue in the Bank Class and how to create this array using the scanner.
if anyone can help me i will appreciate this.
You can do something like this
Scanner scanner = new Scanner(System.in);
System.out.println("Do you wanna create new account?Y/N");
while(scanner.next().equalsIgnoreCase("y")){
//Get needed information. You shouldn't take account number from user. It must be generated
System.out.println("Enter Name");
String name = scanner.next();
System.out.println("Enter Account Balance");
float balance = scanner.nextFloat();
......
//create new instance of Account and set values
Account acc = new Account();
acc.setBalance(balance);
.....
//add instance to list
accounts.add(acc);
System.out.println("Do you wanna create new account?Y/N");
}
I think you are studying for the exams of Lebanese army LOL anw this is the code bro.
AccountDriver
//Demonstrates the BankAccount and derived classes
import java.text.*; // to use Decimal Format
public class AccountDriver
{
public static void main(String[] args)
{
double put_in = 500;
double take_out = 1000;
DecimalFormat myFormat;
String money;
String money_in;
String money_out;
boolean completed;
// to get 2 decimals every time
myFormat = new DecimalFormat("#.00");
//to test the Checking Account class
CheckingAccount myCheckingAccount =
new CheckingAccount ("Benjamin Franklin", 1000);
System.out.println ("Account Number "
+ myCheckingAccount.getAccountNumber() +
" belonging to " + myCheckingAccount.getOwner());
money = myFormat.format(myCheckingAccount.getBalance());
System.out.println ("Initial balance = $" + money);
myCheckingAccount.deposit (put_in);
money_in = myFormat.format(put_in);
money = myFormat.format(myCheckingAccount.getBalance());
System.out.println ("After deposit of $" + money_in
+ ", balance = $" + money);
completed = myCheckingAccount.withdraw(take_out);
money_out = myFormat.format(take_out);
money = myFormat.format(myCheckingAccount.getBalance());
if (completed)
{
System.out.println ("After withdrawal of $" + money_out
+ ", balance = $" + money);
}
else
{
System.out.println ("Insuffient funds to withdraw $"
+ money_out + ", balance = $" + money);
}
System.out.println();
//to test the savings account class
SavingsAccount yourAccount =
new SavingsAccount ("William Shakespeare", 400);
System.out.println ("Account Number "
+ yourAccount.getAccountNumber() +
" belonging to " + yourAccount.getOwner());
money = myFormat.format(yourAccount.getBalance());
System.out.println ("Initial balance = $" + money);
yourAccount.deposit (put_in);
money_in = myFormat.format(put_in);
money = myFormat.format(yourAccount.getBalance());
System.out.println ("After deposit of $" + money_in
+ ", balance = $" + money);
completed = yourAccount.withdraw(take_out);
money_out = myFormat.format(take_out);
money = myFormat.format(yourAccount.getBalance());
if (completed)
{
System.out.println ("After withdrawal of $" + money_out
+ ", balance = $" + money);
}
else
{
System.out.println ("Insuffient funds to withdraw $"
+ money_out + ", balance = $" + money);
}
yourAccount.postInterest();
money = myFormat.format(yourAccount.getBalance());
System.out.println ("After monthly interest has been posted,"
+ "balance = $" + money);
System.out.println();
// to test the copy constructor of the savings account class
SavingsAccount secondAccount =
new SavingsAccount (yourAccount,5);
System.out.println ("Account Number "
+ secondAccount.getAccountNumber()+
" belonging to " + secondAccount.getOwner());
money = myFormat.format(secondAccount.getBalance());
System.out.println ("Initial balance = $" + money);
secondAccount.deposit (put_in);
money_in = myFormat.format(put_in);
money = myFormat.format(secondAccount.getBalance());
System.out.println ("After deposit of $" + money_in
+ ", balance = $" + money);
secondAccount.withdraw(take_out);
money_out = myFormat.format(take_out);
money = myFormat.format(secondAccount.getBalance());
if (completed)
{
System.out.println ("After withdrawal of $" + money_out
+ ", balance = $" + money);
}
else
{
System.out.println ("Insuffient funds to withdraw $"
+ money_out + ", balance = $" + money);
}
System.out.println();
//to test to make sure new accounts are numbered correctly
CheckingAccount yourCheckingAccount =
new CheckingAccount ("Issac Newton", 5000);
System.out.println ("Account Number "
+ yourCheckingAccount.getAccountNumber()
+ " belonging to "
+ yourCheckingAccount.getOwner());
}
}
BankAccount(Parent class)
//Defines any type of bank account
public abstract class BankAccount
{
// class variable so that each account has a unique number
protected static int numberOfAccounts = 100001;
// current balance in the account
private double balance;
// name on the account
private String owner;
// number bank uses to identify account
private String accountNumber;
//default constructor
public BankAccount()
{
balance = 0;
accountNumber = numberOfAccounts + "";
numberOfAccounts++;
}
//standard constructor
public BankAccount(String name, double amount)
{
owner = name;
balance = amount;
accountNumber = numberOfAccounts + "";
numberOfAccounts++;
}
//copy constructor
public BankAccount(BankAccount oldAccount, double amount)
{
owner = oldAccount.owner;
balance = amount;
accountNumber = oldAccount.accountNumber;
}
//allows you to add money to the account
public void deposit(double amount)
{
balance = balance + amount;
}
//allows you to remove money from the account if
//enough money is available
//returns true if the transaction was completed
//returns false if the there was not enough money
public boolean withdraw(double amount)
{
boolean completed = true;
if (amount <= balance)
{
balance = balance - amount;
}
else
{
completed = false;
}
return completed;
}
//accessor method to balance
public double getBalance()
{
return balance;
}
//accessor method to owner
public String getOwner()
{
return owner;
}
//accessor method to account number
public String getAccountNumber()
{
return accountNumber;
}
//mutator method to change the balance
public void setBalance(double newBalance)
{
balance = newBalance;
}
//mutator method to change the account number
public void setAccountNumber(String newAccountNumber)
{
accountNumber = newAccountNumber;
}
}
CheckingAccount
public class CheckingAccount extends BankAccount {
private double fee = .15;
private String accountNumber = getAccountNumber() + "-10";
public CheckingAccount(String name, double amount)
{
super(name, amount);
setAccountNumber(accountNumber);
}
public boolean withdraw(double amount)
{
double finalAmount = amount + fee;
super.withdraw(finalAmount);
boolean completed = true;
return completed;
}
}
And finally, SavingsAccount
public class SavingsAccount extends BankAccount{
double rate = .025;
static int savingsNumber = 0;
private String accountNumber = super.getAccountNumber();
//Default constructor
public SavingsAccount(String name, double amount){
super(name, amount);
accountNumber = accountNumber + "-" + Integer.toString(savingsNumber);
}
//Copy constructor
public SavingsAccount(SavingsAccount oldAccount, double amount)
{
savingsNumber++;
accountNumber = oldAccount.accountNumber + Integer.toString(savingsNumber);
owner = oldAccount.getOwner();
balance = oldAccount.getBalance();
}
public void postInterest(){
double interestAmount = getBalance() * .025/12;
double finalAmount = getBalance() + interestAmount;
setBalance(finalAmount);
}
public String getAccountNumber()
{
return accountNumber;
}
}
The problem I am dealing with is that I can't figure out how to make certain values in my class appear which they only return as zeros. I'm a beginner so I don't know much about Java but I have to make 4 classes or 3 types of employees and one class to output their values. But I can't get the values for the commission and union employ to show. Here is the code:
import java.util.Scanner;
// Base or Superclass
class Employee
{
String name;
String department;
double pay;
double hours;
double money;
double mission;
double rate;
double withheld;
double moneyc;
double moneyu;
double sales;
public void inputs()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter your name: ");
name = in.nextLine();
System.out.println("Enter your department: ");
department = in.nextLine();
System.out.println("Enter your pay per hour: ");
pay = in.nextDouble();
System.out.println("Enter how many hours you worked: ");
hours = in.nextDouble();
System.out.println("Enter your rate of commission(0.00): ");
rate = in.nextDouble();
System.out.println("Enter your withheld amount: ");
withheld = in.nextDouble();
System.out.println("Enter your sales amount: ");
sales = in.nextDouble();
money = pay * hours;
}
// Accessor Methods
public String getName()
{
return this.name;
}
public String getDepartment()
{
return this.department;
}
public Double getPay()
{
return this.pay;
}
public Double getHours()
{
return this.hours;
}
public Double getMoney()
{
return this.money;
}
public Double getMission()
{
return this.mission;
}
public Double getRate()
{
return this.rate;
}
public Double getWithheld()
{
return this.withheld;
}
public Double getMoneyc()
{
return this.moneyc;
}
public Double getMoneyu()
{
return this.moneyu;
}
public Double getSales()
{
return this.sales;
}
// Mutator Methods
public void setName(String n)
{
name = n;
}
public void setDepartment(String d)
{
department = d;
}
public void setPay(double p)
{
pay = p;
}
public void setHours(double h)
{
hours = h;
}
public void setMoney(double m)
{
money = m;
}
public void setMission(double mi)
{
mission = mi;
}
public void setRate(double r)
{
rate = r;
}
public void setWithheld(double w)
{
withheld = w;
}
public void setMoneyc(double mc)
{
moneyc = mc;
}
public void setMoneyu(double mu)
{
moneyu = mu;
}
public void setSales(double s)
{
sales = s;
}
}
class Last extends Employee
{
public void dinero()
{
Employee one = new Employee();
one.inputs();
// Union Employee
UnionEmployee three = new UnionEmployee();
three.Syndicate();
// Commission Employee
Commissioned two = new Commissioned();
two.sales();
System.out.println("\n"+ "Name: "+ one.getName());
System.out.println( "Department: "+ one.getDepartment());
System.out.println( "Hours: "+ one.getHours());
System.out.println( "Pay Rate: "+ one.getPay());
System.out.println("Your money is: "+ one.getMoney());
// Commissioned Employee
System.out.println( "\n"+ "Commissioned Employee");
System.out.println("Your money is: "+ one.getMoneyc());
System.out.println( "Your commission is: "+ one.getMission());
System.out.println("Your rate: "+ one.getRate());
// Union employee
System.out.println("\n"+"Union Employee");
System.out.println("Your money is: "+ one.getMoneyu());
System.out.println( "Your withheld is: "+ one.getWithheld());
}
}
// Derived or Subclass
class Commissioned extends Employee
{
public void sales()
{
moneyc = hours * pay;
// Commission
mission = sales * rate;
}
}
// Derived or Subclass
class UnionEmployee extends Employee
{
public void Syndicate()
{
if (hours <= 40) {
moneyu = (hours * pay) - withheld;
} else {
moneyu = (pay * hours * ((hours - 40) * 1.5)) - withheld;
}
}
}
public class WeiTry extends Last
{
public static void main(String[] args)
{
// Output
Last show = new Last();
show.dinero();
}
}
The only place I see your Employee's fields get set is inside a method called inputs()
one's values get populated because you call the inputs method on one but for your other employee types like your UnionEmployee three, inputs never called, and their fields never get set.
If you want your other employee's values to get set, it looks like you'll have to call their inputs method.
UnionEmployee three = new UnionEmployee();
three.inputs();
three.Syndicate();
or you can just copy them over
three.setName(one.getName());
This is for addnew account.
private void btnSaveAActionPerformed(java.awt.event.ActionEvent evt)
{
BankAccount account = new BankAccount();
ButtonGroup bg = new ButtonGroup();
bg.add(rad_savings);
bg.add(rad_checking);
account.setAccountName(txt_accountname.getText());
account.setAccountNo(txt_accountnumber.getText());
account.setBalance(Double.parseDouble(txt_initialbalance.getText()));
list.add(account);
if(rad_savings.isSelected()){
//account = new SavingsAccount();
account.setAccountType("Savings");
list.add(account);
}
else
{
//account = new CheckingAccount();
account.setAccountType("Checking");
list.add(account);
}
}
I also have, Savings and Checkings Class
Savings:
public class SavingsAccount extends BankAccount {
public SavingsAccount(){
};
public SavingsAccount(String accountNo, String accountName, double initBalance) {
super(accountNo, accountName, initBalance);
}
public SavingsAccount(String accountNo, String accountName) {
super(accountNo, accountName);
}
}
Checking:
public class CheckingAccount extends BankAccount {
private double overdraftProtection;
public CheckingAccount(){
};
public CheckingAccount(String accountNo, String accountName,
double initBalance) {
super(accountNo, accountName, initBalance);
}
public CheckingAccount(String accountNo, String accountName) {
super(accountNo, accountName);
}
public double getOverdraftProtection() {
return overdraftProtection;
}
public void setOverdraftProtection(double overdraftProtection) {
this.overdraftProtection = overdraftProtection;
}
public void withdraw(double amount) {
// TODO: code for withdrawal
}
}
In my BankAccount Class, i have this:
public class BankAccount {
private String accountNo;
private String accountName;
protected double balance;
private String accountType;
public String toString(){
return "Account name: " + accountName + "" + System.getProperty("line.separator") +
"Account Number: " + accountNo + "" +System.getProperty("line.separator")+
"Balance: " + balance + "" + System.getProperty("line.separator")+
"Account Type: " + accountType;
}
When I'm trying it, it replicates the set the of data:
For example: I've entered Account Name: John, Account No: 101, Initial Balance: 500, Account Type: Savings.
It will output like this:
Account Name: John
Account No: 101
Initial Balance: 500
Account Type: Savings
Account Name: John
Account No: 101
Initial Balance: 500
Account Type: Savings
I can't find what is wrong.
In btnSaveAActionPerformed you call list.add(account); and then check whether the account is a checking or savings account. In the if statements of both of those you do another list.add(account);
This is causing the double addition of the account.
**code is the cause of your problem as you are adding the account object twice in the list.
**list.add(account);**
if(rad_savings.isSelected()){
//account = new SavingsAccount();
account.setAccountType("Savings");
**list.add(account);**
}
else
{
//account = new CheckingAccount();
account.setAccountType("Checking");
**list.add(account);**
}
Update your code like this:
if(rad_savings.isSelected()){
//account = new SavingsAccount();
account.setAccountType("Savings");
}
else
{
//account = new CheckingAccount();
account.setAccountType("Checking");
}
list.add(account);