Java - Going through arraylist and outputting highest to lowest numbers? - java

Is there a way to make it so I can sort through an ArrayList output the highest number found along with the lowest number found? The way I have it now is a "shoppinglist" which is set up like this:
public void addToBag(String anyItem, int anyUnits, double anyCost){
int checkUnits = anyUnits;
double checkCost = anyCost;
if(checkUnits < 0 || checkCost < 0){
System.out.println("You must enter a value equal to or greater than 0!");
}else{
shoppingBag.add(new Item(anyItem, anyUnits, anyCost));
}
}
and the output which only outputs the list
public void calculateSalesReceipt(){
System.out.println("Enter the sales tax percentage (ex. 0.08 for 8%) or type \"random\" for a random number: ");
double tax = keybd.nextDouble();
if(tax < 0){
System.out.println("You must enter a value equal to or greater than 0!");
}else{
getPricePreTax();
total = total;
taxCost = total * tax;
finaltotal = total + taxCost;
System.out.println("Sales Receipt");
System.out.println("-------------");
for(Item currentProduct : shoppingBag){
System.out.println(currentProduct.getName() + " - " + currentProduct.getUnits() + " units " + " - $" + currentProduct.getCost());
}
System.out.println("Total cost: $" + total);
System.out.println("Total tax: $" + taxCost);
System.out.println("Total cost with tax: $" + finaltotal);
System.out.println("Do you have any coupons? Enter \"yes\" or \"no\"");
String answer = keybd.next();
if(answer.equals("yes")){
applyCoupon();
}else if(answer.equals("no")){
System.out.println("Thank you!");
}else if(answer != "yes" || answer != "no"){
System.out.println("Thank you!");
}
System.out.println("Coupon discounts: $" + couponAmount);
System.out.println("Grand total: $" + (finaltotal-couponAmount));
}
}
Thanks!
EDIT:
Would this work?
public void getLargest(){
for(Item currentProduct : shoppingBag){
System.out.println(Collections.max());
}
}

You probably want Collections.max() (and Collections.min(), respectively). If you want the entire list in order, you probably want Collections.sort().

Using Google Guava:
Function<Item, Double> itemCostFunction = new Function<Item, Double>() {
#Override public Double apply(Item item) {
return item.getCost();
}
};
List<Item> sortedItems = Ordering.natural().onResultOf(itemCostFunction).sortedCopy(shoppingBag);
Now
Item biggest = Iterables.getLast(sortedItems); // costliest
Item smallest = sortedItems.get(0); // cheapest

What do you want to sort on? Cost?
You can create an new class; CostComparator implementing a Comparator<Item>, and compare Items depeding on their price.

Related

Throws Exception when trying to break loop because a condition has been met

