Monday, July 2, 2012

Computer Management Example (for assignment 2)

here are the classes for the Computer Management example

Here is the Computer.java class

package com.spconger.www;

public class Computer {
 /**
  * This class stores information 
  * about a computer
  * steve conger 7/2/2012
  */
 
 //private fields
 private int computerID;
 private int gigsOfRam;
 private  int hardDiskSize;
 private String brand;
 
 //constructors
 public Computer(int ID, int ram, int hd, String name){
  setComputerID(ID);
  setGigsOfRam(ram);
  setHardDiskSize(hd);
  setBrandName(name);
  
 }
 
 public Computer(int ID){
  setComputerID(ID);
  setGigsOfRam(0);
  setHardDiskSize(0);
  setBrandName("");
  
 }
 
 public Computer(){
  setComputerID(0);
  setGigsOfRam(0);
  setHardDiskSize(0);
  setBrandName("");
 }
 
 // gets and sets
 public void setComputerID(int ID){
  computerID=ID;
 }
 
 public int getComputerID(){
  return computerID;
 }
 
 public void setGigsOfRam(int ram){
  gigsOfRam=ram;
 }
 
 public int getGigsOfRam(){
  return gigsOfRam;
 }
 
 public void setHardDiskSize(int hdisk){
  hardDiskSize=hdisk;
 }
 
 public int getHardDiskSize(){
  return hardDiskSize;
 }
 
 public void setBrandName(String name){
  brand=name;
 }
 
 public String getBrandName(){
  return brand;
 }
 
 // public methods
 
 public String getComputerInfo(){
  return Integer.toString(computerID) + ", " 
   +  brand + ", "
   + Integer.toString(gigsOfRam) + ", " 
   + Integer.toString(hardDiskSize);
 }
 
}



Here is the ComputerManager.java class

package com.spconger.www;

import java.util.ArrayList;

public class ComputerManager {
 /**
  * This class manages computers
  * it uses an ArrayList to collect computer
  * information. The class can return information
  * about a particular computer or return
  * the average size of hard disks
  * the class could do a lot more
  * but this is enough for the example
  * Steve Conger 7/1/2012
  */
 
 //private fields
 private ArrayList<Computer> computers;
 
 //constructor
 public ComputerManager(){
  //initialize the ArrayList
  computers=new ArrayList<Computer>();
 }
 
 //public methods
 public void AddComputer(Computer c){
  //add a computer the the ArrayList
  computers.add(c);
 }
 
 
 public String GetComputerById(int ID){
  //get a computer by its ID
  
  String computerInfo="computer not found";
  
  for(Computer c: computers){
   if(c.getComputerID()==ID){
    
    computerInfo=c.getComputerInfo();
    break;
   }
   
  }

  return computerInfo;
 }
 
 public double getAverageHardDiskSize(){
  //get the average HardDisk size
  //initialize variables
  double average=0;
  int sum=0;
  
  //for each computer in the ArrayList computers
  for(Computer c: computers){
   sum += c.getHardDiskSize();
  }
  //cast sum to double and divide
  //size is the length of the array
  average=(double)sum/computers.size();
  
  return average;
  
 }

}


Here is the Display.java

package com.spconger.www;

import java.text.DecimalFormat;
import java.util.Scanner;

public class Display {
 /**
  * This class handles the input and output 
  * for the Computer and ComputerManager classes
  * Steve Conger 7/2/2012
  */
 //declare class level variables
 ComputerManager cm;
 Scanner scan;
 
 //constructor
 public Display(){
  //initialize classes
  scan = new Scanner(System.in);
  cm= new ComputerManager();
  //call methods
  getInputs();
  showOutputs();
 }

 private void getInputs(){
  int exit=1;
  
  //loop to enter as many computers as desired
  while(exit !=-1){

  //get the values
  System.out.println("Enter Computer ID");
  int id=scan.nextInt();
  
  System.out.println("Hard Disk Size");
  int hd=scan.nextInt();
  
  System.out.println("Enter Ram size");
  int ram=scan.nextInt();
  
  System.out.println("Enter Computer Brand");
  String brandName=scan.next();
  
  //create the computer object and pass it
  //the values through the constructor
  Computer c = new Computer(id, ram, hd, brandName);
  //call the method to add the computer
  //to the ArrayList
  cm.AddComputer(c);
  
  //prompt for exit
  System.out.println("-1 to exit, anything other int to continue");
  exit=scan.nextInt();
  
  }//end while
  
 }//end inputs
 
 private void showOutputs(){
  //get the outputs
  System.out.println("***************************");
  System.out.println("Enter a computer id to get Computer");
  int id = scan.nextInt();
  
  String info=cm.GetComputerById(id);
  
  System.out.println(info);
  
  System.out.println("the average hard disk space is");
  double space = cm.getAverageHardDiskSize();
  
  //Decimal format lets you format the output
  //it doesn't change the actual number
  DecimalFormat df = new DecimalFormat("#,###.##");
  
  System.out.println(df.format(space));
 
 }
}


Here is Program.java

package com.spconger.www;

public class Program {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
   Display d = new Display();
 }

}

No comments:

Post a Comment