load list of details, and display to screen.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • owz
    New Member
    • Nov 2006
    • 14

    load list of details, and display to screen.

    I am trying 2 load details about cars from a.txt file

    and then display the total stock value off all cars.

    Code:
    public class Car 
    {
      // attributes
      
      private String manufacturer;
      private String model;
      private String regNo;
      private int    year;
      private int    price;
    
      // constructor
      
      public Car(String newManufacturer, String model, String regNo, int year, int price)
      {
        this.manufacturer = newManufacturer;
        this.model = model;
        this.regNo = regNo;
        this.year = year;
        this.price = price;
      }
    
      // another constructor
      
      public Car()
      {
      }
    
      // mutator methods
      
      public void setManufacturer(String manufacturer)
      {
        this.manufacturer = manufacturer;
      }
    
      public void setModel(String model)
      {
        this.model = model;
      }
    
      public void setRegNo(String regNo)
      {
        this.regNo = regNo;
      }
    
      public void setYear(int year)
      {
        this.year = year;
      }
    
      public void setPrice(int price)
      {
        this.price = price;
      }
    
      public String getManufacturer()
      {
        return this.manufacturer;
      }
    
      // accessor methods
      
      public String getModel()
      {
        return this.model;
      }
    
      public String getRegNo()
      {
        return this.regNo;
      }
    
      public int getYear()
      {
        return this.year;
      }
    
      public int getPrice()
      {
        return this.price;
      }
      
      public String toString()
      {
        return "Car details \nManufacturer        :" + manufacturer + 
                           "\nModel               :" + model + 
                           "\nRegistration number :" + regNo +
                           "\nYear                :" + year +
                           "\nPrice               :£" + price;
      }
      
      public static void main(String[] args)
      {
        Car car = new Car("Vauxhall", "Omega 2.0 GLS", "M21VUR", 1995, 1000);
        Car car1 = new Car("rustbucket","corsa 1.6","T32brt", 1998, 2350);
        System.out.println(car+car1.toString());
      }// end of main method
    }// end of class Car
    this is my first class attemt with out loading in a file.


    Code:
    import java.io.*;
    
    public class Motors 
    {
    
      private Garage theGarage = null;
    
      public static void main ( String [] args ) throws IOException
      {
         theGarage = new Garage();
      }//main
    
      
    }//Motors


    3nd Class
    Code:
    import java.io.*;
    
    public class Garage 
    {
      private static final int MAX_CARS = 10;
      private Car [] carDetails = new Car[MAX_CARS];
      private int carCount = 0;
        private static final String   textFilesPath = "C:\\Documents and Settings\\owz\\Desktop\\RESIT DO NOW\\car details.txt";
       
      public Garage() throws IOException
      {
        loadCarDetails();
      }
      
      public void loadCarDetails() throws IOException
      {
        BufferedReader inputCarFile; 
        String aManufacturer;
        String aModel;
        String aRegNo;
        int aYear;
        int aPrice;
          inputCarFile = Text.open ( textFilesPath );
          for ( int i = 0; i < MAX_CARS; i++ )
          {
            carDetails[ i ] = new Car( );
          }//end for
    
        
      }// end of loadCarDetails method
    
      public void saveCarDetails() throws IOException
      {
        PrintWriter carFileOut; 
        carFileOut = Text.create(textFilesPath + "saved account details.txt");
        
        carFileOut.println( "****" );
        carFileOut.close();
    
        
    
      }// end of saveCarDetails method
    
      public void addCar( String aManufacturer,
                          String aModel,
                          String aRegNo,
                          int aYear,
                          int aPrice )
      {
        
        
    
      }//addCar
    
      public boolean garageFull(String aRegNo)
      {
          boolean garageFull = false;
          if (aRegNo = 11) 
      {
          garageFull = true;  
      }
        return  garageFull ;  // something true or false
        
      }//garageFull
    
      public void listCarDetails()
      {
        
      }// end of listCarDetails method
    
      public int totalStockValue()
      {
        int stockValue = 0;
    
        return stockValue;
      }// end of totalStockValue method
    
      public int getCarCount()
      {
        return this.carCount ;//something
      }//getCarCount
    
      public Car getCarDetail( int entry )
      {
        return ;//something
      }//getCarDetail
    
      
    
      //Other methods required
    
    
      
      public static void main(String[] args) throws IOException
      {
        Garage garage = new Garage();
        
        garage.loadCarDetails();
        
        garage.listCarDetails();
    
        System.out.println("The total value of car stock is £" + garage.totalStockValue());
        
        Text.showMessage("That's all folks");
        System.exit(0);
      }// end of main method
    }// end of class Garage

    This is the format of my text file

    V40GOR
    Vauxhall
    Omega 2.0 CD
    1998
    1000
    76000
    L17LLL
    Rover
    MGF 1.8
    1998
    4000
    45000
    CW04ABC
    car manufacturer 1
    car model 1
    2004
    40000
    10000
    CW04XYZ
    car manufacturer 1
    car model 2
    2004
    10000
    5000
    ****



    Thanx in advance for any help.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    If your problem is reading files, read this thread.

    Comment

    • owz
      New Member
      • Nov 2006
      • 14

      #3
      The file is not just loaded an displayed .

      It is an order
      String Registration Number
      String Manufacturer
      String Model
      int Year
      int Price

      i plan on creating a command line interface to -

      Print Stock
      Add car
      Sell car
      Amend price
      Find car details

      Comment

      • owz
        New Member
        • Nov 2006
        • 14

        #4
        Help needed with importing file an displaying

        This is what i have so far .Line 99 i have a missing return value, no idea what it should be.
        As stated above, i am loading a txt file in an tryin 2 display 2 the screen. also calculate a total value of all cars.


        Code:
         import java.io.*;
        
        public class Garage 
        {
          private static final int MAX_CARS = 10;
          private Car [] carDetails = new Car[MAX_CARS];
          private int carCount = 0;
            private static final String   textFilesPath = "C:\\Documents and Settings\\owz\\Desktop\\RESIT DO NOW\\car details.txt";
           
           
           
          public Garage() throws IOException
          {
            loadCarDetails();
          }
          
          public void loadCarDetails() throws IOException
          {
            BufferedReader inputCarFile; 
            String aManufacturer;
            String aModel;
            String aRegNo;
            int aYear;
            int aPrice;
          
              for ( int i = 0; i < MAX_CARS; i++ )
              {
                carDetails[ i ] = new Car( );
              }
              carCount = 0;
          
          inputCarFile = Text.open (textFilesPath);
              aManufacturer =Text.readString(inputCarFile);
              while (aManufacturer.equals ("****") == false)
              { 
                aModel = Text.readLine(inputCarFile);
                aRegNo = Text.readLine(inputCarFile);
                aYear = Text.readInt(inputCarFile);
                aPrice = Text.readInt(inputCarFile); 
                this.addCar(aManufacturer,aModel,aRegNo,aYear,aPrice);
            }
            
              inputCarFile.close();
              
              //end for
        
            
          }// end of loadCarDetails method
        
          public void saveCarDetails() throws IOException
          {
            PrintWriter carFileOut; 
            carFileOut = Text.create(textFilesPath + "saved account details.txt");
            
            carFileOut.println( "****" );
            carFileOut.close();
        
            
        
          }// end of saveCarDetails method
        
          public void addCar( String aManufacturer,String aModel,String aRegNo,
                                int aYear,int aPrice )
          {
            this.addCar(aManufacturer,aModel,aRegNo,aYear,aPrice);
            
            
        
          }//addCar
        
          public boolean garageFull()
          {
             
              return  false ; 
          
          // something true or false
            
          }//garageFull
        
          public void listCarDetails()
          {
            
          }// end of listCarDetails method
        
          public int totalStockValue()
          {
            int stockValue = 0;
        
            return stockValue;
          }// end of totalStockValue method
        
          public int getCarCount()
          {
            return this.carCount ;//something
          }//getCarCount
        
          public Car getCarDetail(int entry  )
          {
            return  ;
            //something
          }//getCarDetail
        
          
        
          //Other methods required probly
        public Car setCarDetail()
        {
          
        }
          
          public static void main(String[] args) throws IOException
          {
            Garage garage = new Garage();
            
            garage.loadCarDetails();
            
            garage.listCarDetails();
        
            System.out.println("The total value of car stock is £" + garage.totalStockValue());
            
            Text.showMessage("That's all folks");
            System.exit(0);
          }// end of main method
        }// end of class Garage
        Last edited by owz; Jul 3 '07, 02:37 PM. Reason: added more code

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Hold on here. You don't have to keep on posting more code.
          Did you read that thread that I pointed to on how to read a text file?

          P.S The missing return thing is because you specified that method to return a Car object and yet did not return anything;

          Comment

          • owz
            New Member
            • Nov 2006
            • 14

            #6
            sorry for double posting, i was tryin 2 edit the 1st post but i couldnt.
            yes i did read the link info.It not just loadin an display, it has 2 be stored sum how mabey an array so tha i can use a command line interface type thing to sort,add, delet,save an so forth 2 the data.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by owz
              sorry for double posting, i was tryin 2 edit the 1st post but i couldnt.
              yes i did read the link info.It not just loadin an display, it has 2 be stored sum how mabey an array so tha i can use a command line interface type thing to sort,add, delet,save an so forth 2 the data.
              If you read that thread well, then you'll realize that all you need to do is to change the part that prints the data read from the readLine method to direct those strings to an ArrayList of Strings or to create a Car object first then load cars to an Arraylist. After that you can play any games you want with the arraylist.

              Comment

              Working...