Individual Assignment: Programming 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Programming 1

Individual Assignment
In this assignment, you’ll write a menu-driven program in Java to manage general products.

Requirements
1.1 Program behavior description
The program should include the following features in the menu (the order may be different).
1. Add products
Ask user to enter product name and price in VND, then add the product to the list of products
that are managed by the program. Each product should have an integer ID which is generated
automatically, starting from 1 and increases everytime a new product is added.
2. Display products
Display products in the order as they are added. This feature should display product’s ID, name
and price.
3. Edit a product
Ask user to enter a product’s ID. If the product doesn’t exist, print an error message (for instance
“Product not found”). If the product can be found, ask the user to enter a new name and price for
it, and update the product.
4. Delete a product
Ask user to enter a product’s ID. If the product doesn’t exist, print an error message (for instance
“Product not found”). If the product can be found, remove it from the list of products.
5. Search for products by name
Ask the user to enter a search phrase. Display all products whose names match the search phrase.
6. Sort products by price
Sort and display products in the increasing order of price.
7. Sort products by name
Sort products by name in alphabetical order (A-Z) and display products.
0. Quit
The program should automatically load products from a text file when it starts and save products
to the same text file when the user quits. You have to follow the format of the sample save.txt
file which has been provided with the assignment.

1.2. Required classes, attributes and methods


You are free to add more attributes, classes and methods, but these required classes, attributes
and methods must be implemented correctly.

Product.java
Attributes

Visibility Static Type Name


public no int id
public no String name
public no double price
public yes int autoId

autoId is stores the next product ID and should increase whenever a new Product object is
created.
Constructor
A public constructor which receives two parameters, in the order specified below. The names of
parameters do not matter.

Type Description
String This parameter is used to initialize the ‘name’ attribute.
double This parameter is used to initialize the ‘price’ attribute.

App.java
Attributes

Visibility Static Type Name Description


public yes List<Product> products The products are stored in this attribute.

Methods
public static List<Product> loadProducts()

Load products and autoId from the text file called save.txt under the project’s root folder.
You must follow the format given in the sample save.txt file.
public static void saveProducts()
Save products and autoId to the text file called save.txt under the project’s root folder. You
must follow the format given in the sample save.txt file.
public static List<Product> sortByPrice()

Sort the products in the products attribute by the ascending order or price and return a sorted
list (the products attribute should not be modified). This method should be used as part of
feature 6.
public static List<Product> sortByNames()

Sort the products in the products attribute in the alphabetical order and return a sorted list (the
products attribute should not be modified). This method should be used as part of feature 7.
public static void main(String[] args)

Contains the main program’s logic.

1.3. Frequently asked questions

Exception handling
It is both ok to handle exceptions with try-catch or ignore exceptions with the throws clause
in method headers.
Sorting
It is ok to use built-in sort methods for sorting. But the list of products is not a list of integers so
it would require additional effort to make it possible. Implementing the sorting yourself is the
recommended approach.
Products display
You’re encouraged to display products in a table-like manner. Product display should be
compact while remaining nice to look at.
Will the assignment be marked by tools or by teacher?
Tools will be used to check the functionality of the required methods. Teacher will manually
examine the remaining parts.

Submission
Submit a zip file containing the two required classes. Name the zip file the the format:
pr1_sid.zip where sid is your student id.

Academic honesty
Never copy from others! If any plagiarism is detected, 0 points for both students.

You might also like