Linking Credit Card and Debit Card classes to a Bank Account Class - java

I'm learning Java, and one of my assignments is to create Credit and Debit Cards classes so that you can create new cards linked to a new individual account when one is created. The credit card should store purchases, and when the user makes a payment (total or partial), substract the amount from the account (plus 3% interest). The debit card immediately substracts the purchase's amount from the account.
I have written everything and made it work, but only as nested classes within the Account class. Is there a way to have Account, Credit and Debit as three separate classes and make it so that every time that you create a new account, you can optionally create cards linked to each new individual account?
Disclaimer: I'm trimming some of the code because it's all in spanish so I don't have to translate it all (mainly booleans to check for positive amounts and stuff like that), but the code is still functional. Thanks a lot in advance!
package accounts;
public class Account {
protected static double balance;
protected static String accountNumber;
public Account() {
}
public Account(String accountNumber, double balance) {
Account.accountNumber = accountNumber;
Account.balance = balance;
}
public double getBalance() {
return balance;
}
public String getAccountNumber() {
return accountNumber;
}
public void deposit(double depositAmount) {
Account.balance += depositAmount;
}
public void extraction(double extractionAmount) {
Account.balance -= extractionAmount;
}
public void showBalance() {
System.out.println("Your current balance is: " + getBalance());
}
protected static class DebitCard {
private String userName;
private int cardNumber;
public DebitCard () {
}
public DebitCard (String userName, int cardNumber) {
this.userName = userName;
this.cardNumber = cardNumber;
}
public String getUserName() {
return userName;
}
public int getCardNumber() {
return cardNumber;
}
public void debitPurchase(double purchaseAmount) {
Account.balance -= purchaseAmount;
}
}
protected static class CreditCard {
private String userName;
private int cardNumber;
private double creditCardDebt;
private double limit;
public CreditCard () {
}
public CreditCard(String userName, int cardNumber, double limit) {
this.userName = userName;
this.cardNumber = cardNumber;
this.limit = limit;
}
public String getUserName() {
return userName;
}
public int getCardNumber() {
return cardNumber;
}
public double getLimit() {
return limit;
}
public double getCrediCardDebt() {
return creditCardDebt;
}
public void creditCardPurchase(double purchaseAmount) {
if (this.creditCardDebt + purchaseAmount > this.limit) {
Error notEnoughLimit = new Error("There's not enough limit to make this purchase");
throw notEnoughLimit ;
} else {
this.creditCardDebt += purchaseAmount + (purchaseAmount * 0.03);
this.limit -= purchaseAmount;
this.creditCardDebt += purchaseAmount + (purchaseAmount* 0.03);
this.limit -= purchaseAmount;
}
}
public void payCard(double payAmount) {
Account.balance -= payAmount;
this.creditCardDebt = this.creditCardDebt - payAmount;
}
}
}

Yes it is possible. But before you do that, you should solve the problem caused by declaring balance and accountNumber as static variables. By doing that, you have made every Account instance share one account number and one balance.
Those variables should also be private so that other classes can't access or change them directly.
Once you have fixed that, you should then change the constructors for Credit and Debit to take an instance of Account as a parameter. They store this in a private field, and then perform operations to add and remove money from the Account ... via operations on the Account object.

Related

I want to print all the account numbers and account names that I have created so far ; I am using java