I am trying to figure out why I cannot get the loop to break if either "n" is input for playAgain or when the total is below $10. If you see how I can break the loop to run the gameOver function without having an exception thrown that would be a great help. I have noted in the code below that I am having trouble with. I am unsure why this exception is being thrown. If you are able to find out how to break the loop when total is less than 10 or when playAgain is false please let me know!
import java.util.Random;
import java.util.Scanner;
public class GameOfCrapsTester {
static Scanner in = new Scanner(System.in);
static Random rand = new Random();
public static void main(String[] args) {
System.out.println("Welcome to the game of Craps");
System.out.println(" ");
System.out.println("The house has given you a starting balance of $500");
System.out.println("On each round, you will make a whole number wager.");
System.out.println("The minimum wager is $10, and the maximum wager is your remaining balance.");
System.out.println(" ");
System.out.println("You may keep playing until you decide to cash in, or");
System.out.println(" you can't cover the minimum wager.");
System.out.println("Good Luck!");
boolean win;
double wins = 0, numOfGames = 0;
int total = 500;
// Come out roll and set point value
int pointValue = 0;
boolean playAgain = true;
while (playAgain && total > 0)
{
System.out.println(" ");
System.out.println("Your balance is $" + total);
System.out.println(" ");
System.out.println("Place your bet: $");
// Get and check wager placed
int bet = in.nextInt();
while (bet > total || bet < 10)
{
if (bet < 10)
{
System.out.println("Bet must be larger than $10.");
}
System.out.println("I'm sorry, that's not a valid wager; please re-enter: ");
bet = in.nextInt();
}
int num = rollDice();
if ((num >= 4 && num <= 10 && num != 7) || num == 0)
{
pointValue = num;
System.out.println(" ");
System.out.println("Your point value is " + pointValue);
System.out.println(" ");
win = rollWithPoint(pointValue);
if (win)
{
total = wonGame(bet, total);
wins++;
numOfGames++;
System.out.println("Wins: " + wins + " Number of games: " + numOfGames);
}
else if (!win)
{
total = lostGame(bet, total);
numOfGames++;
System.out.println("Wins: " + wins + " Number of games: " + numOfGames);
}
}
else if (num == 7 || num == 11)
{
total = wonGame(bet, total);
wins++;
numOfGames++;
System.out.println("Wins: " + wins + " Number of games: " + numOfGames);
}
else
{
total = lostGame(bet, total);
numOfGames++;
System.out.println("Wins: " + wins + " Number of games: " + numOfGames);
}
if (total <= 9) // THIS IS WHERE I AM HAVING TROUBLE
{
break;
}
System.out.println("Keep playing (y/Y or n/N)? ");
in.nextLine();
String again = in.nextLine();
if (again.equalsIgnoreCase("y"))
{
playAgain = true;
}
else if (again.equalsIgnoreCase("n")) // THIS IS WHERE I AM HAVING TROUBLE
{
break;
}
else
{
System.out.println("Invalid character input, try again:");
again = in.nextLine();
}
}// end of loop
gameOver(wins, numOfGames);
} // END of main
public static int rollDice() {
int dice1, dice2, total;
dice1 = rand.nextInt(6) + 1;
dice2 = rand.nextInt(6) + 1;
total = dice1 + dice2;
System.out.print("Your roll: ");
System.out.print("Dice1: " + dice1);
System.out.print(", Dice2: " + dice2);
System.out.println("; Roll Value: " + total);
return total;
} // END of rollDice
public static boolean rollWithPoint(int point) {
int total = rollDice();
boolean winner = false;
while(total != 7 && winner == false)
{
total = rollDice();
if (total == point)
{
winner = true;
}
else
{
winner = false;
}
}
return winner;
} // END of rollWithPoint
public static int lostGame(int bet, int total) {
System.out.println("Oh, I'm sorry, you lost.");
System.out.println(" ");
total = total - bet;
System.out.println("Your current balance: $" + total);
System.out.println(" ");
return total;
} // END of lostGame
public static int wonGame(int bet, int total) {
System.out.println("A winner!");
System.out.println(" ");
total = total + bet;
System.out.println("Your current balance: $" + total);
System.out.println(" ");
return total;
} // END of wonGame
public static void gameOver(double win, double tot) {
double winPercent = (win / tot) * 100;
System.out.println(" ");
System.out.printf("Based on your play, the probability of winning is %.2%", winPercent);
System.out.println(" ");
System.out.println("Seems you lost your shirt; better luck next time.");
System.out.println("Have a nice day! Hope to see you soon!");
} // END of gameOver
} // END of GameOfCraps
There is no error when you change this (without using String.format()):
System.out.printf("Based on your play, the probability of winning is %.2%", winPercent);
To this:
System.out.println("Based on your play, the probability of winning is " + winPercent + "%.");
Example with a little bet (console):
Your balance is $11
Place your bet: $
10
Your roll: Dice1: 1, Dice2: 2; Roll Value: 3
Oh, I'm sorry, you lost.
Your current balance: $1
Wins: 0.0 Number of games: 2.0
Based on your play, the probability of winning is 0.0%.
Seems you lost your shirt; better luck next time.
Have a nice day! Hope to see you soon!
I cannot get the loop to break if either "n" is input for playAgain or
when the total is below $10.
It works fine too. If I put a bet below 10 it asks me to put another bit. What

