change the .equals to basic java please since not sure how it works and also ple
ID: 3752305 • Letter: C
Question
change the .equals to basic java please since not sure how it works and also please explain it thank you since an new to jave somewhat thanks.
public void banking() {
int choice = 0;// variable to store the user choice
do {// loop until user enters 0.
System.out.println("(1)deposit (2)withdraw (0)quit");
choice = input.nextInt();
// switch based on the users choice
switch (choice) {
case 1:// if user enters 1
System.out.print("Customer name: ");
input.nextLine();
String name = input.nextLine();
System.out.print("Deposit amount = ");
double depositAmount = input.nextDouble();
boolean isDeposited = false;
for (Account account : accounts) {
if (account.getName().equals(name)) {// checking given account name is in the list or not
account.deposit(depositAmount);
isDeposited = true;
break;
}
}
case 2: // if user enters 2.
System.out.print("Customer name: ");
input.nextLine();
name = input.nextLine();
System.out.print("Withdraw amount = ");
double withDrawAmount = input.nextDouble();
boolean isWithdrew = false;
for (Account account : accounts) {
if (account.getName().equals(name)) {// checking given account name is in the list or not
if (account.getBalance() - withDrawAmount >= 0) { // checking accountant has enough balance to withdraw
account.withdrawal(withDrawAmount);
}
isWithdrew = true;
break;
}
}
case 0:// if user enters 0
break;
}
} while (choice != 0);// if users choice is 0 then it is the loop breaking condition
}
Explanation / Answer
Simple if you want to compare two string you need to use .equals() ...
For example :
public class Test {
public static void main(String[] args)
{
String s1 = "Test";
String s2 = "Test";
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}
Output:
false
true
Here (account.getName().equals(name)) you are checking the acccount name with name by using equal().. There is no other way .
Or use the method ... checkString()
public class Banking {
int choice = 0;// variable to store the user choice
Scanner input = new Scanner(System.in);
do {// loop until user enters 0.
System.out.println("(1)deposit (2)withdraw (0)quit");
choice = input.nextInt();
// switch based on the users choice
switch (choice) {
case 1:// if user enters 1
System.out.print("Customer name: ");
input.nextLine();
String name = input.nextLine();
System.out.print("Deposit amount = ");
double depositAmount = input.nextDouble();
boolean isDeposited = false;
for (Account account : accounts) {
if (checkString(account.getName(), name)) {// checking given account name is in the list or not
account.deposit(depositAmount);
isDeposited = true;
break;
}
}
case 2: // if user enters 2.
System.out.print("Customer name: ");
input.nextLine();
name = input.nextLine();
System.out.print("Withdraw amount = ");
double withDrawAmount = input.nextDouble();
boolean isWithdrew = false;
for (Account account : accounts) {
if (checkString(account.getName(), name)) {// checking given account name is in the list or not
if (account.getBalance() - withDrawAmount >= 0) { // checking accountant has enough balance to withdraw
account.withdrawal(withDrawAmount);
}
isWithdrew = true;
break;
}
}
case 0:// if user enters 0
break;
}
} while (choice != 0);// if users choice is 0 then it is the loop breaking condition
}
public static boolean checkString(String s1, String s2) {
if (s1.length() == s2.length()) {
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) != s2.charAt(i)) {
return false;
}
}
return true;
}
return false;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.