I have the Accounts Java class as follows:
public class Accounts {
These are the fields
private int accountNumber;
private String accountName;
private double accountBalance;
public Accounts(int accountNumber, String accountName, double accountBalance) {
this.accountNumber = accountNumber;
this.accountName = accountName;
this.accountBalance = accountBalance;
}
public int getAccountNumber() {
return accountNumber;
}
public String getAccountName() {
return accountName;
}
public double getAccountBalance() {
return accountBalance;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public void setAccountBalance(double accountBalance) {
this.accountBalance = accountBalance;
}
}
public class AccountController {
ArrayList<Accounts> accounts = new ArrayList<>();
public boolean addAccount(int accountNumber, String accountName, double accountBalance) {
Accounts acc = new Accounts(accountNumber, accountName, accountBalance);
if (findAccount(accountNumber) == null) {
accounts.add(acc);
return true;
}
return false;
}
These are the classes I have:
1: Accounts
2: Account Controller
3: MyMainView
I want to print the list of the Accounts I add to the ArrayList along which contains accountNumber , accountBalance and accountName. I want to add the create the method inside the MyMainView and call it inside the main method of the MyMainView.
You could try overriding the toString() method in your Accounts class.
Next, create a method in Account Controller that iterates over all instances of Accounts, calling their respective toString() method and collecting the outputs.
Now you can call that method in the main in MyMainView.

Simple OOPS demo, stuck with the function of for loop and its role in the main method

The only information about this code is this CreditCard class defines
credit card objects that model a simplified version of traditional credit cards. They
store information about the customer, issuing bank, account identifier, credit limit,
and current balance. They do not charge interest or late payments, but they do
restrict charges that would cause a card’s balance to go over its credit limit.
The following code is from the book Data Structures and Algorithms in java:
Please help me regarding the for loop which is:
for (int val = 1; val <= 16; val++) {
wallet[0].charge(3*val);
wallet[1].charge(2*val);
wallet[2].charge(val);
}
Why the charge method has parameters 3val, 2val and val. What this loop really does?
From the following code:
package com.illustrations;
public class CreditCard{
private String customer;
private String bank;
private String account;
private int limit;
protected double balance;
public CreditCard(String customer, String bank, String account, int limit, double balance){
this.customer = customer;
this.bank = bank;
this.account = account;
this.limit = limit;
this.balance = balance;
}
public CreditCard(String customer, String bank, String account, int limit) {
this(customer, bank, account, limit, 0.0);
}
public String getCustomer(){ return customer; }
public String getBank() { return bank; }
public String getAccount() { return account; }
public int getLimit() { return limit; }
public double getBalance() { return balance; }
public boolean charge(double price) {
if(price + balance > limit)
return false;
balance = balance + price; return true;
}
public void makePayment(double amount) {
balance -=amount;
}
public static void printSummary(CreditCard card) {
System.out.println("Customer ="+card.customer);
System.out.println("Bank ="+card.bank);
System.out.println("Account ="+card.account);
System.out.println("Balance ="+card.balance);
System.out.println("Limit ="+card.limit);
}
public static void main(String[] args) {
CreditCard[ ] wallet = new CreditCard[3];
wallet[0] = new CreditCard("John Bowman", "California Savings", "5391 0375 9387 5309", 5000);
wallet[1] = new CreditCard("John Bowman", "California Federal", "3485 0399 3395 1954", 3500);
wallet[2] = new CreditCard("John Bowman", "California Finance", "5391 0375 9387 5309", 2500, 300);
for (int val = 1; val <= 16; val++) {
wallet[0].charge(3*val);
wallet[1].charge(2*val);
wallet[2].charge(val);
}
for (CreditCard card : wallet) {
CreditCard.printSummary(card); // calling static method
while (card.getBalance() > 200.0) {
card.makePayment(200);
System.out.println("New balance = " +card.getBalance());
}
}
}
}
I don't really see much significance. Actually, there are 3 kinds of wallets defined - Savings, Federal, and Finance with Savings having the largest credit limit of the remaining two. So what we did here in the for loop with 3*val is keep putting in money in the Savings wallet thrice as fast as the Finance one because its limit is much higher.
Once the limit has been reached, we don't allow more money to be added in the wallets.

How do I use the transferTo() method to transfer money between accounts?

I am supposed to Add the following method to allow BankAccounts to transfer money to another BankAccount. Where would I add this method to make sure it works for all BankAccounts, including SavingsAccount and CheckingAccount?
The method im supposed to use is transferTo(BankAccount destinationAccount, int transferAmount)
public class BankAccount {
private String accountHolderName;
private String accountNumber;
private int balance;
public BankAccount(String accountHolder, String accountNumber) {
this.accountHolderName = accountHolder;
this.accountNumber = accountNumber;
this.balance = 0;
}
public BankAccount(String accountHolder, String accountNumber, int balance) {
this.accountHolderName = accountHolder;
this.accountNumber = accountNumber;
this.balance = balance;
}
public String getAccountHolderName() {
return accountHolderName;
}
public String getAccountNumber() {
return accountNumber;
}
public int getBalance() {
return balance;
}
// Update the balance by using the DollarAmount.Plus method
public int deposit(int amountToDeposit) {
balance = balance + amountToDeposit;
return balance;
}
// Update the balance by using the DollarAmount.Minus method
public int withdraw(int amountToWithdraw) {
balance = balance - amountToWithdraw;
return balance;
}
public int transferTo(BankAccount destinationAccount, int transferAmount) {
return balance;
}
}
Assumptions:
This is only an exercise and not a real application for a bank.
SavingsAccount and CheckingAccount are subclasses of BankAccount
The method transferTo can be implemented like the following:
public int transferTo(BankAccount destinationAccount, int transferAmount) {
this.balance -= transferAmount;
destinationAccount.deposit(transferAmount);
return balance;
}
In a real world application you need to ensure that this operation will be always atomic and thread-safe. Additionally, using int for the balance is strongly not recommended.

Using One Object in Multiple Classes

I'm trying to use a setter/getter class in multiple classes to modify a single variable. I understand that I have to use the same object in order for this to work, but I'm unsure on how to make a single object accessible to multiple classes.
public class accountbalance {
private double balance;
//getter
public double getBalance() {
return balance;
}
//Setter
public void setBalance(double newBalance) {
this.balance = newBalance;
}
}
This is where I'm trying to use it first:
public class CreateAccount {
String name;
double social;
int pin;
public CreateAccount()
{
name = "null";
social = 0;
pin = 0;
}
public CreateAccount(String enteredName, double enteredSocial,int enteredPin, double value)
{
name = enteredName;
social = enteredSocial;
pin = enteredPin;
double accountnum = 87495;
accountbalance a1 = new accountbalance();
a1.setBalance(value);
System.out.println(name);
System.out.println(social);
System.out.println("Your new PIN is " + pin);
System.out.println("Your new account balance is " + (a1.getBalance()));
}
}
And then I'm trying to use it again here:
public class deposit {
double enteredAmt;
double amt;
public void deposit() {
System.out.println("Enter an amount to desposit: ");
Scanner in = new Scanner(System.in);
enteredAmt = in.nextDouble();
accountbalance ab1 = new accountbalance();
System.out.println("current balence: " + ab1.getBalance());
amt = ab1.getBalance() + enteredAmt;
ab1.setBalance(amt);
System.out.println("Your new balance is " + (ab1.getBalance()));
}
}
I believe what you're trying to do is use the Command Design Pattern.
If you changed your classes to look more like this, things might work a tad bit better.
public class Account {
// Please do not use SS for an identifier.
private String identifier;
private String name;
// Money needs to be stored in it's lowest common denominator.
// Which in the united states, is pennies.
private long balance;
public Account(String identifier, String name) {
this.identifier = identifier;
this.name = name;
this.balance = 0L;
}
public String getIdentifier() {
return this.identifier;
}
public String getName() {
return this.name;
}
public long getBalance() {
return balance;
}
public void credit(long amount) {
balance =+ amount;
}
public void debit(long amount) {
balance =- amount;
}
}
Then here would be your CreateAccountCommand. It is a bit anemic, but that is ok.
public class CreateAccountCommand {
private String identifier;
private String name;
public CreateAccount(String identifier, String name) {
// Identifier sould actually be generated by something else.
this.identifier = identifier;
name = name;
}
public Account execute() {
return new Account(identifier, name);
}
}
Here is your DepositCommand:
public class DepositCommand {
private Account account;
private long amount;
public DepositCommand(Account account, long amount) {
this.account = account;
this.amount = amount;
}
public void execute() {
this.account.credit(amount);
}
}
WithdrawCommand:
public class WithdrawCommand {
private Account account;
private long amount;
public DepositCommand(Account account, long amount) {
this.account = account;
this.amount = amount;
}
public void execute() {
this.account.debit(amount);
}
}
Then run everything:
public class Run {
public static void main(String... args) {
CreateAccountCommand createAccountCommand = new CreateAccountCommand("12345678", "First_Name Last_Name");
Account account = createAccountCommand.execute();
System.out.println(account.getIdentifier());
System.out.println(account.getName());
System.out.println("Your new account balance in pennies: " + account.getBalance());
// Deposit $100.00
DepositCommand depositCommand = new DepositCommand(account, 10000);
depositCommand.execute();
System.out.println(account.getIdentifier());
System.out.println(account.getName());
System.out.println("Your new account balance in pennies: " + account.getBalance());
// Withdraw $75.00
WithdrawCommand withdrawCommand = new WithdrawCommand(account, 7500);
withdrawCommand.execute();
System.out.println(account.getIdentifier());
System.out.println(account.getName());
System.out.println("Your new account balance in pennies: " + account.getBalance());
}
}

Java compiling error on Eclipse

So I'm working on this program and I've created two classes, one class called CreditCard, the other called CreditCardTester.I'm using Eclipse IDE and I keep getting compiling errors such as "The method getBalance(double) in the type CreditCard is not applicable for the arguments ()". I'm not really sure on what I have to fix.
This is the first class called CreditCard:
public class CreditCard
{
private String accountNumber;
private double creditLimit;
private double balance;
public void CreditCard(String number, double limit)
{
accountNumber = number;
limit = creditLimit;
balance = 0;
}
public String getAccountNumber()
{
return accountNumber;
}
public double getCreditLimit()
{
return creditLimit;
}
public double getBalance(double theBalance)
{
return balance;
}
public double charge(double amount)
{
balance = balance + amount;
return balance;
}
This is the second class CreditCardTester:
public class CreditCardTester
{
public static void main(String[] args)
{
CreditCard card = CreditCard("1234-5678-9012-3456", 1000.00);
String formatString = "Credit Card [number = %s, bal = %.2f, limit = %.2f]\n";
System.out.printf(formatString, card.getAccountNumber(), card.getBalance(), card.getCreditLimit());
System.out.println("\nCharging $50.00 to credit card...\n");
card.charge(50.00);
System.out.printf(formatString, card.getAccountNumber(), card.getBalance(), card.getCreditLimit());
}
This is not a constructor, because you added void, making it a normal method:
public void CreditCard(String number, double limit)
Remove void:
public CreditCard(String number, double limit)
Also, one of the assignments in the method/constructor is backwards. You assigned the instance variable to the parameter.
limit = creditLimit;
Change it around:
creditLimit = limit;
You're missing "new" when creating a CreditCard:
CreditCard card = CreditCard("1234-5678-9012-3456", 1000.00);
Try
CreditCard card = new CreditCard("1234-5678-9012-3456", 1000.00);
You have an unused parameter on the getBalance method, and you call it without a parameter.
public double getBalance(double theBalance)
Remove it:
public double getBalance()
I think getBalance should not accept an argument in your class definition.
Use this in your CreditCard class:
public double getBalance()
{
return balance;
}

Categories