Question: Can someone fix my code? I'm new at c and I somehow mixed my understanding of Java and C. Can you help me fix it
Can someone fix my code? I'm new at c and I somehow mixed my understanding of Java and C. Can you help me fix it so my user_interface is working?
There's a lot of errors in my code and I'm kinda stuck on how to fix it.
The goal of the program is
To access the database, the banker interacts with the user-interface, which displays data and accepts inputs while accessing the database part.
In this assignment, you will finish the user-interface code.
Here is supposed to be the data structure of the record:
struct record { int accountno; char name[25]; char address[50]; struct record* next; }; Here is the database:
int addRecord (struct record **, int, char [ ],char [ ]); void printAllRecords(struct record *); int findRecord (struct record *, int); int deleteRecord(struct record **, int);
Here is my code: (Please help me fix this I'm really desperate on how to do it but I'm kinda stuck)
#include
static void menu() { printf("Greetings, what do you want to do with the database? "); printf("------------------------------------------------------------------------ ");
printf(" 1. add "); printf("2. printall "); printf("3. find "); printf("4, delete "); printf("5. quit "); }
// method to add customer static boolean addCustomer(ArrayList customers,BankCustomer newCustomer) { // looping and finding the proper position to add for (int i = 0; i < customers.size(); i++) { // checking if new customer can be added to index i if (newCustomer.getAcctNumber() < customers.get(i).getAcctNumber()) { // adding to index i customers.add(i, newCustomer); return true; // success } else if (newCustomer.getAcctNumber() == customers.get(i).getAcctNumber()) { // duplicate found, returning false return false; } } // adding to the end, if still not added customers.add(newCustomer); return true; // success }
// method to print customers static void printCustomers(ArrayList customers) { // displaying heading printf("%-15s %-15s %-15s ", "Customer Name","Account Number", "Account Balance"); // looping and printing each customer's name, acc num and balance for (BankCustomer c : customers) { printf("%-15s %-15d $%-14.2f ", c.getName(), c.getAcctNumber(), c.getBalance()); } printf("----------------------------------------------- "); } // method to find customer
// method to remove a customer by account number, if exists static boolean removeCustomer(ArrayList customers,int accountNum) { for (int i = 0; i < customers.size(); i++) { if (customers.get(i).getAcctNumber() == accountNum) { // found, removing current customer customers.remove(i); return true; // found and removed } } return false; // not found }
// main method int main(int argc, char* argv[]) { // creating an array list of BankCustomer ArrayList accounts = new ArrayList(); // scanner to read user input Scanner scanner = new Scanner(System.in); int ch = 0; // looping until ch is 4 do { // displaying menu menu(); // inside try with multiple catches block, getting and processing // user input try { printf("Enter your choice: "); //reading choice ch = Integer.parseInt(scanner.nextLine()); printf(); int accNum; // use switch instead if-else if, // using switch case for menu is better. I tried using switch upon seeing Lenora's code during the meeting. switch(ch) { case 1: //reading details for bank customer printf("Enter name: "); String name = scanner.nextLine(); printf("Enter account number: "); accNum = Integer.parseInt(scanner.nextLine()); printf("Enter balance: "); double balance = Double.parseDouble(scanner.nextLine()); //creating a customer, will throw BCException if any detail is invalid BankCustomer cust = new BankCustomer(accNum, balance, name); //if no exception occurred, adding to the list and displaying the result if (addCustomer(accounts, cust)) { printf"Success!"); } else { printf("Customer with same account number already exists!"); } break; case 2: //fetching an account number, removing customer printf("Enter account number: "); accNum = Integer.parseInt(scanner.nextLine()); if (removeCustomer(accounts, accNum)) { printf("Success!"); } else { printf("Customer with given account number does not exist!"); } break; case 3: //printing customers printCustomers(accounts); break; } } catch (BCException e) { //BCException is occurred printf(e.getMessage()); } catch (Exception e) { /printf("Invalid input!"); } } while (ch != 0); // replace 4 into 0, from the feedback }
Plus also help me on how to incorporate the database on the user_interface. Do I need to create a separate file for that?
Just ask me if you need more information. Thanks again.