Trying to have program return to main menu after user answers yes to continue or have program end and display message of total if user answers no

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean start = true;
while(start)
System.out.printf("%70s %n", " ##### Zoos Australia ##### " + "\n");
System.out.printf("%57s %n", "Main Menu" + "\n");
System.out.printf("%72s %n", "Zoo has the following ticketing options:");
System.out.print("\n");
System.out.printf("%59s %n", "1 = Child (4-5 yrs)");
System.out.printf("%59s %n", "2 = Adult (18+ yrs)");
System.out.printf("%60s %n", "3 = Senior (60+ yrs)");
System.out.println("\n");
String choose1 = "";
String choose2 = "";
String choose3 = "";
String selected = "";
int option = 0;
{
System.out.print("Please select an option: ");
option = input.nextInt();
if (option == 1) {
choose1 = "Child";
selected = choose1;
} else if (option == 2) {
choose2 = "Adult";
selected = choose2;
} else if (option == 3) {
choose3 = "Senior";
selected = choose3;
}
}
// done
System.out.println("\n");
int price = 0;
int tickets = 0;
System.out.print("Enter the number of tickets: ");
tickets = input.nextInt();
if (selected == choose1) {
price = 10;
} else if (selected == choose2) {
price = 20;
} else if (selected == choose3) {
price = 15;
}
System.out.println("\n");
System.out.print("You are purchasing " + tickets + " " + selected + " tickets at " + "$" + price + " each!");
System.out.println("\n");
int confirm = 0;
System.out.print("Press 1 to confirm purchase: ");
confirm = input.nextInt();
if (confirm != 1) {
System.out.print("Incorrect Key. Please return to Main Menu");
System.out.println("\n");
} else {
break;
}
System.out.println("\n");
int total = tickets;
price = total * price;
System.out.print("Total amount for " + selected + " tickets: " + "$" + price);
System.out.println("\n");
String pick = "";
System.out.print("Do you wish to continue: ");
input.next();
System.out.println("\n");
if (pick == "no") {
System.out.print("Total amount payable is: " + "$" + price);
System.out.println("\n");
System.out.print("Have a nice day!");
System.out.println("\n");
}}}
Trying to do this at the end of the program where user is asked "Do you wish to continue" using a method or something but cant get it to work. Either the program returns to main menu only or the program ends and displays the total message "Total amount payable..." etc. I have tried using while with continue and break. Using boolean with true and false. But no luck. Thank you anyone that may be able to clear this up for me please.
First, you have to assign the users's input to a variable: pick = input.next(). After that, the problem is that you compare the user's input string with a "no" string by using == operator. When comparing reference types (objects) (and String is an object), in most cases == operator gives you an unpredictable result, because it compares the reference (address of an object in memory) and not the actual content. Please remember, that you always have to use the .equals() method instead. You also have to break from your loop, when the user's input is "no".
There is plenty of material concerning this issue. You can check, for instance, this one How do I compare strings in Java?
P.S. I quickly looked at the rest of your code and put some additional comments, which might help you to improve it. Good luck with learning Java!
Scanner input = new Scanner(System.in);
// boolean start = true; you don't need this line
while(true) { // 'true' condition makes it an infinite loop until you use break
// You also have to surround your while loop with curly braces,
// otherwise you fall into an infinite loop
System.out.printf("%70s %n", " ##### Zoos Australia ##### \n");
System.out.printf("%57s %n", "Main Menu\n");
System.out.printf("%72s %n", "Zoo has the following ticketing options: \n");
System.out.printf("%59s %n", "1 = Child (4-5 yrs)");
System.out.printf("%59s %n", "2 = Adult (18+ yrs)");
System.out.printf("%60s %n", "3 = Senior (60+ yrs)\n");
String choose1 = "";
String choose2 = "";
String choose3 = "";
String selected = "";
int option = 0;
System.out.print("Please select an option: ");
option = input.nextInt();
if (option == 1) {
choose1 = "Child";
selected = choose1;
} else if (option == 2) {
choose2 = "Adult";
selected = choose2;
} else if (option == 3) {
choose3 = "Senior";
selected = choose3;
}
System.out.println(); // "\n" is a redundant argument
int price = 0;
int tickets = 0;
System.out.print("Enter the number of tickets: ");
tickets = input.nextInt();
if (selected.equals(choose1)) { // you should never compare strings with == operator! Always use .equals() instead
price = 10;
} else if (selected.equals(choose2)) {
price = 20;
} else if (selected.equals(choose3)) {
price = 15;
}
System.out.println();
System.out.print("You are purchasing " + tickets + " " + selected + " tickets at " + "$" + price + " each!");
System.out.println();
int confirm = 0;
System.out.print("Press 1 to confirm purchase: ");
confirm = input.nextInt();
if (confirm != 1) {
System.out.print("Incorrect Key. Please return to Main Menu");
System.out.println("\n");
} else {
//break; you cannot use 'break' in the if statement! You have to figure out another way, how to handle an invalid input
}
System.out.println();
int total = tickets;
price = total * price;
System.out.print("Total amount for " + selected + " tickets: " + "$" + price);
System.out.println();
String pick = "";
System.out.print("Do you wish to continue: ");
pick = input.next(); // you have to assign the input to a variable
System.out.println();
if (pick.equals("no")) { // You have to ALWAYS use .equals() when comparing Strings or any other reference types! == works correctly only with primitive types
System.out.print("Total amount payable is: " + "$" + price);
System.out.println();
System.out.print("Have a nice day!");
System.out.println();
break; // you need to break from the loop in the end
}
}
}

