# Increase book amount increaseBookAmount() { object=$1 amount=$2 cmd1=${object}_getBookAmount oldAmount=${!cmd1}# to execute the cmd1 variable newAmount=$(($oldAmount + $amount)) export${object}_getBookAmount="$newAmount" }
# Decrease book amount with error checking and handling decreaseBookAmount() { object=$1 amount=$2 cmd1=${object}_getBookAmount oldAmount=${!cmd1}# to execute the cmd1 variable newAmount=$(($oldAmount - $amount)) if [ $newAmount -lt 0 ]; then echo"Error: Not enough books to decrease" else export${object}_getBookAmount="$newAmount" fi } }
// Increase book amount publicvoidincreaseBookAmount(int inAmount){ bookAmount += inAmount; }
// Decrease book amount with error checking and handling publicvoiddecreaseBookAmount(int inAmount){ if (bookAmount - inAmount < 0){ System.out.println("Error: Not enough books to decrease"); } else { bookAmount -= inAmount; } }
publicstaticvoidmain(String[] args){ BookmyBook=newBook("Harry Potter", 300); System.out.println(myBook.getBookName()); // Harry Potter System.out.println(myBook.getBookAmount()); // 300 myBook.setBookName("Lord of the Rings"); System.out.println(myBook.getBookName()); // Lord of the Rings myBook.increaseBookAmount(100); System.out.println(myBook.getBookAmount()); // 400 myBook.decreaseBookAmount(200); System.out.println(myBook.getBookAmount()); // 200 myBook.decreaseBookAmount(300); // Error: Not enough books to decrease } }
# Setter method # e.g. setBookName myBook "The Lord of the Rings" setBookName() { object=$1 name=$2 export${object}_getBookName="$name" }
# Increase book amount # e.g. increaseBookAmount myBook 100 increaseBookAmount() { object=$1 amount=$2 cmd1=${object}_getBookAmount oldAmount=${!cmd1}# to execute the cmd1 variable newAmount=$(($oldAmount + $amount)) export${object}_getBookAmount="$newAmount" }
# Decrease book amount with error checking and handling # e.g. decreaseBookAmount myBook 100 decreaseBookAmount() { object=$1 amount=$2 cmd1=${object}_getBookAmount oldAmount=${!cmd1}# to execute the cmd1 variable newAmount=$(($oldAmount - $amount)) if [ $newAmount -lt 0 ]; then echo"Error: Not enough books to decrease" else export${object}_getBookAmount="$newAmount" fi } }
Book myBook "Harry Potter" 300 echo$myBook_getBookName# Harry Potter echo$myBook_getBookAmount# 300 setBookName myBook "The Lord of the Rings" echo$myBook_getBookName# The Lord of the Rings increaseBookAmount myBook 100 echo$myBook_getBookAmount# 400 decreaseBookAmount myBook 200 echo$myBook_getBookAmount# 200 decreaseBookAmount myBook 300 # Error: Not enough books to decrease