unable to access account number - java

import java.text.DecimalFormat;
import java.util.ArrayList;
public class AccountOwner {
private Account account;
private String firstname;
private String lastname;
private String address;
private double currentBalance;
private ArrayList<Integer> withdrawAmount = new ArrayList<Integer>(5);
private ArrayList<Double> deposits = new ArrayList<Double>();
private ArrayList<Double> purchases = new ArrayList<Double>(5);
private DecimalFormat formatter = new DecimalFormat("##0.00");
public AccountOwner(String firstname, String lastname, String address) {
this.firstname = firstname;
this.lastname = lastname;
this.address = address;
}
public String getFirstName() {
return firstname;
}
public String getLatName() {
return lastname;
}
public String getAddress() {
return address;
}
public String checkBalance() {
for (Double deposit : deposits) {
this.currentBalance += deposit;
}
return formatter.format(currentBalance);
}
public void makeDeposit(double amount) {
deposits.add(amount);
}
public void viewAllDeposits() {
double allDeposits = 0.0;
System.out.println("All deposits till today:");
for (int i = 0; i < deposits.size(); i++) {
allDeposits = deposits.get(i);
System.out.print("\t"+"$"+allDeposits);
}
System.out.println();
}
public void withdrawMoney(int amount) {
withdrawAmount.add(amount);
currentBalance -= amount;
}
public String getActualBalance() {
return formatter.format(currentBalance);
}
public void withdrawAmounts(){
System.out.println("All Withdrawls till today");
for(int i = 0; i < withdrawAmount.size(); i++){
System.out.print("\t"+"$"+withdrawAmount.get(i));
}
System.out.println();
}
public void makePurchase(double itemPrice){
purchases.add(itemPrice);
this.currentBalance -= itemPrice;
}
public void viewAllmadePurchases() {
double purchase = 0.0;
System.out.println("All purchases made till today:");
for (int i = 0; i < purchases.size(); i++) {
purchase = purchases.get(i);
System.out.print("\t"+"$"+purchase);
}
}
public void viewMyPersonalInformation(){
System.out.println("Firstname:" + this.firstname);
System.out.println("LastName:" +this.lastname);
System.out.println("Address:" +this.address);
System.out.println("Balance:" +this.checkBalance());
viewAllDeposits();
withdrawAmounts();
viewAllmadePurchases();
}
public class Account {
private AccountOwner customer;
private int accountNumber;
public Account(){
customer = null;
accountNumber = 0000000;
}
public Account(int accountNumber, AccountOwner owner){
this.accountNumber = accountNumber;
customer = owner;
}
public int accountNumberIs(){
return accountNumber;
}
public class BankManager {
private Account account;
private AccountOwner accountOwner;
private int accountNumber;
public void createNewAccount(int accountNumber, AccountOwner owner){
account = new Account(accountNumber, owner);
this.accountNumber = accountNumber;
this.accountOwner = owner;
}
public int getaccountNumber(){
return accountNumber;
}
public void setAccountNumber(int newaccountnumber){
accountNumber = newaccountnumber;
}
public void customerInformation(){
accountOwner.viewMyPersonalInformation();
}
public class MainProgram {
/**
* #param args
*/
public static void main(String[] args) {
BankManager manager = new BankManager();
AccountOwner owner = new AccountOwner("Tom", "Smith", "208 W 119th St");
manager.createNewAccount(389745, owner);
Account acc = new Account();
owner.makeDeposit(550);
owner.makeDeposit(700);
owner.makeDeposit(122.93);
owner.makeDeposit(195.93);
owner.withdrawMoney(200);
owner.makePurchase(200);
owner.makeDeposit(100);
owner.makePurchase(1220);
owner.withdrawMoney(30);
owner.viewMyPersonalInformation();
System.out.println();
System.out.println();
System.out.println();
System.out.println(acc.accountNumberIs());
The problem that i have is i'm trying to access the account number from the accountowner without involving a reference to the bankmanager class.. how can i get it to work. I have
been trying to using the account class itself cause i created a constructor and assign those
paramaters to the fields in the account class but seems not to work

This code contains a bug :
public Account(int accountNumber, AccountOwner owner){
AccountOwner cstomer = owner;
int acctNumber = accountNumber;
accountNumber = acctNumber;
//System.out.println(accountNumber);
}
The accountNumber parameter you're passing into your constructor is taking precedence over your class's accountNumber field. your Account's accountNumber field is never actually getting set.
This is equivalent to :
public Account(int accountNumber, AccountOwner owner){
AccountOwner cstomer = owner;
accountNumber = accountNumber;
}
to make sure that you're accessing the field use the this keyword as in: this.accountNumber
public Account(int accountNumber, AccountOwner owner){
AccountOwner cstomer = owner;
this.accountNumber = accountNumber;
//System.out.println(accountNumber);
}
There is another bug, the Account.customer is not assigned :
public Account(int accountNumber, AccountOwner owner){
customer = owner;
this.accountNumber = accountNumber;
}
I recommend you to learn to use a debugger or still better write unit tests (first).

class AccountOwner {
//...
private Account account;
//...
public Integer getAccountNumber() {
return this.account != null ? this.account.accountNumberIs() : null;
}
}

You have set your accountNumber type to be int and set it to 0000000 (which is equal to just 0 for an int type) therefore you are getting 0 when you call getAccountNumber() method.
Instead, change accountNumber type to String and initialize it to '0000000'. You will see 0000000 being printed then.

Related

How do I assign object from one class to another class and how do I calculate double and int inside an ArrayList?

I have an assignment but i have problem trying to do some part of it. Appreciate if anyone can give me a hand.
Assignment brief: https://pastebin.com/3PiqvfTE
Main Method: https://pastebin.com/J2kFzB3B
import java.util.ArrayList;
import java.util.Scanner;
public class mainMethod {
public static void main(String[] args) {
//scanner
Scanner scanner = new Scanner (System.in);
//subject object
Subject subject = new Subject (0,null,0);
System.out.println("How many subject do you want to enter: ");
int subjectNumber = scanner.nextInt();
for (int j = 0; j < subjectNumber; j++) {
//subject ID
System.out.println("Please enter the subject ID: ");
int subID = scanner.nextInt();
//subject Name
System.out.println("Please enter subject name: ");
String subName = scanner.next();
//subject fee
System.out.println("Please enter subject fee: ");
double subFee = scanner.nextDouble();
//add subject
subject.addSubject(subID, subName, subFee);
}
//display subject
System.out.println(subject.getSubject());
/*
//loop for part time teacher
System.out.println("Please enter how many part time teacher do you have: ");
int PTcounter = scanner.nextInt();
for (int i = 0; i < PTcounter; i++) {
*/
Teacher teach = new Teacher (0,null,null);
//teacher employee ID
System.out.println ("Please enter the teacher employee ID: ");
int tid = scanner.nextInt();
//teacher name
System.out.println("Please enter the teacher name: ");
String tname = scanner.next();
//teacher gender
System.out.println("Please enter the teacher gender: ");
String tgender = scanner.next();
//add teacher details
teach.addTeacher(tid, tname, tgender);
//display teacher details
System.out.println(teach.getTeacher());
//call address class
Address address = new Address (0,null,null,0);
//address house number
System.out.println("Please enter house number: ");
int addyNum = scanner.nextInt();
//address street name
System.out.println("Please enter street name: ");
String StreetName = scanner.next();
//address city
System.out.println("Please enter city: ");
String City = scanner.next();
//address post code
System.out.println("Please enter postcode: ");
int Postcode = scanner.nextInt();
//add address
address.addAddress(addyNum, StreetName, City, Postcode);
//display address
System.out.println(address.getAddress());
//call Part Time Salary class
PartTimeSalary ptSalary = new PartTimeSalary(0,0);
//max hours
System.out.println("Please enter maximum work hours: ");
int maxHours = scanner.nextInt();
//hourly rate
System.out.println("Please enter hourly rate: ");
double hourlyRate = scanner.nextDouble();
ptSalary.addPTSalary(maxHours, hourlyRate);
System.out.println(ptSalary.getHourlyRate());
//System.out.printf("Teacher details is %s, Subject details is %s, Address is %s", teach.toString(),subject.toString(),address.toString());
}
}
1st problem. I have a subject class and a teacher class. Teacher will be able to teach maximum of 4 subject. I have prompt user to enter subject details before entering teacher details, so when user enter teacher details they will be able to assign subject to teacher just by using the subjectID. I have problem implementing this. My subject is already store in an ArrayList but how to I connect this with teacher. And in the end the program will display what subject each teacher teaches.
Subject Class: https://pastebin.com/iBYFqYDN
import java.util.ArrayList;
import java.util.Arrays;
public class Subject {
private int subjectID;
private String subjectName;
private double fee;
private ArrayList <Subject> subjectList = new ArrayList <Subject>();
public Subject(int subjectID, String subjectName, double fee) {
this.subjectID = subjectID;
this.subjectName = subjectName;
this.fee = fee;
}
public int getSubjectID () {
return subjectID;
}
public void setSubjectID (int subjectID) {
this.subjectID = subjectID;
}
public String getSubjectName () {
return subjectName;
}
public void setSubjectName (String subjectName) {
this.subjectName = subjectName;
}
public double getFee () {
return fee;
}
public void setFee (double fee) {
this.fee = fee;
}
#Override
public String toString() {
return "[subjectID=" + subjectID + ", subjectName=" + subjectName + ", fee=" + fee + "]";
}
public void addSubject (int subjectID, String subjectName, double fee) {
subjectList.add(new Subject(subjectID, subjectName, fee) );
}
public String getSubject () {
return Arrays.toString(subjectList.toArray());
}
}
Teacher class: https://pastebin.com/Np7xUry2
import java.util.ArrayList;
import java.util.Arrays;
public class Teacher {
private int employeeID;
private String name;
private String gender;
private ArrayList <Teacher> teacherDetailsList;
public Teacher (int employeeID, String name, String gender) {
this.employeeID = employeeID;
this.name = name;
this.gender = gender;
this.teacherDetailsList = new ArrayList <Teacher>();
}
public int getEmployeeID () {
return employeeID;
}
public void setEmployeeID (int employeeID) {
this.employeeID = employeeID;
}
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
public String getGender () {
return gender;
}
public void setGender (String gender) {
this.gender = gender;
}
#Override
public String toString() {
return "[employeeID=" + employeeID + ", name=" + name + ", gender=" + gender + "]";
}
public void addTeacher (int employeeID, String name, String gender) {
teacherDetailsList.add(new Teacher (employeeID,name,gender));
}
public String getTeacher () {
return Arrays.toString(teacherDetailsList.toArray());
}
}
2nd problem. Teacher will either be part time or full time teacher. Part time teacher will have a maximum work hour they can work and an hourly rate they will be paid for, so the final salary of part time teacher will be "maximum hours" multiply by "hourly rate". I have store "hourly rate" and "maximum work hours" in an ArrayList but how do I call them to make the multiplication then displaying at the end.
Part time salary class: https://pastebin.com/iGKpu87Y
import java.util.ArrayList;
public class PartTimeSalary {
private int maxHour;
private double hourlyRate;
private double hourlySalary;
private ArrayList <PartTimeSalary> PTSalary = new ArrayList <PartTimeSalary>();
private ArrayList <Double> finalHourlySalary = new ArrayList <Double>();
public PartTimeSalary (int maxHour, double hourlyRate) {
this.maxHour = maxHour;
this.hourlyRate = hourlyRate;
}
public int getMaxHours () {
return maxHour;
}
public void setMaxHour (int maxHour) {
this.maxHour = maxHour;
}
public double getHourlyRate () {
return hourlyRate;
}
public void setHourlyRate (double hourlyRate) {
this.hourlyRate = hourlyRate;
}
public double getHourlySalary() {
hourlySalary = hourlyRate*maxHour;
return hourlySalary;
}
public void addPTSalary (int maxHour, double hourlyRate) {
PTSalary.add(new PartTimeSalary(maxHour,hourlyRate));
}
public void FinalHourlySalary (double hourlySalary) {
hourlySalary = hourlyRate * maxHour;
finalHourlySalary.add(hourlySalary);
}
public ArrayList<Double> getFinalSalary () {
return (new ArrayList <Double>());
}
}
3rd question. I have an address class which is suppose to be part of the Teacher class. I can't seem to connect the address class with teacher class.
Address class: https://pastebin.com/s2HN5p80
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Address {
private int houseNum;
private String streetName;
private String city;
private int postcode;
private List <Address> address = new ArrayList <Address>();
public Address (int houseNum, String streetName, String city, int postcode) {
this.houseNum = houseNum;
this.streetName = streetName;
this.city = city;
this.postcode = postcode;
}
public int getHouseNum() {
return houseNum;
}
public void setHouseNum (int houseNum) {
this.houseNum = houseNum;
}
public String getStreetName() {
return streetName;
}
public void setStreetName (String streetName) {
this.streetName = streetName;
}
public String getCity() {
return city;
}
public void setCity (String city) {
this.city = city;
}
public int getPostcode() {
return postcode;
}
public void setPostcode (int postcode) {
this.postcode = postcode;
}
public void addAddress (int houseNum, String streetName, String city, int postcode) {
address.add(new Address (houseNum,streetName,city,postcode));
}
#Override
public String toString() {
return "[houseNum=" + houseNum + ", streetName=" + streetName + ", city=" + city + ", postcode="
+ postcode + "]";
}
public String getAddress () {
return Arrays.toString(address.toArray());
}
}
Thank you 😃😊
Question 1)
Put the "teacherDetailsList" and "subjectList" ArrayLists in the main method, not the consructors. Add the teachers and subjects in main methods, not constructors.
Add a new ArrayList called "mySubjects" as a field for the Teacher Class
Add the following 2 method to the Teacher class:
public Teacher (int employeeID, String name, String gender, ArrayList<Subject> s) {
this.employeeID = employeeID;
this.name = name;
this.gender = gender;
this.mySubjects=s;
}
public Teacher (int employeeID, String name, String gender, Subject s) {
this.employeeID = employeeID;
this.name = name;
this.gender = gender;
this.mySubjects.add(s);
}
public void addSubject(Subject s, boolean b){
if(mySubjects.size()<4)mySubjects.add(s);
else throw OutOfBoundsError;
}
public void removeSubject(Subject s, boolean b){
mySubject.remove(s);
}
public void addSubject(Subject s){
s.changeTeacher(s);
}
public void removeSubject(Subject s){
mySubject.remove(s);
}
Add the following methods & fields to Student class
private Teacher teacher;
public Subject(int subjectID, String subjectName, double fee, Teacher t) {
this.subjectID = subjectID;
this.subjectName = subjectName;
this.fee = fee;
this.setTeacher(t);
}
public void setTeacher(Teacher t){
try{
t.addSubject(this);
teacher=t;
}
catch(OutOfBoundsError e){
System.out.println(t.getName()+" already has a full schedule.");
}
}
public void changeTeacher(Teacher t){
teacher.removeSubject(this);
teacher = t;
t.addSubject(this);
}
Question 2)
make a new double field, constructor parameter (for every constructor in the class), mutator method and accessor mutator called maxHour in the class Subject
add a double field to the teacher class called salary. If the teacher is part-time, set this to 0 in the constructor.
add this method to Teacher class:
public double getSalary(){
if(salary!=0) return salary;
double s=0;
for(Subject i: mySubjects) s+=i.getFee()*i.getMaxHours();
return s;
}
You honestly no longer need the PartTimeSalary class now.
Question 3
make a new Address field, constructor parameter (for every constructor in the class), mutator method and accessor mutator called myAddress in the class Teacher
Don't bother with the ArrayList stuff in your Address Class

How do I Solve this method?

I have 3 Classes:Account,Customer and Main.the Main Class has the main method:
these Classes are the some parts of a programm.
public class Account {
private static ArrayList<Account> allAccounts=new ArrayList<>();
private Bank bank;
private int id;
private int money;
private int remainingDuration;
private int interest;
private Customer customer;
public Account(Bank bank, Customer customer,int id, int money,int duration,int interest) {
this.bank = bank;
this.customer=customer;
this.id=id;
this.money = money;
this.remainingDuration=duration;
this.interest = interest;
allAccounts.add(this);
}
public int getId() {
return id;
}
public Bank getBank() {
return bank;
}
}
public class Customer {
private static ArrayList<Customer> allCustomers=new ArrayList<>();
private String name;
private double moneyInSafe;
private ArrayList<Account> allActiveAccounts;
private int totalNumberOfAccountsCreated;
private int negativeScore;
public Customer(String name, double moneyInSafe) {
this.name=name;
this.moneyInSafe=moneyInSafe;
totalNumberOfAccountsCreated=0;
allCustomers.add(this);
}
public static Customer getCustomerByName(String name){
for (Customer customer:allCustomers){
if(customer.getName().equals(name)){
return customer;
}
}
return null;
}
public String getName() {
return name;
}
public void createNewAccount(Bank bank,int money,int duration,int interest){
totalNumberOfAccountsCreated++;
allActiveAccounts.add(new Account(bank,this, totalNumberOfAccountsCreated, money, duration, interest));
}
public double getMoneyInSafe() {
return moneyInSafe;
}
public void setMoneyInSafe(double moneyInSafe) {
this.moneyInSafe = moneyInSafe;
}
public boolean hasActiveAccountBank(Bank bank){
}
private Account getAccountWithId(int id){
for(Account account:allActiveAccounts){
if(account.getId()==id){
return account;
}
}
return null;
}
}
public class Bank {
private static ArrayList<Bank> allBanks=new ArrayList<>();
private String name;
public Bank(String name) {
this.name = name;
allBanks.add(this);
}
public static Bank getBankWithName(String name){
for (Bank bank:allBanks){
if(bank.getName().equals(name)){
return bank;
}
}
return null;
}
public static boolean isThereBankWithName(String name){
return allBanks.contains(getBankWithName(name));
}
public static int getAccountInterestFromName (String name){
if(name.equals("KOOTAH")){
return 10;
}else if(name.equals("BOLAN")){
return 30;
}else{
return 50;
}
}
public String getName() {
return name;
}
}
So my question is How do I Define the hasActiveAccountBank method in Customer Class to Check Is there any Account with this Account id or not in Main Class.
the part of the Main Class has a matcher that returns Customer's name and the id so they are given.Here is the part:
if (!getCustomerByName(matcher.group(1)).hasActiveAccountBank()) {
System.out.println("Chizi zadi?!");
}
So How do i Write in the hasActiveAccountBank() argument?

payroll system in java

I am implementing a payroll system in which I need to implement a PayrollSystem class to add an employee to an ArrayList of employees and create checks for each of those said employees. I've written up the Employee and Paycheck classes, but I'm having trouble with the PayrollSystem class.
How do I create the addEmployee method? Do I pass an Employee object to it along with the information on that employee or is there another way?
Employee:
package payrollSystem;
public class Employee {
private String firstName;
private String lastName;
private int ID;
private double hourlyWage;
private double hoursWorked;
public Employee(String first, String last, int id, double wage, double hours) {
firstName = first;
lastName = last;
ID = id;
hourlyWage = wage;
hoursWorked = hours;
}
public void setFirstName(String first) {
this.firstName = first;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String last) {
this.lastName = last;
}
public String getLastName() {
return lastName;
}
public void setID(int ID) {
this.ID = ID;
}
public int getID() {
return ID;
}
public void setHourlyWage(double hourlyWage) {
this.hourlyWage = hourlyWage;
}
public double getHourlyWage() {
return hourlyWage;
}
public void setHoursWorked(double hoursWorked) {
this.hoursWorked = hoursWorked;
}
public double getHoursWorked() {
return hoursWorked;
}
public double calcPay(double wage, double hours) {
wage = getHourlyWage();
hours = getHoursWorked();
return wage * hours;
}
}
PayCheck:
package payrollSystem;
public class PayCheck {
private String firstName;
private String lastName;
private int ID;
private double netAmount;
public PayCheck(String first, String last, int id, double wage, double hours) {
firstName = first;
lastName = last;
ID = id;
netAmount = wage * hours;
}
public String toString() {
return "Paycheck issued for " + netAmount + "to " + firstName + ", "+ lastName + ", employee ID " + ID;
}
}
PayrollSystem:
package payrollSystem;
import java.util.List;
import java.util.ArrayList;
public class PayrollSystem {
public List<Employee> employees = new ArrayList<Employee>();
public String companyName;
PayrollSystem(String company) {
companyName = company;
}
void addEmployee(Employee a) {
employees.add(a);
}
void getHoursWorked(double hrs) {
this.a.getHoursWorked();
}
void issueCheck() {
double checkAmount = this.a.calcPay(a.getHoursWorked(), a.getHourlyWage());
PayCheck check = new PayCheck(a.getFirstName(), a.getLastName(), a.getID(), a.getHoursWorked(), a.getHourlyWage());
check.toString();
}
}
You can make an addEmployee() method. As long as somewhere you create the employee, and then pass it into your payRoll class.
Would look like this, assuming you have a List of Employees :
public void addEmployee(Employee employee){
employees.add(employee);
}
In your main you could then just go (assuming myEmployee is an Employee Object):
PayrollSystem payrollSystem = new PayRollSystem();
payrollSystem.addEmployee(new Employee("John", "Smith", 1, wage, hours)); // Way 1
payrollSystem.addEmployee(myEmployee); // Way 2
The Employee Object will contain all the information of the Employee. So when that Object is passed in, all the information will come with it. That allows you to fetch all the Employees from the payrollSystem, or use internal methods to perform actions on them.

NullPointerException with a bank account in Java

I get this Exception when I try to create a new customer in my bank account program:
Exception in thread "main" java.lang.NullPointerException
at bank.program.Customer.addAccount(Customer.java:18)
at bank.program.BankProgram.main(BankProgram.java:24)
Java Result: 1
What does it mean, and what do I need to change to get rid of it?
Bank class:
public class Bank {
String bankName;
private ArrayList<Customer> customers = new ArrayList<Customer>();
Bank(String bankName) {
this.bankName = bankName;
}
public void addCustomer(Customer newCustomer) {
customers.add(newCustomer);
}
}
Account class:
public abstract class Account {
protected double balance = 0;
protected String accountId;
public Account() {} //Defaultkonstruktor
public Account(double bal, String id) { //Konstruktor
if (balance >= 0) {
balance = bal;
}
else {
balance = 0;
}
accountId = id;
}
public abstract void deposit(double amount);
public abstract void withdraw(double amount);
public abstract double getBalance();
public abstract String getAccountId();
public void transfer(double amount, Account account) {
account.deposit(amount);
balance -= amount;
}
}
SavingsAccount class:
public class SavingsAccount extends Account {
private double interest = 2.9;
public SavingsAccount() {
super();
}
public SavingsAccount(double bal, String id) {
super(bal, id);
}
#Override
public void deposit(double amount) {
}
#Override
public void withdraw(double amount) {
}
#Override
public double getBalance() {
}
#Override
public String getAccountId() {
}
}
Customer class:
public class Customer {
private String firstName;
private String lastName;
private String number;
private ArrayList<Account> accounts;
Customer(String firstName, String lastName, String number) {
this.firstName = firstName;
this.lastName = lastName;
this.number = number;
}
public void addAccount(SavingsAccount account) {
accounts.add(account);
}
public ArrayList<Account> getAccounts() {
return accounts;
}
}
Bank program class:
public class BankProgram {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numberOfCustomers = 0;
Bank bank = new Bank("Bank name");
System.out.print("Enter amount to deposit: ");
double amount = input.nextDouble();
System.out.println("Account number: " + numberOfCustomers);
System.out.print("First name: ");
String firstName = input.next();
System.out.print("Last name: ");
String lastName = input.next();
System.out.print("Customer number: ");
String pnumber = input.next();
Customer newCustomer = new Customer(firstName, lastName, pnumber);
SavingsAccount savingsAccount = new SavingsAccount(amount, "11");
newCustomer.addAccount(savingsAccount);
bank.addCustomer(newCustomer);
}
}
You're trying to add an account without initializing the ArrayList in which they're stored. Add this to your Customer constructor.
this.accounts = new ArrayList<Account>();
In your Customer class, you declare an ArrayList accounts. Then, you have a method called addAccount, but it seems as though this accounts was never initialized.
Somewhere where you create your Customer object, you need to say:
newCustomer.accounts = new ArrayList<Account>();

Giving wrong result.What should I do?

I'm trying to calculate a simple banking function.I passed two withdraw value 150 and 47.62 through one method from execution class to another.But it takes 47.62 twice and giving me wrong result here is the execution class.
public class Account {
public double balance ;
public double deposite;
public double withdraw;
public Account(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getDeposite() {
balance = balance + deposite;
return deposite;
}
public void setDeposite(double deposite) {
this.deposite = deposite;
}
public double getWithdraw() {
return withdraw;
}
public void setWithdraw(double withdraw) {
this.withdraw = withdraw;
if(withdraw <= balance){
balance = balance - withdraw;
}
}
public boolean withdraw(double wamt)
{
boolean result = false;
if(withdraw <= wamt)
{
balance= balance - withdraw;
return true;
}
return result;
}
}
My customer class
public class Customer {
private String firstName;
private String lastName;
Account account;
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
//this.account = account;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account; }
}
Main class:
public class BankProjectDemo {
public static void main(String[] args) {
double balance = 500;
Customer cust = new Customer("asasd0","asdasda");
Account accnt = new Account(balance);
System.out.println("Creating customer: " +cust.getFirstName());
accnt.setWithdraw(150);
accnt.setDeposite(22.50);
System.out.println("Withdraw1 "+accnt.getWithdraw());
System.out.println("Depsoite "+accnt.getDeposite());
Account accnt1 = new Account(balance);
accnt1.setWithdraw(47.62);
System.out.println("Withdraw2 "+accnt1.getWithdraw()+" " + accnt1.withdraw(balance));
System.out.println("Balance " + accnt.getBalance());
}
}
public boolean withdraw(double wamt)
{
boolean result = false;
if(withdraw <= wamt)
{
balance= balance - withdraw;
return true;
}
return result;
}
You reduce your Balance both withdraw() and setWithdraw() So you set it once and then reduce again on your system println thats why at the end you get twice the reduction
public double getDeposite() {
balance = balance + deposite;
return deposite;
}
This method looks wrong, every time you return a variable you're incrementing another - this means the more you call this method, the bigger the balance gets. Is this by intent?
Also - you should look into some basic unit testing as well as debugging so it's clearer to see exactly what is happening with different variables.
Maybe the problem is here
System.out.println("Balance " + accnt.getBalance());
It should be
System.out.println("Balance " + accnt1.getBalance());

Categories

Resources