Use Java ToWith notes of Steps Please Write a program to cre

Use Java To(With notes of Steps Please):

Write a program to create a class Customer. The properties of the customer are ssn, name, gender, account_No, balance. Include the following methods in the class. (20 marks)

a) changeName(String newName), which takes the new name and changes the name of the customer.

b) doTransaction(float amount), which takes the amount (which can be positive or negative) and changes the balance of the customer.

Solution

Customer.java

public class Customer {

   // Declaring instance variables
   private String ssn;
   private String name;
   private char gender;
   private int account_No;
   private float balance;

   // Parameterized constructor
   public Customer(String ssn, String name, char gender, int account_No,
           float balance) {
       super();
       this.ssn = ssn;
       this.name = name;
       this.gender = gender;
       this.account_No = account_No;
       this.balance = balance;
   }

   // This method is used to modify the name of the Account Holder
   void changeName(String newName) {
       this.name = newName;
   }

   /*
   * This method is used to perform the transactions like depositing the
   * amount or withdraw the amount
   */
   void doTransaction(float amount) {
       this.balance = balance + amount;

   }

   // toString method is used to display the contents of an object inside it
   @Override
   public String toString() {
       return \"SSN=\" + ssn + \", Name=\" + name + \", Gender=\" + gender
               + \", Account No=\" + account_No + \", Balance=\" + balance;
   }

}

__________________

TestCustomer.java

public class TestCustomer {

   public static void main(String[] args) {
       //Creating the Customer class object by passing the values as arguments
   Customer cust1=new Customer(\"123-45-6789\", \"Williams\", \'M\',1122,3000);
     
   //Displaying the contents of the customer class obejct
   System.out.println(cust1.toString());
     
   //Changing the customer name
   cust1.changeName(\"Johnson\");
     
   //Withdrawing the amount
   cust1.doTransaction(-550);
     
   //Displaying the contents of the customer class obejct
   System.out.println(cust1.toString());

   //Changing the customer name
   cust1.changeName(\"Philips\");
     
   //Depositing the amount
   cust1.doTransaction(3000);
     
   //Displaying the contents of the customer class obejct
   System.out.println(cust1.toString());     

   }

}

_________________

output:

SSN=123-45-6789, Name=Williams, Gender=M, Account No=1122, Balance=3000.0
SSN=123-45-6789, Name=Johnson, Gender=M, Account No=1122, Balance=2450.0
SSN=123-45-6789, Name=Philips, Gender=M, Account No=1122, Balance=5450.0

___________Thank You

Use Java To(With notes of Steps Please): Write a program to create a class Customer. The properties of the customer are ssn, name, gender, account_No, balance.
Use Java To(With notes of Steps Please): Write a program to create a class Customer. The properties of the customer are ssn, name, gender, account_No, balance.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site