Question

Hello there, So I got an assignment that I just cant get further with. So trying...

Hello there,

So I got an assignment that I just cant get further with. So trying to start from scratch again..

So im supposed to make a class with a UML aswell and a main file.

This is how far ive came, https://imgur.com/a/MGqUN , Thats the car class code and the UML.

This is the assignment

Make a program where you can handle different cars at a car company. The user should be able to choose
do the following:
a) Add cars to an array of Car items.
b) Change car data based on car_id (car_id can be, for example, reg.no)
c) Remove car based on car_id
d) Search for a car brand and list all cars for the search market.
e) Search on year and list all cars for the search year.
f) The program should sum up the value of all cars located at the car company.
g) List all cars in the array of car items
Make a car class that keeps the appropriate data about cars
Make a class of main method where objects are created by the car class. (PS, dont use arraylist, only standard array).

When executing the program, a list of the choices a user can make is displayed in the console of Eclipse
and the user should be able to make a choice and the program should not be closed until the user
choose to end.

So I thought if you could help me out with the car class and the UML so I can get a proper base for the main string.

If you want ur free to do the main string aswell but optional for you.

Greatly appreciate any help!! :)

Kind regards

0 0
Add a comment Improve this question Transcribed image text
Answer #1

UML would be like-

Car.java

public class Car {

//data members

private int carId;

private String carModel;

private int carYear;

private double carPrice;

public Car(){

}

// constructor to set data members

public Car(int carId, String carModel, int carYear, double carPrice) {

this.carId = carId;

this.carModel = carModel;

this.carYear = carYear;

this.carPrice = carPrice;

}

//setter and getter methods for data members

public int getCarId() {

return carId;

}

public void setCarId(int carId) {

this.carId = carId;

}

public String getCarModel() {

return carModel;

}

public void setCarModel(String carModel) {

this.carModel = carModel;

}

public int getCarYear() {

return carYear;

}

public void setCarYear(int carYear) {

this.carYear = carYear;

}

public double getCarPrice() {

return carPrice;

}

public void setCarPrice(double carPrice) {

this.carPrice = carPrice;

}

// this method returns car details

public String toString(){

return "Car [cardId= "+this.carId+

", carModel= "+this.carModel+

", carYear= "+this.carYear+

", carPrice= "+this.carPrice+"]";

}

}

CarDemo.java

import java.util.Scanner;

import package2.Book;

