简体   繁体   中英

How to go to a different class carry out code and return to previous class

I'm currently in my second year of college. I'm finding the year very hard and don't see myself passing. But any way. I'm currently working on a Java project. The project is based on the National Car test (NCT).

Basically what I want to do is have a choice of numbers to carry out a full test or a retest. If the user selects a full test I then want to go to the fullTest class and carry out some question starting with personal information, then car details then car test question. Eg Oil level ok? Y/N

What I want to know is how do I go to the fulltest class run through the code and then ether display the results from fulltest class of return the result to mainNct.

package Nct;


import java.util.Scanner;

public class MainNCT 
{

public static int choice = -1;

public static void main( String[] args) 
{

    Scanner Console = new Scanner(System.in);


    System.out.println("Menu\n\t1. Full Test\n\t2. Re-test\n\t0. Exit\n");

    System.out.print("Enter a number: ");
    choice = Console.nextInt();

    switch(choice)
    {
        case 1:
            //Go to fulltest class
            break;
        case 2:
            //Go to retest class
            break;
        case 0:
            System.exit(0);
            break;
        default:
            System.out.println("Invalid number entered");
    } // switch



}

}

And

package Nct;

import java.util.Scanner;

public class FullTest extends MainNCT {

int wheelAliResult = 0;
String wheelResult;

public FullTest() {

    Scanner Console = new Scanner(System.in);
    //Questions here

    System.out.print("Wheel alingment (%)? ");
    wheelAliResult = Console.nextInt();

    if(wheelAliResult < 0 || wheelAliResult > 6)
    {
        wheelResult = "Fail";
    }
    else
    {
        wheelResult = "Pass";
    }


}

}

Base on the code you have so far, since the logic seems to all reside in your FullTest constructor, this should do it:

switch(choice)
{
    case 1:
        FullTest ft = new FullTest();
        break;
    case 2:
        ReTest rt = new ReTest();
        break;
    case 0:
        System.exit(0);
        break;
    default:
        System.out.println("Invalid number entered");
} // switch

I'm assuming you have a class called ReTest too.

First of all make a method such as boolean chkOilLevel() in FullTest class, that will return you true or false after evaluation as:

boolean chkOilLevel(){
Scanner Console = new Scanner(System.in);
//Questions here

System.out.print("Wheel alingment (%)? ");
wheelAliResult = Console.nextInt();

if(wheelAliResult < 0 || wheelAliResult > 6)
{
    wheelResult = true;
}
else
{
    wheelResult = false;
}
return wheelResult;
}

You can call such method from MainNCT by making an object of FullTest as

FullTest fullTest=new FullTest();

and can call this method in Your MainNCT class

        case 1:
           boolean oilLevel=fullTest.chkOilLevel();
           // Do Whatever you want with oilLevel
           break;

This thing will also help you in future...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM