Question

For a program i am writing for school one of the extensions says i need to figure a way to to edit a case statement if i enter as an admin, I have created two methods because it seemed like the best way to do this.

method one (the one i need to edit):

public static double costSqM(Console c) {
    double price = 0;
    int choice;
    c.println("#. Type               Cost per sq.m.");
    c.println("1. Low pile carpet           $ ");
    c.print(pileCost, 5, 2);
    c.println("2. Shag rug                  $ ");
    c.print(rugCost, 5, 2);
    c.println("3. Parquet                   $ ");
    c.print(parquetCost, 5, 2);
    c.println("4. Linleum                   $ ");
    c.print(leumCost, 5, 2);
    c.println("5. Hardwood                  $ ");
    c.print(hardwoodCost, 5, 2);
    c.println("Please enter your choice: ");
    choice = c.readInt();
    while (choice < 1 && choice > 5) {
        c.println("That is not a valid choice. Please try again!");
        choice = c.readInt();
    }
    switch (choice) {
    case 0:
        admin(c);
    case 1:
        price = 18.75;
    case 2:
        price = 11.05;
    case 3:
        price = 14.35;
    case 4:
        price = 10.40;
    case 5:
        price = 28.15;
    }
    return price;
}

method two (the one I'm editing from):

public static void admin(Console c) {
    int choice = 0, priceChange = 0;
    cls(c);
    c.println("Hello admin. What changes would you like to make?");
    c.println("1. Change a price.");
    c.println("2. Add an item.");
    c.println("3. Remove an item.");
    switch(choice){
    //Changing the price of an item
    case 1: 
        c.print("What price would you like to change? ");
        c.println("1. Low Pile Carpet.");
        c.println("2. Shag Rug.");
        c.println("3. Parquet.");
        c.println("4. Linleum.");
        c.println("5. Hardwood.");
        switch(priceChange){
        case 1:
            c.println("Please enter the new price for the Low Pile Carpet: ");
            pileCost = c.readInt();
        case 2:
            c.println("Please enter the new price for the Shag Rug: ");
            rugCost = c.readInt();
        case 3:
            c.println("Please enter the new price for the Parquet: ");
            parquetCost = c.readInt();
        case 4:
            c.println("Please enter the new price for the Linluem: ");
            leumCost = c.readInt();
        case 5:
            c.println("Please enter the new price for the Hardwood: ");
            hardwoodCost = c.readInt();
        }
    //Adding an item
    case 2:
    //Removing an item
    case 3:

    }

If it is not possible to do from another method but it is from the same method please explain that too and i can just make it one big method. Thanks.

Was it helpful?

Solution

You could use a flag for the admin mode. set it to true and you're in admin mode:
boolean admin = true.

Then you could simply check if admin is true and if that is the case, print admin stuff, if not... print those normal outputs.

Example in case the user typed 1:

case 1:
    if(admin)
    {  //admin, change price
        c.println("Please enter the new price for the Low Pile Carpet: ");
        pileCost = c.readInt();
    }
    else
    { //non-admin, set price
        price = 18.75;
    }
break; //important! don't forget the break-keyword

If you forget to add break at the end of a case-statement all of the following statements (e.g. for 2: 3,4,5..) will run.

That should be an inspiration, you should figure out the rest yourself :)

Oh of course you will end up with only 1 method this way, which is smaller than the two before combined since logic isn't duplicated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top