public class CarDemo {

private final static int MAX_SIZE=10; //max size of car array (change it a/c to requirement)

private static int currentIndex=0; //represents current index of car array

public static void main(String[] args) {

Car[] carList=new Car[MAX_SIZE]; //Car array to hold cars

Scanner scan=new Scanner(System.in); //Scanner object to read user inputs

int choice;

//execute loop till user enters 8

do{

//display menu

System.out.println("Menu");

System.out.println("1. Add a Car");

System.out.println("2. Change a Car details");

System.out.println("3. Delete a Car");

System.out.println("4. Search for a car brand");

System.out.println("5. Search on year");

System.out.println("6. Total Price");

System.out.println("7. All car in the list");

System.out.println("8.Exit");

System.out.print("Enter choice : "); //ask user to enter choice

choice=scan.nextInt();

//call function according to choice

switch(choice){

case 1:

addCar(carList);

break;

case 2:

changeCar(carList);

break;

case 3:

deleteCar(carList);

break;

case 4:

searchByBrand(carList);

break;

case 5:

searchbyYear(carList);

break;

case 6:

totalPrice(carList);

break;

case 7:

allCars(carList);

break;

case 8:

System.exit(0);

}

}while(choice!=8);

}

private static void addCar(Car[] carList) {

if(currentIndex==MAX_SIZE){ //check if carlist is full else ask for input

System.out.println("Car list is full");

}else{

Scanner scan=new Scanner(System.in);

System.out.print("Enter car id : "); //ask to enter id

int id=Integer.parseInt(scan.nextLine());

for(int i=0;i<currentIndex;i++){ //iterate over carlist to check if car exists

if(carList[i].getCarId()==id){ //if exists, do not allow to add car and return

System.out.println("Car with this id already exists.");

return;

}

}

//else ask to enter model, year and price

System.out.print("Enter car model : ");

String model=scan.nextLine();

System.out.print("Enter car year : ");

int year=Integer.parseInt(scan.nextLine());

System.out.print("Enter car price : ");

double price=Double.parseDouble(scan.nextLine());

carList[currentIndex]=new Car(id,model,year, price); //add car to carList

currentIndex++; //increment current index

}

}

private static void changeCar(Car[] carList) {

if(currentIndex==0){ //check if carlist is empty else ask for input

System.out.println("Car list is empty");

}else{

Scanner scan=new Scanner(System.in);

System.out.print("Enter car id : "); //ask to enter id

int id=Integer.parseInt(scan.nextLine());

for(int i=0;i<currentIndex;i++){ //iterate over carlist to check if car exists

if(carList[i].getCarId()==id){ //If exists,

//ask for new id, model, year and price

System.out.print("Enter new id : ");

id=Integer.parseInt(scan.nextLine());

System.out.print("Enter car model : ");

String model=scan.nextLine();

System.out.print("Enter car year : ");

int year=Integer.parseInt(scan.nextLine());

System.out.print("Enter car model : ");

double price=Double.parseDouble(scan.nextLine());

//set new values

carList[i].setCarId(id);

carList[i].setCarModel(model);

carList[i].setCarYear(year);

carList[i].setCarPrice(price);

return;//return

}

}

//else show error message

System.out.println("Car with this id does not exists.");

}

}

private static void deleteCar(Car[] carList) {

if(currentIndex==0){ //check if carlist is empty else ask for input

System.out.println("Car list is empty");

}else{

Scanner scan=new Scanner(System.in);

System.out.print("Enter car id : "); //ask to enter id

int id=Integer.parseInt(scan.nextLine());

for(int i=0;i<currentIndex;i++){ //iterate over carlist to check if car exists

if(carList[i].getCarId()==id){ //If yes, delete the car

//delete a car by shifting array values

for(int j=i;j<(currentIndex-1);j++){

carList[j]=carList[j+1];

}

currentIndex-=1; //decrement current index

System.out.println("Car deleted");

return; //return

}

}

//else show error message

System.out.println("Car with this id does not exists.");

}

}

private static void searchByBrand(Car[] carList) {

if(currentIndex==0){ //check if carlist is empty else ask for input

System.out.println("Car array is empty");

}else{

Scanner scan=new Scanner(System.in);

System.out.print("Enter car brand : "); //enter brand

String brand=scan.nextLine();

for(int i=0;i<currentIndex;i++){ //iterate over carlist

//if brand matches, print car details

if(carList[i].getCarModel().toLowerCase().contains(brand.toLowerCase())){

System.out.println(carList[i]);

}

}

}

}

private static void searchbyYear(Car[] carList) {

if(currentIndex==0){ //check if carlist is empty else ask for input

System.out.println("Car array is empty");

}else{

Scanner scan=new Scanner(System.in);

System.out.print("Enter year : "); //enter year

int year=Integer.parseInt(scan.nextLine());

for(int i=0;i<currentIndex;i++){ //iterate over carlist

//if year matches, print car details

if(carList[i].getCarYear()==year){

System.out.println(carList[i]); }

}

}

}

private static void totalPrice(Car[] carList) {

if(currentIndex==0){ //check if carlist is empty else ask for input

System.out.println("Car list is empty");

}else{

double total=0;

for(int i=0;i<currentIndex;i++){ //iterate over carlist

total+=carList[i].getCarPrice(); //add price to total

}

System.out.println("Total value of all cars located at the car company = "+total);

}

}

private static void allCars(Car[] carList) {

//iterate over carlist and print all cars

for(int i=0;i<currentIndex;i++){

System.out.println(carList[i]);

}

}

}

OUTPUT-

5. Search on year 6. Total Price 7. All car in the list 8.Exit Enter choice1 Enter car id 125 Enter car model volvO XC45 Ente

