jeudi 21 novembre 2019

Changing outputs on a textual menu that uses a switch statement

Using Java I have a system class that has different methods for displaying details of a computer system. I then have a test system class that calls these methods as a menu using a switch statement. If the user enters choice 1 or 2 then it displays some input that I have hard coded. But if they choose choice three then they are promoted to enter their own input.

How do I then get choice 1 and 2 to show their input instead of mine if they enter it in choice 3 and then enter choice 1 and 2 again?

I’m a complete newbie with this and I’m not sure how I would do this, I have researched online and in books and was thinking maybe an if else statement somewhere, but I could be wrong. Any advice would be appreciated.

Here's the code I have:

import java.util.Scanner;

public class SystemTest_Y3881268 {

public static void main(String[] args) {
    //Create System_Y3881268 object and test methods
    System_Y3881268 s=new System_Y3881268("Lenovo", 
            "Ideacentre A340-24IWL", 2);
    s.setHardDisk(2);
    s.setMemory(128);
    s.setPurchaseCost(599);

    //Create textual menu
    int memorySize;
    double hardDiskSize;

    @SuppressWarnings("resource")
    Scanner keyboard = new Scanner(System.in);
    char choice;
    do 
    {
        System.out.println();
        System.out.println("***** Computer system menu *****");
        System.out.println();
        System.out.println("Choice 1: Print System Details");
        System.out.println("Choice 2: Diagnose System");
        System.out.println("Choice 3: Set Details");
        System.out.println("Choice 4: Print System Properties");
        System.out.println("Choice 5: Quit the Program");
        System.out.println();
        System.out.println("Enter a number from 1 - 5");
        System.out.println();
        choice = keyboard.next().charAt(0);
        switch(choice) 
        {

        case '1': 
        {
            s.displayDetails();
        }
        break;

        case '2': 
        {
            s.diagnoseSystem();
        } 
        break;

        case '3': 
        {
            System.out.println("Enter hard disk size in GB: ");
            hardDiskSize = keyboard.nextDouble();
            if(hardDiskSize<2) 
            {
                System.out.println("Hard disk size = Low");
            }

            else 
            {
                System.out.println("Hard disk size = Ok");
            }

            System.out.println();
            System.out.println("Enter memory size in MB: ");
            memorySize = keyboard.nextInt();
            if(memorySize<128) 
            {
                System.out.println("Memory Ok = False");
            }

            else 
            {
                System.out.println("Memory Ok = True");
            }


        }
        break;

        case '4' : 
        {
            System_Y3881268.displaySystemProperties();
        }
        break;

        case '5' : break;
        default :  System.out.println("Enter only numbers from 1 - 5");
                   System.out.println();
        }
    } while(choice != '5');




}

}

Aucun commentaire:

Enregistrer un commentaire