Search This Blog

Monday, January 23

!ANSWER! Assignment 3

Here it comes the ANSWER TO the difficult number 3!...

Are you ready?

BRACE YOURSELF....its going to be a long and bumpy one!

import java.io.*;
import java.util.*;

public class BajrovicAssignment3 {
 static ArrayList<Computer> computers = new ArrayList<Computer>();
 
 public static void main(String[] args){
 
 while(true){
  Scanner menuin = new Scanner(System.in);
  String[] menu = new String[6];
  menu[0] = "Choose one of the following options:";
  menu[1] = "i (Input Computer info)";
  menu[2] = "d (Display Computer info)";
  menu[3] = "s (Start a computer)";
  menu[4] = "u (Shutdown a computer)";
  menu[5] = "q (Quit Program)";
 for (String m : menu) //prints the entire array with only 1 system.out
  System.out.println(m);
 String menuchoice = menuin.nextLine();
 
 if (menuchoice.equals("i")){
   Scanner compmenuin = new Scanner(System.in);
   String[] compmenu = new String[3];
   compmenu[0] = "Choose one of the computer types listed below:";
   compmenu[1] = "1. desktop computer";
   compmenu[2] = "2. notebook computer";
  for (String c : compmenu) //prints the entire array with only 1 system.out
   System.out.println(c);
  int compchoice = compmenuin.nextInt();
   switch (compchoice){
    case 1:
     desktopinput();
     break;
    case 2:
     notebookinput(); 
     break;
    }  
 }
 else if (menuchoice.equals("d")){
  Scanner index = new Scanner(System.in);
  System.out.print("Enter a computer number to be displayed: ");
  int ind = index.nextInt() ;
  
  System.out.print(computers.get(ind));
  
  if (computers.size() == 0){
   System.out.println("\nThere are no computers created.\n");
  }

  }
 
 else if (menuchoice.equals("s")){
  if (computers.size()==0){
   System.out.println("You haven't created any computers.");
  }
  else{
  Scanner index = new Scanner(System.in);
  System.out.print("Enter a computer number to be started: ");
  int ind = index.nextInt() ;
  Computer starter = computers.get(ind);
  
  if (starter.getpower() == false){
  starter.start();
  System.out.format("%s %s has been started.\n", starter.getmanufacturer(), starter.getmodel());
  }
  else if (starter.getpower() == true){
   System.out.println("This computer is already on.");
  }
   
 }
 }
 else if (menuchoice.equals("u")){
  if (computers.size()==0){
   System.out.println("You haven't created any computers.");
  }
  else{
  Scanner index = new Scanner(System.in);
  System.out.print("Enter a computer number to be started: ");
  int ind = index.nextInt() ;
  Computer shutter = computers.get(ind);
  
  
  if (shutter.getpower() == true){
  shutter.shutdown();
  System.out.format("Shutting Down\n");
  }
  else if (shutter.getpower() == false){
   System.out.println("This computer is already off.\n");
  }
  }
 }
 else if (menuchoice.equals("q")){
  System.exit(0);
 }
 
 else{
    System.out.println("Invalid Selection.");
   }
  }
 } 
//  }

 public static void desktopinput(){
  Desktop c1 = new Desktop (null, null, 0, null, 0, 0);
  CPU c3 = new CPU (null, 0, 0, 0);
  
  
  Scanner compin = new Scanner(System.in);
  System.out.println("Enter manufacturer:");
  String manufacturer = compin.nextLine();
  c1.setmanufacturer(manufacturer);
  System.out.println("Enter model:");
  String model = compin.nextLine();
  c1.setmodel(model);
  System.out.println("Enter CPU type:");
  String CPUtype = compin.nextLine();
  c3.settype(CPUtype);
  System.out.println("Enter CPU speed (GHZ):");
  float CPUspeed = compin.nextFloat();
  c3.setspeed(CPUspeed);
  System.out.println("Enter CPU cache (MuB):");
  int cache = compin.nextInt();
  c3.setcache(cache);
  System.out.println("Enter FSB (MHZ):");
  int FSB = compin.nextInt();
  c3.setFSBe(FSB);
  System.out.println("Enter hard drive capacity (GB):");
  int drivecap = compin.nextInt();
  c1.sethardDrive(drivecap);
  System.out.println("Enter color:");
  String color = compin.next();
  c1.setcolor(color);
  System.out.println("Enter raid level:");
  int raid = compin.nextInt();
  c1.setraid(raid);
  System.out.println("Enter price ($):");
  double price = compin.nextDouble();
  c1.setprice(price);
  
  c1.setCPU(c3);
  computers.add(c1);
  System.out.print(computers.get(0));
//  System.out.format("%s %s has been created.", c1.getmanufacturer(), c1.getmodel());
 }
 public static void notebookinput(){
  Desktop c1 = new Desktop (null, null, 0, null, 0, 0);
  CPU c3 = new CPU (null, 0, 0, 0);
  
  
  Scanner compin = new Scanner(System.in);
  System.out.println("Enter manufacturer:");
  String manufacturer = compin.nextLine();
  c1.setmanufacturer(manufacturer);
  System.out.println("Enter model:");
  String model = compin.nextLine();
  c1.setmodel(model);
  System.out.println("Enter CPU type:");
  String CPUtype = compin.nextLine();
  c3.settype(CPUtype);
  System.out.println("Enter CPU speed (GHZ):");
  float CPUspeed = compin.nextFloat();
  c3.setspeed(CPUspeed);
  System.out.println("Enter CPU cache (MuB):");
  int cache = compin.nextInt();
  c3.setcache(cache);
  System.out.println("Enter FSB (MHZ):");
  int FSB = compin.nextInt();
  c3.setFSBe(FSB);
  System.out.println("Enter hard drive capacity (GB):");
  int drivecap = compin.nextInt();
  c1.sethardDrive(drivecap);
  System.out.println("Enter color:");
  String color = compin.next();
  c1.setcolor(color);
  System.out.println("Enter raid level:");
  int raid = compin.nextInt();
  c1.setraid(raid);
  System.out.println("Enter price ($):");
  double price = compin.nextDouble();
  c1.setprice(price);
 
  c1.setCPU(c3);
  computers.add(c1);
  
  System.out.format("%s %s has been created.", c1.getmanufacturer(), c1.getmodel());
 } 
}

Now, because this assignment requires you to create separate classes to handle the storage & processing of the different types of user input (Computer, CPU, etc.) I will post the code for each class separately to avoid having JUMBO posts. (She says after posting ~200 lines of code)

I will post each class daily this week in this section so that next Monday you will have Assignment 4!!! (Only 2 more to go)

HAPPY CODING!

No comments:

Post a Comment