Salman Hanif Salman Hanif - 26 days ago 5
Java Question

how to change the values in the array list

I am doing an Assignment in school, where i have to make Restaurant table management system. i have written the following code but i am having problem with the edit items. i am supposed to edit the items in the array list. but i have to make sure when i re-run my program the changes are still there. editMenu() is in Manager class and the array List is in Meal class

import java.util.*;

public class Manager {
private String name, username, password;
private Scanner input;
private static ArrayList<Manager> managers = new ArrayList<>();
private int promotion;

public Manager(){
this("", "", "");
}

public Manager(String username, String password){
this("", username, password);
}

public Manager(String name, String username, String password){
this.name = name;
this.username = username;
this.password = password;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}


public int getPromotion() {
return promotion;
}

public void setPromotion(int promotion) {
this.promotion = promotion;
}


public void createNewManager(){

receiveCredentials(); //Receive input for new manager credentials

boolean isManagerExist = false;
for(Manager manager: managers){
if (manager.name.equals(name)){
isManagerExist = true;
System.out.println("Manager Account Already Exists!\n");
break;
}
} //Checking whether new manager credentials match with existing manager
if (!isManagerExist){
managers.add(this);
System.out.println("New Manager Account Successfully Created.\n");
displayManager();
} //Create new manager and add to managers list if the manager does not exist
}

public void receiveCredentials(){
//Receiving all manager credentials and double checking password entry
input = new Scanner(System.in);
System.out.println("Create New Manager Account\n--------------------------");
System.out.print("Enter manager name: ");
name = input.nextLine();
System.out.print("Enter new username: ");
username = input.nextLine();
System.out.print("Enter new password: ");
String passwordFirstTyped = input.nextLine();
System.out.print("Retype new password: ");
String passwordRetyped = input.nextLine();
while(!passwordFirstTyped.equals(passwordRetyped)){
System.out.println("Retyped password does not match! Try again.");
System.out.print("Enter new password: ");
passwordFirstTyped = input.nextLine();
System.out.print("Retype new password: ");
passwordRetyped = input.nextLine();
}
password = passwordFirstTyped;
}

public boolean isValidManager(){
boolean isValid = false;
for (Manager manager: managers){
if (manager.username.equals(username) && manager.password.equals(password))
isValid = true;
}
return isValid; //Validating manager credentials through managers list
}

public static void displayManager(){
System.out.println("Managers List\n-------------");
int count = 1;
for (Manager manager: managers){
System.out.print(count + ". Manager Name: ");
System.out.println(manager.name);
System.out.print(" Manager Username: ");
System.out.println(manager.username + "\n");
count++;
} //Displaying all managers in the list
}

public static void deleteManager(String name){
//Checking whether specified manager exist and deleting the manager from managers list if exist
boolean deleted = false;
for (Manager manager: managers){
if (manager.name.equals(name)){
deleted = managers.remove(manager);
}
}
if (deleted)
System.out.println("Specified Manager Account Successfully Deleted.");
else
System.out.println("Specified Manager Account Does Not Exist!");
}

public void editMenu(){

}
}


This is the meal class the array List consist of MealCode, MealPrice, and MealName.

import java.util.*;

public class Order {
private int noOfPax;
private ArrayList<Meal> meals = new ArrayList<>();

public Order(){
this(0);
}

public Order(int noOfPax){
this.noOfPax = noOfPax;
}

public int getNoOfPax() {
return noOfPax;
}

public void setNoOfPax(int noOfPax) {
this.noOfPax = noOfPax;
}

public ArrayList<Meal> getMeals() {
return meals;
}

public void setMeals(ArrayList<Meal> meals) {
this.meals = meals;
}

public static void displayFunctions(int tableNum){
//Standing here&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
}


}

Answer

When you re-run the program, everything starts from zero. You need to persist your list in a file or database and read it when you restart your program.