PRO192: OOP in Java Assignment: Learning Outcome
PRO192: OOP in Java Assignment: Learning Outcome
Assignment
Learning outcome:
Upon successful completion of this assignment, you will have demonstrated the abilities
to:
Uses streams to read and write data from/to different types of sources/targets
Identify classes, objects, members of a class, and relationships among them
needed for a specific problem
Explain the concept and demonstrates the use of Polymorphism, Encapsulation,
Abstraction, and Inheritance in java
Scenario
The car showroom, named Minh Trang BMW, has a list of BMW cars. BMW brands are stored in a text
file, named brands.txt, and cars are stored in a text file, named cars.txt as following:
1
Problem requirements
The manager of the showroom needs a Java console application in which operations must be supported:
1- List all brands
2- Add a new brand
3- Search a brand based on its ID
4- Update a brand
5- Save brands to the file, named brands.txt
6- List all cars in ascending order of brand names
7- List cars based on a part of an input brand name
8- Add a car
9- Remove a car based on its ID
10- Update a car based on its ID
11- Save cars to file, named cars.txt
Constraints
1- Constraints on brands:
a. Brand ID can not be duplicated.
b. The brand name can not be blank.
c. The sound manufacturer can not be blank.
d. The price must be a positive real number.
2- Constraints on cars:
a. Car ID can not be duplicated.
b. Brand ID must have existed and it must be inputted using a menu.
c. Color can not be blank.
d. Frame ID can not be blank and must be in the “F00000” format and can not be
duplicated.
e. Engine ID can not be blank and must be in the “E00000” format and can not be
duplicated.
Analysis
From the problem description, main concepts and their details are identified:
Concept Detail
Brand Brand ID, brand name, sound brand, price
List of brands
Car Car ID, brand ID, color, frame ID, engine ID
List of cars
Menu A list of objects
Program A menu, a list of brands, a list of cars
2
Design
1- Class Design outline
Menu
<used>
<used> <used>
CarManager
Menu
Public int int int_getChoice(ArrayList<E>): Get user choice as an integer.
int_getChoice(ArrayList<E>) E ref_getChoice(ArrayList<E>): Get the object chosen by user.
Public E
ref_getChoice(ArrayList<E>)
3
Java.lang.Comparable
<implement> for sorting
CarManager
4
4- Main Algorithm
5
public int searchID (String bID); Search a brand based on brand ID. Return the existence position(int)
N= size of the list;
For I = 0 … N-1
If (this.get(i).brandID == bID) return I;
Return -1;
public Brand getUserChoice(); Transform the list to a menu, the user will choose a brand from this
menu.
Menu mnu = new Menu();
Return (Brand)mnu. ref_getChoice(this);
public void addBrand() Add a new Brand to the list.
Receive String ID, constraint. Input ID can not exist in the list
Receive String brandName. The brand name is not blank
Receive String soundBrand. The sound brand is not blank
Receive double price. Price >0
Create a new brand from inputted data;
Add a new brand to the list.
public void updateBrand(); Update brand_name, sound_brand, price of an existed brand.
Receive brandID;
Pos = searchID (brandID);
if pos<0 print out “Not found!”;
Else{
Receive String brandName. The brand name is not blank
Receive String soundBrand. The sound brand is not blank
Receive double price. Price >0
Update new brandName, new sound brand, new price to the pos(th) brand.
}
public void listBrands(); N = size of the list;
For I = 0.. N-1
Print out this.get(i);
6
Else {
Open file in text format for reading line-by-line;
While ( a line is read from file) {
Split the read line into parts;
Extract parts to carID, brandID, color, frameID, engineID
int pos= brandList.searchID(brandID);
Brand b = brandList.get(pos);
Create new car with data above;
Add new car to the list;
}
Close the file;
Return true;
}
public boolean Open the file based on the filename to write data in line-by-line in text
saveToFile(String); format;
For each car in the list {
Write the car to file + “\n”;
}
Close the file;
Return true;
public int searchID (String carID); Search a car based on car ID. Return the existed position(int)
N= size of the list;
For I = 0 … N-1
If (this.get(i).carID == carID) return I;
Return -1;
public int searchFrame (String Search a car by its frame ID. Use in checking frames are not duplicated.
fID); N= size of the list;
For I = 0 … N-1
If (this.get(i).frameID == fID) return I;
Return -1;
public int searchEngine (String Search a car by its engine ID. Use in checking engines are not
eID); duplicated.
N= size of the list;
For I = 0 … N-1
If (this.get(i).engineID == eID) return I;
Return -1;
public void addCar(); Receive carID, carID must be not duplicated
Create a menu for choosing a brand;
Band b = (Brand)menu. ref_getChoice(brandList);
Receive color, color can not be blank
Receive frameID. It must be in the “F0000” and not be duplicated
Receive engineID. It must be in the “E0000” format and not be
duplicated
Create a new car with inputted data;
Add a new car to the list
public void Receive aPartOfBrandName;
printBasedBrandName (); N = size of the list;
Int count = 0;
7
For I = 0.. N-1 {
Car c = this.get(i);
If (aPartOfBrandName is a sub-string of c.brand.brandName) {
Print out c.screenString();
count++;
}
If (count==0) print out “No car is detected!”;
public boolean removeCar(); Remove a car based on it’s ID
Receive removedID;
Int pos = searchID(removedID);
If (pos<0) {
print out “Not found!”
return false;
}
Else{
Remove (pos);
}
Return true;
public boolean updateCar(); Update a car based on it’s ID
Receive updatedID;
Int pos = searchID(updatedID);
If (pos<0) {
print out “Not found!”
return false;
}
Else{
Create a menu for choosing a brand;
Band b = (Brand)menu. ref_getChoice(brandList);
Receive color, color can not be blank
Receive frameID. It must be in the “F0000” and not be duplicated
Receive engineID. It must be in the “E0000” format and not be
duplicated
Update brand, color, Frame ID, machine ID for the pos(th) car.
}
Return true;
public void listCars(); Listing cars in ascending order of brand names.
Sorting cars // Collection.sort(this);
N = size of the list;
For i=0 … N-1 {
Car c = this.get(i);
Print out c.screenString();
}
8
Create an empty brandList;
Load brands from the file brands.txt to brandList;
Create an empty carList using brandList;
Load cars from the file cars.txt to carList;
Int choice;
Create a menu;
Do{
Choice = menu.int_getChoice(ops);
Switch (choice) {
Case 1: brandList.listBrands(); break;
……
}
}
While (choice>0 && choice <=ops.size());
5- Test cases
9
[1 point] - Brand ID must have existed and it must be inputted using
a menu. Choose B5-18
- Color can not be blank.( black/ yellow)
- Frame ID can not be blank and must be in the “F00000”
format and can not be duplicated (K0123/ F12345/
F12352).
- Engine ID can not be blank and must be in the “E00000”
format and can not be duplicated (M0123/ M12345/
E12352)
10