Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class CarDatabase {
- static int noOfCars = 0;
- static Car carDetails[] = new Car[10];
- public static void main(String args[]){
- Scanner scan = new Scanner(System.in);
- int response = 1;
- do {
- System.out.println("Please choose one of the following: ");
- System.out.println("1) Input a new entry from a keyboard");
- System.out.println("2) Print all the entries so far");
- System.out.println("3) Quit ");
- response = Integer.valueOf(scan.next().charAt(0)) -48;
- scan.nextLine();
- if(response==1) {
- setDetails();
- }else if(response==2) {
- if(noOfCars!=0) {
- printDetails();
- }else{System.out.println("You have not entered any cars into the database.");}
- }else if(response==3) {
- break;
- }else{
- System.out.println("You have not entered a valid option. Please try again.");
- }
- } while(response!=3);
- }
- public static void setDetails(){
- char addCarResponse = ' ';
- Scanner scan = new Scanner(System.in);
- do {
- System.out.println("Enter the car manufacture: ");
- String manufacture = scan.nextLine();
- System.out.println("Enter the car registration number");
- String regNo = scan.nextLine();
- System.out.println("Enter the colour of the car: ");
- String colour = scan.nextLine();
- System.out.println("Enter the model of the car: ");
- String model = scan.nextLine();
- System.out.println("Enter the price of the car to the nearest GBP: ");
- double price = scan.nextDouble();
- scan.nextLine();
- carDetails[noOfCars] = new Car(price, manufacture, model, regNo, colour)
- System.out.println("Would you like to enter another car's details?");
- addCarResponse = scan.next().charAt(0);
- scan.nextLine();
- noOfCars++;
- } while(!(addCarResponse=='N') && !(addCarResponse=='n'));
- }
- public static void printDetails(){
- for(int i=0; i<=noOfCars; i++) {
- System.out.println("Manufacture: " + carDetails[i].getManufacture());
- System.out.println("Registration Number: "+ carDetails[i].getRegNo());
- System.out.println("Colour: "+ carDetails[i].getColour());
- System.out.println("Model: "+ carDetails[i].getModel());
- System.out.println("Year: "+carDetails[i].getYear());
- System.out.println("Price: "+carDetails[i].getPrice());
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement