2021年3月27日星期六

How do I update the element inside the arraylist?

enter image description here

    import java.util.ArrayList;      import java.util.Scanner;      import java.util.*;      import java.util.Collection;                  public class billing{              private int id;              //File f=new File("C:/Users/Bingus/Documents/Projects/accounts.txt");              private String bc;              private String bd;              private String customerName;              private String customerAddress;              private String customerNumber;              private String periodT;              private String periodF;              private double presentR;              private double previousR;              private double previousB;              private double dueTotal;              private static ArrayList<Account> accountList = new ArrayList<>();                    public billing(int id,String bc,String bd,String customerName,String customerAddress,String customerNumber,String periodT,String periodF,double presentR,double previousR,double previousB,double dueTotal){              this.id = id;              this.bc = bc;              this.bd = bd;              this.customerName = customerName;              this.customerAddress = customerAddress;              this.customerNumber = customerNumber;              this.periodT = periodT;              this.periodF = periodF;              this.presentR = presentR;              this.previousR = previousR;              this.previousB = previousB;              this.dueTotal = dueTotal;              }                public int getId(){              return id;          }          public String getBc(){              return bc;          }          public String getBd(){              return bd;          }          public String getCustomerName(){              return customerName;          }          public String getCustomerAddress(){              return customerAddress;          }          public String getCustomerNumber(){              return customerNumber;          }          public String getPeriodT(){              return periodT;          }          public String getPeriodF(){              return periodF;          }          public double getPresentR(){              return presentR;          }          public double getPreviousR(){              return previousR;          }          public double getPreviousB(){              return previousB;          }          public double getDue(){              return dueTotal;          }              public static void main(String[] args){                            Scanner scanner = new Scanner (System.in);              Scanner kb = new Scanner (System.in);              int user_choice;              int x = 0;                                do{                      System.out.println();                      System.out.println("1) New Billing");                      System.out.println("2) Add Existing Billing");                      System.out.println("3) View Billing Account ID");                      System.out.println("4) View By Date");                      System.out.println("5) Update Existing Billing");                      System.out.println("6) Delete Billing Account");                      System.out.println("7) Display All Account");                      System.out.println("8) Exit");                      System.out.println();                      System.out.print("Enter choice [1-8]: ");                      user_choice = scanner.nextInt();                            switch (user_choice){                                case 1:                           int min = 1000;                          int max = 9999;                          int randomStr = (int)(Math.random() * (max - min + 1) + min);                          int id = randomStr;                          System.out.println("Your Account Number is : " + id);                          System.out.print("Enter Billing Code: ");                          String bc = scanner.next();                          System.out.print("Enter Billing Date(dd/mm/yyyy): ");                          String bd = scanner.next();                          System.out.print("Enter Customer Name: ");                          String customerName = kb.nextLine();                          System.out.print("Enter Customer Address: ");                          String customerAddress = kb.nextLine();                          System.out.print("Enter Customer Number: ");                          String customerNumber = scanner.next();                          System.out.print("Enter Period To: ");                          String periodT = scanner.next();                          System.out.print("Enter Period From: ");                          String periodF = scanner.next();                          System.out.print("Enter Present Reading: ");                          double presentR = scanner.nextDouble();                          System.out.print("Enter Previous Reading: ");                          double previousR = scanner.nextDouble();                                                System.out.print("Enter Previous Balance: ");                          double previousB = scanner.nextDouble();                          double dueTotal = getTotalDue(presentR,previousR,previousB);                          Account user = new Account(id,bc,bd,customerName,customerAddress,customerNumber,periodT,periodF,presentR,previousR,previousB,dueTotal);                          accountList.add(user);                          break;                                case 2:                                      case 3:                          System.out.print("Enter Account Number: ");                               int a = scanner.nextInt();                                for(int i = 0; i<accountList.size();i++){                                  if(a == accountList.get(i).getId()){                              System.out.println("Account ID: " + accountList.get(i).getId());                              System.out.println("Customer Name: " +accountList.get(i).getCustomerName());                              System.out.println("Customer Address: " + accountList.get(i).getCustomerAddress());                              System.out.print("Customer Number: " + accountList.get(i).getCustomerNumber());                              }                          }                          System.out.println("\nBilling Code\t\tBilling Date\t\tAmount Due");                               for(int i = 0; i<accountList.size();i++){                                   if(a == accountList.get(i).getId())                                  {                                   System.out.println(accountList.get(i).getBc()+"\t\t\t"+accountList.get(i).getBd()+"\t\t\t"+accountList.get(i).getDue());                                 }                              }                              break;                          case 4:                                                    case 5:                            System.out.print("Enter Account Number: ");                             a = scanner.nextInt();                             for(int i = 0; i<accountList.size();i++){                            if(a == accountList.get(i).getId())                                  {                                      System.out.println("Your Account Number is : " + accountList.get(i).getId());                                        System.out.print("Enter Billing Code: ");                                      String bCode = scanner.next();                                       String c = accountList.get(i).getBc(); //this is the part in which i am having a hard time to fix, ive used the set but still i cannot change the element inside.                                      int index = accountList.indexOf(c);                                      accountList.set(index, bCode);                                                                                                                     }                              }                          break;                                                    case 6:                           System.out.print("Enter Account Number: ");                          a = scanner.nextInt();                           System.out.print("Enter Billing Code: ");                          String b = scanner.next();                          for(int i = 0; i<accountList.size();i++){                              if(a == accountList.get(i).getId()){                                        if(b.equals(accountList.get(i).getBc())){                                          accountList.remove(i);                                          System.out.print("\nAccount Removed\n");                                              }else{                                          System.out.print("Invalid Billing Code\n");                                        }                                                                               }else if(a != accountList.get(i).getId()){                                      System.out.print("Invalid Account Number or Number not in the database.\n");                              }else{                                  System.out.print("Try Again\n");                              }                          }                                              break;                                                    case 7:                          System.out.println("Account ID\t\tBilling Code\t\tAccount Name\t\tTotal Due\t\tPresent R\t\tPrevious R");                             Collections.sort(accountList,Collections.reverseOrder());                          for(int i=0; i<accountList.size();i++){                              System.out.println(accountList.get(i).getId() + "\t\t\t"+accountList.get(i).getBc()+ "\t\t\t"+accountList.get(i).getCustomerName()+ "\t\t\t"+accountList.get(i).getDue()+"\t\t\t"+accountList.get(i).getPresentR()+ "\t\t\t"+accountList.get(i).getPreviousR());                          }                          break;                                                  }                                           }while(user_choice!=8);          }                    public static double getTotalDue(double presR,double prevR,double prevB){                  double answer = 0;                  double gc = 0;                  double tc = 0;                  double slc = 0;                  double dc = 0;                  double sc = 0;                  double mc = 0;                  double totalDue = 0;                                     answer = presR - prevR;                   gc = 4.4397 * answer;                   tc = 0.4122 * answer;                   slc = 0.7956 * answer;                   dc = 1.6902 * answer;                   sc = 0.3829 * answer;                   mc = 0.5424 * answer;                        totalDue = gc + tc + slc + dc + sc + mc + prevB;                  return totalDue;              }           }  

I'm new to programming in Java and I'm still learning towards it. I'm making my billing system which calcualtes the payment. My problem for this is how can I change or update the value which is already in the arraylist, I've tried the set() but I cannot make it work. using arraylist is a big jump for me and I haven't yet got a hang of it. I've watched youtube vids but they seem to show non user input arry lists

Any help?

https://stackoverflow.com/questions/66828516/how-do-i-update-the-element-inside-the-arraylist March 27, 2021 at 02:11PM

没有评论:

发表评论