I have the following arrayList: private static ArrayList<Customer> customers = n
ID: 3529939 • Letter: I
Question
I have the following arrayList:
private static ArrayList<Customer> customers = new ArrayList<Customer>(); which goes through my main class
public static void main(String[] args) {
Customer generalC;
Transaction generalT;
double withdrawalAmount = 0;
boolean finished = false;
while (finished == false) {
// Menu Display and Get user input
int inputInt = 0;
while (inputInt == 0) {
inputInt = displayMenuAndGetInput();
// if the input is out of range
if ((inputInt < 1) || (inputInt > 8)) {
System.out.println(" The input is out of range!");
System.out.println();
inputInt = 0;
}
} // end while
// switch to correspondence function
switch (inputInt) {
case 1:
generalC = createPersonalCustomer();
System.out.println(" The Personal customer has been created: "
+ generalC.toString());
customers.add(generalC);
System.out.println();
break;
case 2:
generalC = createCommercialCustomer();
System.out.println(" The Commercial customer has been created: "
+ generalC.toString());
customers.add(generalC);
break;
case 3:
generalT = recordTransaction();
if (generalT != null)
System.out.println(" The Transaction has been created: "
+ generalT.toString());
else
System.out.println(" The ID could not be found. ");
break;
case 4:
withdrawalAmount = makeWithdrawal();
if (withdrawalAmount > 0)
System.out.println(" Amount withdrawn from this account: "
+ moneyFormat.format(acct.getWithdrawAmount()) + " ");
else
System.out.println(" The ID could not be found. ");
break;
case 5:
displayCustomer();
break;
case 6:
displayCustomerSummary();
break;
case 7:
displayGrandSummary();
break;
case 8:
// exit
finished = true;
System.out.println("Goodbye.");
break;
default:
System.out.println("Invalid Input!");
System.out.println("");
break;
} // end switch
} // end while
the code below should scan the customerID, see if it is in generalC (customers) ArrayList. if it is not in the array list or valid it should go to Case 3 in the switch statement. If it is valid, it should ask for the system.out.printlns below. For the life of me, I cannot get this to work. if I do return generalT, which references the Transaction class, the program accepts every customerID valid or not. If I do return null, then the program declines every customerID. I know I am messing up somewhere. I do not know switch statements very well. Please help me figure out what I am doing wrong below.
// Create a new Transaction
public static Transaction recordTransaction() {
System.out.println("Enter the customer ID to create the transaction > ");
generalC.customerID = scan.nextLong();
for (Customers c : customers) {
if(customers.contains (generalC.customerID)) {
System.out.println(" Enter the weight of gold > ");
Transaction.goldWt = scan.nextDouble();
System.out.println(" Enter the weight of platinum > ");
Transaction.platWt = scan.nextDouble();
System.out.println(" Enter the weight of silver > ");
Transaction.silvWt = scan.nextDouble();
}
}
return null;
Explanation / Answer
//the customer class is something like that
public class Customer {
//attributes
int id;
int tel;
String fname;
String lname;
String resgistrationDate;
Customer findCustomerByid(int id){
boolean exist=false;
if(this.customers.isEmpty()) {
return null;
}
for(int i=0;i<this.customers.size();i++) {
if(this.customers.get(i).getId() == id) {
exist=true;
break;
}
if(exist) {
return this.customers.get(id);
} else {
return this.customers.get(id);
}
}
}
}