Loop is moving to wrong spot in code after the one iteration is run

In my code, I am trying to allow the customer to order cheesecake topping and size then give them the price of that cheesecake then add the total to a running total until they finish ordering then give them grand total order at the end when they are finished ordering.
When the code runs it gets to end gives total but no matter whether I hit yes or no on the second time around it immediately goes to cake size instead of letting me choose my flavor again and never ends to let me get my grand total.
float biteSize = 3;
float small = 9;
float large = 12;
String chosenSize = "";
double pricePerInch = 0;
double total = 0;
String userFlavor = "";
String[] chooseSizes = {"bite size", "small", "large"};
String[] chooseFlavors = {"plain", "strawberry", "raspberry", "caramel", "chocolate"};
boolean flavorFound = false;
String want = "";
double newTotal = 0;
Scanner scnr = new Scanner(System.in);
System.out.println("Flavors to choose from: plain, strawberry, raspberry, caramel, chocolate."); //giving user flavor list
while (want != "n") {
while (!flavorFound) {
System.out.println("Please choose flavor:"); // Loop until flavorFound is true
userFlavor = scnr.nextLine();
for(int i = 0; i < chooseFlavors.length; i++) { // Loop from 0 to choosFlavors.length
if(userFlavor.equals(chooseFlavors[i])) { // Compare user input to flavor at index i
System.out.println(userFlavor);
flavorFound = true;
// This is the flag to break out of while loop
break;
// Only breaks out of for loop
} else {
System.out.println("Please choose from flavors above.");
}
}
}
if (userFlavor.contains("plain")) { // setting prices for all the flavors
pricePerInch = 0.50;
}
else if (userFlavor.contains("strawberry")) {
pricePerInch = 1.25;
}
else if (userFlavor.contains("raspberry")) {
pricePerInch = 1.15;
}
else if (userFlavor.contains("caramel")) {
pricePerInch = 0.75;
}
else if (userFlavor.contains("chocolate")) {
pricePerInch = 0.85;
}
System.out.println("Sizes to choose from: bite size, small, large");
boolean sizeFound = false;
while (!sizeFound) {
// Loop until flavorFound is true
System.out.println("Please choose cheese cake size: ");
chosenSize = scnr.nextLine();
for(int i = 0; i < chooseSizes.length; i++) {
// Loop from 0 to choosFlavors.length
if(chosenSize.equals(chooseSizes[i])) {
// Compare user input to flavor at index i
System.out.println(chosenSize);
sizeFound = true;
// This is the flag to break out of while loop
break;
// Only breaks out of for loop
} else {
System.out.println("Please choose from size above.");
}
}
}
//chosenSize = scnr.nextLine();
if (chosenSize.contains("bite size")) { //setting the prices for the sizes
total = pricePerInch * biteSize;
}
else if (chosenSize.contains("small")) {
total = pricePerInch * small;
}
else if (chosenSize.contains("large")) {
total = pricePerInch * large;
}
System.out.println("Your chosen flavor: " + userFlavor); /*printing out a receipt for the customer with
the size, toping, and total cost*/
System.out.println("Your chosen size: " + chosenSize);
System.out.println("Price for topping: " + "$" + pricePerInch + " per inch.");
System.out.println("Size of cheesecake: " + chosenSize + " inches.");
System.out.printf("Your total cost will be: $" + newTotal + " dollars.");
newTotal = total + newTotal;
System.out.println("Would you like to order a cheesecake please choose y/n");
want = scnr.next();
}
System.out.printf("Your grand total cost will be: $" + newTotal + " dollars.");
}
}
change
want = scnr.next();
to
want = scnr.nextLine();
I'd recommend taking a look at this question:
What's the difference between next() and nextLine() methods from Scanner class?