Car Car Car Menu [card1d= [card1d= [card1d= 127, 125, 126, carMode1= carMode1= carMode!= volvo volvo maruti XC601, carYear= 2

5. Search on year 6. Total Price 7. All car in the list 8.Exit Enter choice 7 Car [card1d= 127, carMode1= volvo XC601, carYea

Menu 1. Add a Car 2. Change a Car details 3. Delete a Car 4. Search for a car brand 5. Search on year 6. Total Price 7. All c

Add a comment
Know the answer?
Add Answer to:
Hello there, So I got an assignment that I just cant get further with. So trying...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need an OUTLINE ONLY (pseudocode/comments). DO NOT DO THE PROGRAMMING ASSIGNMENT. Part I: PA3 Outline...

    I need an OUTLINE ONLY (pseudocode/comments). DO NOT DO THE PROGRAMMING ASSIGNMENT. Part I: PA3 Outline (10 points). Create an outline in comments/psuedocode for the programming assignment below. Place your comments in the appropriate files: main.cpp, functions.h, dealer.cpp, dealer.h, dealer.cpp (as noted below). Place into a file folder named LastnamePA3, the zip the content and hand in a zip file to Canvas. Part II: PA3: Car Dealership (40 points) For Programming Assignment 3 you will be creating a program to...

  • Programming Assignment #2 (Arrays) I. The Assignment This assignment is to take your Garage class from...

    Programming Assignment #2 (Arrays) I. The Assignment This assignment is to take your Garage class from the previous assignment and modify it so that it uses an array of Car objects as the principal data structure instead of an ArrayList-of-Car. This is an example of the OOP principle of information hiding as your Car and test classes will not have to be modified at all. Unless you broke encapsulationon the previous assignment, that is   II. Specifications Specifications for all 3...

  • Assignment # 6              ...

    Assignment # 6                      CSCI 215    Due:   04/11/2019 50   pts.                                        In this program assignment, you will construct two different exception classes: ClassNotFoundException and ObjectNotFoundException. A static method search that takes an array of objects and a target object as arguments and throws two types of exceptions must be defined. An object of ClassNotFoundException should be thrown if the array doesn’t contain an object which is from the same class as the target; if the array contains an object which is the...

  • C++, use the skeleton code to make a program of the following

    c++, use the skeleton code to make a program of the following include <iostream> tinclude <string> using namespace std; class car public: //define your functions here, at least 5 private: string name; int mpg; double price int horsepower; // feel free to add more atributes int main() // create you objects, call your functions // define member functions here For this lab, write a program that does the following: Creates a class based on the car skeleton code (you may...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • Use your Car class from the previous Programming Assignment. For this assignment, create a new class...

    Use your Car class from the previous Programming Assignment. For this assignment, create a new class that represents a Used Car Dealership. Your Java program will simulate an inventory system for the dealership, where the employees can manage the car information. Start by creating an array of 10 cars, with an "ID" of 0 through 9, and use the default constructor to create each car. For example, the third car in an array called cars would have ID 2. You...

  • Assignment # 6                      CSCI 215    Due:   04/11/2019 50   pts.       &nb

    Assignment # 6                      CSCI 215    Due:   04/11/2019 50   pts.                                        In this program assignment, you will construct two different exception classes: ClassNotFoundException and ObjectNotFoundException. A static method search that takes an array of objects and a target object as arguments and throws two types of exceptions must be defined. An object of ClassNotFoundException should be thrown if the array doesn’t contain an object which is from the same class as the target; if the array contains an object which is...

  • We are to make a program about a car dealership using arrays. I got the code...

    We are to make a program about a car dealership using arrays. I got the code to display all cars in a list, so I'm good with that. What I'm stuck at is how to make it so when a user inputs s for search, it allows them to search the vehicle. Here is what I have so far     public class Car {         String color;         String model;         String year;         String company;         String plate;       ...

  • Java - Car Dealership Hey, so i am having a little trouble with my code, so...

    Java - Car Dealership Hey, so i am having a little trouble with my code, so in my dealer class each of the setName for the salesman have errors and im not sure why. Any and all help is greatly appreciated. package app5; import java.util.Scanner; class Salesman { private int ID; private String name; private double commRate; private double totalComm; private int numberOfSales; } class Car { static int count = 0; public String year; public String model; public String...

  • I've been trying to get this written for days, and cannot seem to get this written....

    I've been trying to get this written for days, and cannot seem to get this written. I keep restarting, but cannot get it to do the math I'm needing or to have a main menu with the list. //Int32 price; //double tax; Int32 intTime = 3000; Boolean varIsValid = true; object price = null; //double tax = .08 * Convert.ToDouble(price) / 100; while (varIsValid) { Console.WriteLine("Welcome to CIS 120"); Console.WriteLine("Shopping List Project"); Console.WriteLine("Please Enter Price"); //if (Console.ReadLine() != "exit") ;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT