Member Avatar for OzY360

I'm having trouble getting the program to loop back to the menu once I have created a new computer/customer. Once I figure it out I can do it for all the functions in the menu. This program is not yet complete but any guidance as to why my program won't loop back would be much appreciated. It allows me to carry on and create a new computer/customer but not to go back to main menu to access the other functions like search/match/sort which I still haven't done.

FYI this was done in the latest version of Netbeans (6.8).

/* Name: 
 * Student ID: 
 * Date: 19/12/09
 * 2AIT515 Assignment
 * Program which simulates an online shop
 * selling computers with different characteristics
 */
import java.util.*;
import java.io.*;

public class computershop {

    public static void main(String[] args)throws IOException{
    final BufferedReader stdin = new BufferedReader ( new InputStreamReader( System.in ) );

    /* Variables for Menu System and Sub Menu System */
    String MainMenu;
    String SubMenu;
    String ReturnToMenu;
    String SubMenuReturnToMenu;

     /* Displays menu system to console */
     System.out.println("..............MENU...............");
     System.out.println("..............C New computer .................");
     System.out.println("..............N New customer..............");
     System.out.println("..............S Search...................");
     System.out.println("..............M Match...................");
     System.out.println("..............O order/sort...................");
     System.out.println("..............L list/sort...................");
     System.out.println("Please select an option:");
     Scanner sc = new Scanner(System.in);
     MainMenu = sc.next();

 /* Option 'c' creates a new computer (laptop or desktop
 * with specific characteristics entered by the user */
      if (MainMenu.equals("c")){
      System.out.println("");
      System.out.println("You have created a new computer, please define the hardware specification.");
      System.out.println("");

     Computer Computer = new Computer() {};
     Computer.description();
     System.out.println("");
     System.out.println("l = laptop & d = Desktop");
     System.out.println("Please select a Laptop or Desktop:");
     Scanner sc2 = new Scanner(System.in);
     SubMenu = sc2.next();

     while (SubMenu.equals("l")){
     System.out.println("");
     System.out.println("");
     Laptop laptop = new Laptop();
     laptop.CpuType();
     laptop.CpuSpeed();
     laptop.Ram();
     laptop.HardDrive();
     laptop.Graphics();
     laptop.ScreenSize();
     laptop.Weight();
     laptop.BatteryLife();
     laptop.totalprice();
     laptop.OverAllSpec();

    System.out.println("");
    System.out.println("type 1: continue with new laptop or type 2: to return menu");
    Scanner sc4 = new Scanner(System.in);
    
    ReturnToMenu = sc4.next();
    if(ReturnToMenu.equals("2")){
    return;
    }
    }

    while (SubMenu.equals("d")){
    System.out.println("");
    System.out.println("");
    Desktop desktop = new Desktop();
    desktop.CpuType();
    desktop.CpuSpeed();
    desktop.Ram();
    desktop.HardDrive();
    desktop.Graphics();
    desktop.ScreenSize();
    desktop.totalprice();
    desktop.OverAllSpec();

    System.out.println("");
    System.out.println("type 1: continue with new Desktop or type 2: to return menu");

    Scanner sc4 = new Scanner(System.in);
    SubMenuReturnToMenu = sc4.next();
    if(SubMenuReturnToMenu.equals("2")){
    return;
    }
     }
  }

/* Option 'n' creates a new customer looking for a computer */

    if (MainMenu.equals("n")){
    System.out.println("New Customer");
    Customer customer = new Customer();
    customer.Name();
    customer.Address();
    customer.TelephoneNo();

    ArrayList<String> listA = new ArrayList<String>();
    listA.add(customer.Name);
    listA.add(customer.Address);
    listA.add(customer.TelephoneNo);
    System.out.println(listA);
    }

    
/* Option 's' (search) gives a user the possibility to search
 * for a particular type a computer already existing in the system */
     if (MainMenu.equals("s")){
     System.out.println("search");
    }

/* Option 'm' (match) matches all customers in the system with a single
 * computer in the sytem based on the needs of a customer and the
 * computer characteristics
 */
     if (MainMenu.equals("m")){

     System.out.println("match");
    }

/* Option 'o' (order or otherwise sort computers) displays all existing
 * customers according to price in descending order */
     if (MainMenu.equals("o")){

     System.out.println("order/sort");
    }

/* Option 'l' (list or otherwise sort customers) displays all existing customers
* according to requirements of a computer with CPU speed in ascending order */
       if (MainMenu.equals("l")){

       System.out.println("list/sort");
    }
    }
    }

    abstract class Computer{
      float cpu;
      int cpuPrice;
      String CpuType;
      int Ram;
      int RamPrice;
      int Graphics;
      int GraphicsPrice;
      int HardDrive;
      int HardDrivePrice;
      float Weight;
      int BatteryLife;
      int ScreenSize;


        public static void description(){

        System.out.println("CPU");
        System.out.println("RAM");
        System.out.println("Hard drive");
        System.out.println("Graphics card");
        System.out.println("Weight");
        System.out.println("Battery Life");
        System.out.println("Screen Size");

           }
    }

     class Laptop extends Computer{

        public void CpuType(){
                System.out.println("");
                System.out.println("CPU Type : Celeron, Core 2 Duo, Pentium 4, Core 2 Quad");
                System.out.println("Please select a CPU Type:");
                Scanner sc = new Scanner(System.in);
                CpuType = sc.next();
        }

        public void CpuSpeed(){
            System.out.println("");
            System.out.println("Clock Speed: 0GHz – 2.0GHz, 2.0GHz – 3.0GHz, 3.0GHz+");
            System.out.println("Please select CPU Clock Speed:");
            Scanner sc = new Scanner(System.in);
            cpu = sc.nextFloat();
              if (cpu == 0 && cpu <= 2){
              cpuPrice = 30;
             System.out.print("Cost of CPU: " + "£");
              System.out.println("30");
              }
            else if (cpu == 2 && cpu <= 3){
              cpuPrice = 120;
                  System.out.print("Cost of CPU: " + "£");
              System.out.println("120");
              }
            else if (cpu > 3){
            cpuPrice = 250;
                System.out.print("Cost of CPU: " + "£");
              System.out.println("250");
            }
        }

         public void Ram(){
            System.out.println("");
            System.out.println("RAM: <1GB, 1GB, 2GB, 3GB, 4GB, 6GB, 8GB");
            System.out.println("Please select Capacity you want in Giga Bytes:");
            Scanner sc2 = new Scanner(System.in);
            Ram = sc2.nextInt();
            if (Ram < 1){
              RamPrice = 15;
              System.out.print("Cost of RAM : " + "£");
              System.out.println("15");
            }
            else if (Ram == 1){
              RamPrice = 25;
              System.out.print("Cost of Ram : " + "£");
              System.out.println("25");
            }
            else if (Ram == 2){
            RamPrice = 35;
            System.out.print("Cost of Ram : " + "£");
              System.out.println("35");
            }
            else if (Ram == 3){
            RamPrice = 50;
            System.out.print("Cost of Ram : " + "£");
            System.out.println("50");
            }
         }

         public void HardDrive(){
            System.out.println("");
            System.out.println("Hard Drive Capacity: 0GB – 160GB, 161GB - 320GB, 321GB – 500GB, 500 – 1TB, >1TB");
            System.out.println("Please select the Hard Drive Capacity you want:");
            Scanner sc3 = new Scanner(System.in);
            HardDrive = sc3.nextInt();

            if (HardDrive == 0 && HardDrive <= 160){
              HardDrivePrice = 30;
              System.out.print("Cost of HardDrive : " + "£");
              System.out.println("30");
            }
            else if (HardDrive > 160 && HardDrive <= 320){
              HardDrivePrice = 60;
              System.out.print("Cost of HardDrive : " + "£");
              System.out.println("60");
            }
            else if (HardDrive > 320 && HardDrive <= 500){
              HardDrivePrice = 80;
              System.out.print("Cost of HardDrive : " + "£");
              System.out.println("80");
            }

            else if (HardDrive >= 500 && HardDrive <= 1){
              HardDrivePrice = 100;
              System.out.print("Cost of HardDrive : " + "£");
              System.out.println("100");
            }
            else if (HardDrive > 1){
            HardDrivePrice = 250;
            System.out.print("Price HardDrive : " + "£");
              System.out.println("160");
            }
         }

          public void Graphics(){
            System.out.println("");
            System.out.println("Memory Size: <128MB, 256MB – 512MB, 512MB – 1024MB, 1024MB, 1792MB, 1GB and above");
            System.out.println("Please select the memory size for your graphics card:");
            Scanner sc4 = new Scanner(System.in);
            Graphics = sc4.nextInt();
             
              if (Graphics < 128){
              GraphicsPrice = 50;
              System.out.print("Cost of Graphics Card: " + "£");
              System.out.println("40");
            }
            else if (Graphics == 256 && Graphics <= 512){
              GraphicsPrice = 75;
              System.out.print("Cost of Graphics Card: " + "£");
              System.out.println("75");
            }
            else if(Graphics >= 512 && Graphics <=1024){
            GraphicsPrice = 100;
            System.out.print("Cost of Graphics Card: " + "£");
              System.out.println("100");
            }

          }

           public void Weight(){
            System.out.println("");
            System.out.println("Please enter the maximum weight");
            Scanner sc5 = new Scanner(System.in);
            Weight = sc5.nextFloat();
           }

           public void ScreenSize(){
              System.out.println("");
              System.out.println("Screen Size: 10, 13, 15, 17");
              System.out.println("Please select a screen size");
              Scanner sc7 = new Scanner(System.in);
              ScreenSize = sc7.nextInt();
              switch (ScreenSize){
                  case 1: ScreenSize = 10;
                  break;
                  case 2: ScreenSize = 13;
                  break;
                  case 3: ScreenSize = 15;
                  break;
                  case 4: ScreenSize = 17;
                  break;
              }
           }

            public void BatteryLife(){
            System.out.println("");
            System.out.println("Battery Life: 2.5hrs, 3hrs, 5hrs, 7hrs, 9hrs");
            Scanner sc6 = new Scanner(System.in);
            BatteryLife = sc6.nextInt();

            if (BatteryLife == 2.5){
            System.out.print("Battery Life is 2.5 Hours");
              }
            else if (BatteryLife == 3){
            System.out.print("Battery Life is 3 Hours");
            }
            else if (BatteryLife == 5){
            System.out.print("Battery Life is 5 Hours");
            }
            else if (BatteryLife == 7){
            System.out.print("Battery Life is 7 Hours");
            }
            else if (BatteryLife == 9){
            System.out.print("Battery Life is 9 Hours");
            }
            }

          public void totalprice(){
              System.out.println("");
              System.out.print("Price Altogether : " + "£");
              System.out.println(cpuPrice + GraphicsPrice + HardDrivePrice +
              RamPrice);
          }

               public void OverAllSpec(){
                System.out.print("Cpu Type : ");
                System.out.println(CpuType);
                System.out.print("Cpu Speed : ");
                System.out.println(cpu + "GHz");
                System.out.print("RAM : ");
                System.out.println(Ram + "GB");
                System.out.print("HardDrive : ");
                System.out.println(HardDrive + "GB");
                System.out.print("Graphics : ");
                System.out.println(Graphics + "MB");
                System.out.print("Weight : ");
                System.out.println(Weight + "Kg");
                System.out.print("Battery Life : ");
                System.out.println(BatteryLife + "hours");
                System.out.print("ScreenSize : ");
                System.out.println(ScreenSize + "Inches");
           }
     }

  class Desktop extends Computer{

            public void CpuType(){
             System.out.println("");
             System.out.println("CPU Type : Pentium 4, Celeron, Core 2 Duo, Core 2 Quad, Core i7");
             System.out.println("Please select a CPU Type:");
             Scanner sc = new Scanner(System.in);
             CpuType = sc.next();
        }

            public void CpuSpeed(){
            System.out.println("");
            System.out.println("Clock Speed: 0GHz – 2.0GHz, 2.0GHz – 3.0GHz, 3.0GHz+");
            System.out.println("Please select CPU Clock Speed:");
            Scanner sc = new Scanner(System.in);
            cpu = sc.nextFloat();
            if (cpu < 2.5 && cpu > 1){
            cpuPrice = 40;
            System.out.print("Cost of CPU : " + "£");
            System.out.println("40");
            }
              if (cpu == 0 && cpu <= 2){
              cpuPrice = 30;
              System.out.print("Cost of CPU: " + "£");
              System.out.println("30");
              }
              else if (cpu == 2 && cpu <= 3){
              cpuPrice = 120;
              System.out.print("Cost of CPU: " + "£");
              System.out.println("120");
              }
              else if (cpu > 3){
              cpuPrice = 250;
              System.out.print("Cost of CPU: " + "£");
              System.out.println("250");
            }

        }

         public void Ram(){
            System.out.println("");
            System.out.println("RAM: <1GB, 1GB, 3GB, 4GB, 6GB, 8GB, 12GB, 16GB, 24GB, 32GB, 64GB");
            System.out.println("Please select Capacity you want in Giga Bytes:");
            Scanner sc2 = new Scanner(System.in);
            Ram = sc2.nextInt();
              if (Ram < 1){
              RamPrice = 15;
              System.out.print("Cost of RAM : " + "£");
              System.out.println("15");
            }
              else if (Ram == 1){
              RamPrice = 25;
              System.out.print("Cost of Ram : " + "£");
              System.out.println("25");
            }
            else if (Ram == 2){
            RamPrice = 35;
            System.out.print("Cost of Ram : " + "£");
              System.out.println("35");
            }
            else if (Ram == 3){
            RamPrice = 50;
            System.out.print("Cost of Ram : " + "£");
            System.out.println("50");
            }

         }

         public void HardDrive(){
            System.out.println("");
            System.out.println("Hard Drive Capacity: 0GB – 160GB, 161GB - 320GB, 321GB – 500GB, 500 – 1TB, >1TB");
            System.out.println("Please select the Hard Drive Capacity you want:");
            Scanner sc3 = new Scanner(System.in);
            HardDrive = sc3.nextInt();

              if (HardDrive == 0 && HardDrive <= 160){
              HardDrivePrice = 30;
              System.out.print("Cost of HardDrive : " + "£");
              System.out.println("30");
            }
              else if (HardDrive > 160 && HardDrive <= 320){
              HardDrivePrice = 60;
              System.out.print("Cost of HardDrive : " + "£");
              System.out.println("60");
            }
              else if (HardDrive > 320 && HardDrive <= 500){
              HardDrivePrice = 80;
              System.out.print("Cost of HardDrive : " + "£");
              System.out.println("80");
            }

              else if (HardDrive >= 500 && HardDrive <= 1){
              HardDrivePrice = 100;
              System.out.print("Cost of HardDrive : " + "£");
              System.out.println("100");
            }
              else if (HardDrive > 1){
              HardDrivePrice = 250;
              System.out.print("Cost of HardDrive : " + "£");
              System.out.println("160");
            }


         }

          public void Graphics(){
            System.out.println("");
            System.out.println("GPU Memory Size: <128MB, 256MB – 512MB, 512MB – 1024MB, 1024MB, 1792MB, 1GB and above");
            System.out.println("Please select the memory size for your graphics card:");
            Scanner sc4 = new Scanner(System.in);
            Graphics = sc4.nextInt();

              if (Graphics < 128){
              GraphicsPrice = 50;
              System.out.print("Cost of Graphics Card: " + "£");
              System.out.println("40");
            }
              else if (Graphics == 256 && Graphics <= 512){
              GraphicsPrice = 75;
              System.out.print("Cost of Graphics Card: " + "£");
              System.out.println("75");
            }
              else if(Graphics >= 512 && Graphics <= 1024){
              GraphicsPrice = 100;
              System.out.print("Cost of Graphics Card: " + "£");
              System.out.println("100");
            }
          }
              public void ScreenSize(){
              System.out.println("");
              System.out.println("Screen Size: 15, 17, 19, 21, 24");
              System.out.println("Please select a screen size");
              Scanner sc5 = new Scanner(System.in);
              ScreenSize = sc5.nextInt();
              switch (ScreenSize){
                  case 1: ScreenSize = 15;
                  break;
                  case 2: ScreenSize = 17;
                  break;
                  case 3: ScreenSize = 19;
                  break;
                  case 4: ScreenSize = 21;
                  break;
                  case 5: ScreenSize = 24;
                  break;
              }
              }

          public void totalprice(){
              System.out.println("");
              System.out.print("Price Altogether: " + "£");
              System.out.println(cpuPrice + GraphicsPrice + HardDrivePrice + RamPrice);
          }
          
          public void OverAllSpec(){
               System.out.print("Cpu Type : ");
                System.out.println(CpuType);
                System.out.print("Cpu Speed : ");
                System.out.println(cpu + "GHz");
                System.out.print("RAM : ");
                System.out.println(Ram + "GB");
                System.out.print("HardDrive : ");
                System.out.println(HardDrive + "GB");
                System.out.print("Graphics : ");
                System.out.println(Graphics + "MB");
                System.out.print("ScreenSize : ");
                System.out.println(ScreenSize + "Inches");

           }
     }


abstract class Person{
     String Name;
     String Address;
     String TelephoneNo;

     public void Name(){
        System.out.println("Please enter your full name");
        Scanner sc = new Scanner(System.in);
        Name = sc.next();
        }
     public void Address(){
        System.out.println("Please enter your current address");
        Scanner sc = new Scanner(System.in);
        Address = sc.next();
        }

     public void TelephoneNo(){
        System.out.println("Please enter your Telephone Number");
        Scanner sc = new Scanner(System.in);
        TelephoneNo = sc.next();
     }
}

    class Customer extends Person{
      public void NewComputer(){
      System.out.println("Create New Computer");
       }
      public void NewCustomer(){
      System.out.println("Create New Customer");
      }
      public void Search(){
      System.out.println("Please enter a search");
      }
      public void Match(){
      System.out.println("Match a computer based on your needs");
            }
      public void Order(){
          System.out.println("Display all existing computers according to price in descending order");
      }
      public void List(){
      System.out.println("List computers in ascending order according to CPU Speed");
      }
    }

Recommended Answers

All 2 Replies

You need to place the whole menu handing (from line 22) inside a loop.
Most easily you would use a while(true) { ... } loop, and add a menu option to exit from the program.

Member Avatar for OzY360

Cheers for the reply, I will definitely keep that in mind. I'm sorting out option menu N at the moment because its wrong lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.