Aggregating results of different calculations

This is my first post here, so forgive me for any formatting errors.
So as you can see my program requests the gender, # of accidents and year of car to display a fictitious insurance quote.
Based on all that information I need to add the subtotal of the insurance cost to the end.
I have my code working up until the Total Cost comment (posted it all for reference). I am stuck there because the genders have different base amounts. I'm trying to figure out a way to only do one if statement if it matches the gender that was input by the user.
Any ideas?
import java.util.*;
public class Insurance {
public static void main(String [] args) {
Scanner scanner = new Scanner(System.in);
int currentYear = 2017; //used for calculating the age of the users car
int maleGender = 1000;
int femaleGender = 500;
//Letting user know they are inputting data for car insurance purposes
System.out.println("Car insurance questionnaire. Please input correct information when prompted.");
// gender information from user
System.out.println("What is your gender? m/f");
String gender = scanner.next();
// accident quantity information from user
System.out.println("How many accidents have you had?");
int acc = scanner.nextInt();
// car year information from user
System.out.println("What year was your car manufactured?");
int carAge = scanner.nextInt();
//if statements which refer to the users data input
if (gender.equals("m")) {
System.out.println("You are a male.\nThe base cost is $1000.");
} else {
System.out.println("You are a female.\nThe base cost is $500.");
}
if (acc == 0) {
System.out.println("You have no accidents. Insurance increase is $0.");
} else if (acc >= 1) {
System.out.println("You have " + acc + " accidents. Insurance increase is $" + acc * 100 + ".");
}
if (carAge >= 2007) {
System.out.println("Your car is " + (currentYear - carAge) + " years old.\nYour car is still in warranty, no savings added.");
} else
System.out.println("Your car is out of warranty, final cost is halved.");
//Total cost
/*
if (carAge <= 2007) {
System.out.println("Your total price is $" + ((acc * 100 + femaleGender) / 2) + ".");
} else
System.out.println("Your total price is $" + (acc * 100 + femaleGender) + ".");
*/
}
}
I am not totally sure how you want to calculate your result but if do NOT want to use femaleGender all the time but in dependency of the gender different values then maybe something like this could help:
int baseAmount = gender.equals("m") ? maleGender : femaleGender;
if (carAge <= 2007) {
System.out.println("Your total price is $" + ((acc * 100 + baseAmount ) / 2) + ".");
} else
System.out.println("Your total price is $" + (acc * 100 + baseAmount ) + ".");
}
int genderCost;
...
if (gender.equals("m")) {
System.out.println("You are a male.\nThe base cost is $1000.");
genderCost = maleGender;
} else {
System.out.println("You are a female.\nThe base cost is $500.");
genderCost = femaleGender;
}
...
if (carAge <= 2007) {
System.out.println("Your total price is $" + ((acc * 100 + genderCost) / 2) + ".");
} else
System.out.println("Your total price is $" + (acc * 100 + genderCost) + ".");
}
Put the amount for gender in a variable genderCost when the gender input variable is evaluated and use genderCost when you calculate the total.

If else statement in Java [duplicate]

