Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class CarDatabase {
  3. static int noOfCars = 0;
  4. static Car carDetails[] = new Car[10];
  5.  
  6. public static void main(String args[]){
  7.         Scanner scan = new Scanner(System.in);
  8.         int response = 1;
  9.         do {
  10.  
  11.                 System.out.println("Please choose one of the following: ");
  12.                 System.out.println("1) Input a new entry from a keyboard");
  13.                 System.out.println("2) Print all the entries so far");
  14.                 System.out.println("3) Quit ");
  15.  
  16.                 response = Integer.valueOf(scan.next().charAt(0)) -48;
  17.                 scan.nextLine();
  18.  
  19.                 if(response==1) {
  20.                         setDetails();
  21.                 }else if(response==2) {
  22.                         if(noOfCars!=0) {
  23.                                 printDetails();
  24.                         }else{System.out.println("You have not entered any cars into the database.");}
  25.                 }else if(response==3) {
  26.                       break;
  27.                 }else{
  28.                         System.out.println("You have not entered a valid option. Please try again.");
  29.  
  30.                 }
  31.         } while(response!=3);
  32. }
  33. public static void setDetails(){
  34.         char addCarResponse = ' ';
  35.         Scanner scan = new Scanner(System.in);
  36.         do {
  37.  
  38.                 System.out.println("Enter the car manufacture: ");
  39.                 String manufacture = scan.nextLine();
  40.  
  41.                 System.out.println("Enter the car registration number");
  42.                 String regNo = scan.nextLine();
  43.  
  44.                 System.out.println("Enter the colour of the car: ");
  45.                 String colour = scan.nextLine();
  46.                
  47.                 System.out.println("Enter the model of the car: ");
  48.                 String model = scan.nextLine();
  49.  
  50.                 System.out.println("Enter the price of the car to the nearest GBP: ");
  51.                 double price = scan.nextDouble();
  52.                 scan.nextLine();
  53.  
  54.                 carDetails[noOfCars] = new Car(price, manufacture, model, regNo, colour)
  55.                 System.out.println("Would you like to enter another car's details?");
  56.                 addCarResponse = scan.next().charAt(0);
  57.                 scan.nextLine();
  58.                         noOfCars++;
  59.  
  60.         } while(!(addCarResponse=='N') && !(addCarResponse=='n'));
  61. }
  62.  
  63. public static void printDetails(){
  64.         for(int i=0; i<=noOfCars; i++) {
  65.                 System.out.println("Manufacture: " + carDetails[i].getManufacture());
  66.                 System.out.println("Registration Number: "+ carDetails[i].getRegNo());
  67.                 System.out.println("Colour: "+ carDetails[i].getColour());
  68.                 System.out.println("Model: "+ carDetails[i].getModel());
  69.                 System.out.println("Year: "+carDetails[i].getYear());
  70.                 System.out.println("Price: "+carDetails[i].getPrice());
  71.                 System.out.println();
  72.         }
  73. }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement