JAVA .WRITE THE CODES FOR A-E a. (Refer to class diagram 7-1.) Declare the Accou
ID: 3677941 • Letter: J
Question
JAVA .WRITE THE CODES FOR A-E
a. (Refer to class diagram 7-1.) Declare the Account class and its instance variables. Then, code a constructor that assigns default values to these variables. The default value you assign to the customer variable should prevent a NullPointerException (hint: do not assign a default value of null to the customer variable). Write this code as concisely as possible Class diagram 7-1 Account -nber int type: String balance: double getNurber int +setCustaner(custaner) tgetCustomer (int): Custoner +setType(Stringi) getType:String +set Balance (double) tget Balance): doble tgetFormattedBalance String creditccount (double) tdebitRocount (double)Explanation / Answer
import java.text.DecimalFormat;
import java.text.NumberFormat;
/**
* @author Srinivas Palli
*
*/
public class Account {
int number;
Customer customer;
String type;
double balance;
/**
* @param number
* @param customer
* @param type
* @param balance
*/
public Account() {
this.number = 0;
this.customer = new Customer("name of customer");
this.type = "";
this.balance = 500;
}
/**
* @return the number
*/
public int getNumber() {
return number;
}
/**
* @return the customer
*/
public String getCustomerName() {
return customer.getCustomerName();
}
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @return the balance
*/
public double getBalance() {
return balance;
}
/**
* @param number
* the number to set
*/
public void setNumber(int number) {
this.number = number;
}
/**
* @param customer
* the customer to set
*/
public void setCustomer(Customer customer) {
this.customer = customer;
}
/**
* @param type
* the type to set
*/
public void setType(String type) {
this.type = type;
}
/**
* @param balance
* the balance to set
*/
public void setBalance(double balance) {
this.balance = balance;
}
/**
* @return
*/
public String getFormattedBalance() {
NumberFormat format = new DecimalFormat("#.##");
return format.format(balance);
}
/**
* @param amount
*/
public void creditAccount(double amount) {
balance += amount;
}
/**
* @param amount
*/
public void debitAccount(double amount) {
if (balance >= amount)
balance -= amount;
}
}
public class Customer {
String customerName;
/**
* @param customerName
*/
public Customer(String customerName) {
this.customerName = customerName;
}
/**
* @return the customerName
*/
public String getCustomerName() {
return customerName;
}
/**
* @param customerName
* the customerName to set
*/
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.