This question already has answers here:
Semicolon at end of 'if' statement
(18 answers)
Closed 6 years ago.
Need help with if else statement in Java. Need the program to say "Sorry, out of stock" when items are at 0. I tried but it wont print out "Sorry, out of stock" Can anyone explain to me how to properly set that up so when the items are at 0 the program will let the user know that the item is out of stock. Thank you.
import java.util.Scanner;
public class VendingMachine {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int Chips = 5;
int Cookies = 4;
int Candies = 3;
double ChipsPrice = 1.25;
double CookiesPrice = 0.85;
double CandiesPrice = 0.95;
Scanner choice = new Scanner(System.in);
Scanner moneyIn = new Scanner(System.in);
while (true) {
double Change = 0;
double Amount = 0;
double Money = 0;
System.out.println("Welcome to the Vending Machine");
System.out.println("Please insert Money");
Amount = moneyIn.nextDouble();
//Make an if statements, such as if moneyIn equals 5 quarters then Amount = 5*0.25
//Ask how many quarters how many nickels how many dimes
System.out.println("What snack would you like?");
System.out.println("Potato Chips: $" + ChipsPrice + " " + Chips + " left");
System.out.println("Cookies: $" + CookiesPrice + " " + Cookies + " left");
System.out.println("Candies: $" + CandiesPrice + " " + Candies + " left");
String which = choice.nextLine();
if (which.equals("Potato Chips")) {
System.out.println("You selected Potato Chips: $" + ChipsPrice + " " + Chips + " left");
if (Amount < ChipsPrice) {
System.out.println("Not enough money inserted");
if (Chips == 0) ;
System.out.println("Sorry, out of stock");
} else {
Chips = Chips - 1;
Change = ChipsPrice - Amount;
System.out.println("Please take your chips ");
System.out.println("Your change is " + Change);
}
} else if (which.equals("Cookies")) {
System.out.println("You selected Cookies: $" + CookiesPrice + " " + Cookies + " left");
Cookies = Cookies - 1;
if (Amount < CookiesPrice) {
System.out.println("Not enough money inserted");
if (Cookies == 0)
System.out.println("Sorry, out of stock");
} else {
Cookies = Cookies - 1;
Change = CookiesPrice - Amount;
System.out.println("Please take your cookies");
System.out.println("Your change is " + Change);
}
} else if (which.equals("Candies")) {
System.out.println("You selected Candies: $" + CandiesPrice + " " + Candies + " left");
if (Amount < CandiesPrice) {
System.out.println("Not enough money inserted");
if (Cookies == 0)
System.out.println("Sorry, out of stock");
} else {
Candies = Candies - 1;
Change = CookiesPrice - Amount;
System.out.println("Please take your candies");
System.out.println("Your change is " + Change);
}
} else {
System.out.println("Please select one of the snacks below");
}
}
}
}
Just to go through this, a few observations:
// It might be simpler to use a "switch" statement here
if (which.equals("Potato Chips")) {
System.out.println("You selected Potato Chips: $"+ChipsPrice+" "+Chips+" left");
if (Amount < ChipsPrice){
System.out.println("Not enough money inserted");
// Remove the semicolon - as written this won't do anything
// Also, this condition shouldn't be here since you're not vending anyway
// Incidentally, many people argue that you should always use curly
// brackets, even around one-line "if" statements like this, precisely
// to prevent errors like this
if (Chips == 0);
System.out.println("Sorry, out of stock");
}
else {
// This can be written as Chips--;
Chips = Chips - 1;
// Should actually be Amount - ChipsPrice;
// If they paid 75 cents for a 25-cent item, the change is 75 - 25 = 50 cents,
// NOT 25 - 75 = -50 cents
Change = ChipsPrice - Amount;
System.out.println("Please take your chips " );
System.out.println("Your change is "+ Change );
}
}
else if (which.equals("Cookies")) {
System.out.println("You selected Cookies: $"+CookiesPrice+" "+Cookies+" left");
// Cookies--
Cookies = Cookies - 1;
if (Amount < CookiesPrice){
System.out.println("Not enough money inserted");
// Should be checked in the "else" statement
if (Cookies == 0)
System.out.println("Sorry, out of stock");
}
else {
// Cookies--
Cookies = Cookies - 1;
// Amount - CookiesPrice
Change = CookiesPrice - Amount;
System.out.println("Please take your cookies");
System.out.println("Your change is "+ Change );
}
}
else if (which.equals("Candies")) {
System.out.println("You selected Candies: $"+CandiesPrice+" "+Candies+" left");
if (Amount < CandiesPrice){
System.out.println("Not enough money inserted");
// Again, you shouldn't check this here given that you won't vend either way
// Also, should be if (Candies == 0), NOT if (Cookies == 0)
if (Cookies == 0)
System.out.println("Sorry, out of stock");
}
else {
// Candies--;
Candies = Candies - 1;
// Should actually be Amount - CandyPrice. You use CookiesPrice instead.
Change = CookiesPrice - Amount;
System.out.println("Please take your candies");
System.out.println("Your change is "+ Change );
}
}
else {
System.out.println("Please select one of the snacks below");
}
One more thing: you're basically doing the same exact thing 3 consecutive times; in situations like that, it's usually better to try to refactor the behavior in question as a method (rather than typing it 3 separate times).

Categories