0% found this document useful (0 votes)
234 views

Title Background Context: LAB211 Assignment

The document describes a Java program to manage a fruit shop. The program allows a shop owner to: 1. Create fruit products by entering the fruit ID, name, price, quantity and origin. The fruits are stored in an ArrayList. 2. View orders placed by customers. The orders are stored in a Hashtable with the customer name as the key. 3. Facilitate customer shopping. Customers can view available fruits, add items to their order, and check out by providing their name. Their order is stored in the Hashtable.

Uploaded by

Blyat Blyat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
234 views

Title Background Context: LAB211 Assignment

The document describes a Java program to manage a fruit shop. The program allows a shop owner to: 1. Create fruit products by entering the fruit ID, name, price, quantity and origin. The fruits are stored in an ArrayList. 2. View orders placed by customers. The orders are stored in a Hashtable with the customer name as the key. 3. Facilitate customer shopping. Customers can view available fruits, add items to their order, and check out by providing their name. Their order is stored in the Hashtable.

Uploaded by

Blyat Blyat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB 9:

LAB211 Assignment
Question 1: J1.S.P0023
Title
Create a Java console program to manage a Fruit Shop (Product and Shopping).
Background Context
Fruit Shop management system in java is basically developed for manage the Fruit Shop. In the
Fruit Shop, product and Shopping management is very important.By making system is
computerized it make possible to reduce effort, work is efficient and increase their revenue
opportunities for shop owner.

Program Specifications
The program provides shop owners tools to run their business effectively. The program’s functions
as below:

1. Main Screen as below:

FRUIT SHOP SYSTEM


1. Create Fruit
2. View orders
3. Shopping (for buyer)
4. Exit

(Please choose 1 to createproduct, 2 to view order, 3 for shopping, 4 to Exit program).

2. Function details:
2.1. For Fruit Shop owner
Create product (Fruit):
- A Fruit has attributes: Fruit Id, Fruit Name, Price, Quantity and Origin.
- From “Main Screen”, use select item (1) to create Fruit. After each Fruit is created, the
system shows message: Do you want to continue (Y/N)? User chooses Y to continues, if you
chooses N, the program returns main screen and display all Fruits what are created.
View orders
- To view orders list, who buy and how many product

Customer: Marry Carie


Product | Quantity | Price | Amount
1. Apple 3 1$ 3$
2. Mango 2 2$ 4$
Total: 7$

Customer: John Smith


Product | Quantity | Price | Amount
1. JackFruit 3 3$ 9$
2. Mango 2 2$ 4$
Total: 13$

2.2. Shopping
- Customer selects item 3, the program displays all fruits. For example:
1
List of Fruit:
| ++ Item ++ | ++ Fruit Name ++ | ++ Origin ++ | ++ Price ++ |
1 Coconut Vietnam 2$
2 Orange US 3$
3 Apple Thailand 4$
4 Grape France 6$

To order, customer selects Item, for example: when customer selects item 1, the program
shows:
You selected: Coconut
Please input quantity:
After customer inputs quantity of fruit, the program shows message: Do you want to order
now (Y/N). If customer selects N, the program returns to List of Fruit to continue ordering.
If select Y, the program displays:
Product | Quantity | Price | Amount
Coconut 3 2$ 6$
Total: 6$
Input your name:
Customer inputs his/her name to finish ordering. The program returns main screen.

Technical Requirements
1. Using Object-Oriented programming style.
2. Use only core Java functions and classes.
3. Only use ArrayList and HashTable to store data (is required).
Guidelines

Slot Task Description


1 - Code Design - Student should create Fruit class with some its attributes:Fruit Id,
- Create Fruit Fruit Name, Price, Quantity and Origin
- Using ArrayList to store the Fruit
2 - Shopping - Using ArrayList to store items that customer bought fruit and
using HashTable to store order of customer.
Eg: hashTable.set(<customer name>,<list of items bought>)
3 - Shopping
4 - View order
5 - Review program

Code
8 import java.util.ArrayList;
9 import java.util.Hashtable;
10
11 /**
12 *
13 * @author Quynh Tran Ly
14 */
15 public class J1LP0023 {
16
17 /**
18 * @param args the command line arguments
19 */
20 public static void main(String[] args) {
2
21 // TODO code application logic here
22 ArrayList<Fruit> lf = new ArrayList<>();
23 Hashtable<String, ArrayList<Order>> ht = new Hashtable<>();
24 //loop until user want to exit
25 while (true) {
26 int choice = Manager.menu();
27 switch (choice) {
28 case 1:
29 Manager.createFruit(lf);
30 break;
31 case 2:
32 Manager.viewOrder(ht);
33 break;
34 case 3:
35 Manager.shopping(lf, ht);
36 break;
37 case 4:
38 return;
39 }
40 }
41 }
42
43 }
44

8 import java.util.ArrayList;
9 import java.util.Hashtable;
10
11 /**
12 *
13 * @author Quynh Tran Ly
14 */
15 public class Manager {
16 //display menu
17 static int menu() {
18 //loop until user want to exit
19 //Students write here appropriate statements to complete this function
20 int choice = Validation.checkInputIntLimit(1, 4);
21 return choice;
22 }
23
24 //allow user create fruit
25 static void createFruit(ArrayList<Fruit> lf) {
26 //loop until user don't want to create fruit
27 while (true) {
28 System.out.print("Enter fruit id: ");
29 String fruitId = Validation.checkInputString();
30 //check id exist
31 if (!Validation.checkIdExist(lf, fruitId)) {
32 //Students write here appropriate statements to complete this function
33 return;
34 }
35 System.out.print("Enter fruit name: ");
36 String fruitName = Validation.checkInputString();
37 //Students write here appropriate statements to complete this function
38 lf.add(new Fruit(fruitId, fruitName, price, quantity, origin));
39 //check user want to continue or not
40 if (!Validation.checkInputYN()) {
41 return;
42 }
43 }
44 }
45
46 //allow user show view order
3
47 static void viewOrder(Hashtable<String, ArrayList<Order>> ht) {
48 for (String name : ht.keySet()) {
49 System.out.println("Customer: " + name);
50 ArrayList<Order> lo = ht.get(name);
51 displayListOrder(lo);
52 }
53 }
54
55 //allow user buy items
56 static void shopping(ArrayList<Fruit> lf, Hashtable<String, ArrayList<Order>> ht) {
57 //check list empty user can't buy
58 if (lf.isEmpty()) {
59 System.err.println("No have item.");
60 return;
61 }
62 //loop until user don't want to buy continue
63 ArrayList<Order> lo = new ArrayList<>();
64 while (true) {
65 displayListFruit(lf);
66 //Students write here appropriate statements to complete this function
67 int quantity = Validation.checkInputIntLimit(1, fruit.getQuantity());
68 fruit.setQuantity(fruit.getQuantity() - quantity);
69 //check item exist or not
70 if (!Validation.checkItemExist(lo, fruit.getFruitId())) {
71 updateOrder(lo, fruit.getFruitId(), quantity);
72 } else {
73 lo.add(new Order(fruit.getFruitId(), fruit.getFruitName(),
74 quantity, fruit.getPrice()));
75 }
76
77 if (!Validation.checkInputYN()) {
78 break;
79 }
80 }
81 displayListOrder(lo);
82 System.out.print("Enter name: ");
83 String name = Validation.checkInputString();
84 ht.put(name, lo);
85 System.err.println("Add successfull");
86 }
87
88 //display list fruit in shop
89 static void displayListFruit(ArrayList<Fruit> lf) {
90 int countItem = 1;
91 System.out.printf("%-10s%-20s%-20s%-15s\n", "Item", "Fruit name", "Origin", "Price");
92 for (Fruit fruit : lf) {
93 //Students write here appropriate statements to complete this function
94 }
95 }
96
97 //get fruint user want to by
98 static Fruit getFruitByItem(ArrayList<Fruit> lf, int item) {
99 int countItem = 1;
100 for (Fruit fruit : lf) {
101 //check shop have item or not
102 if (fruit.getQuantity() != 0) {
103 countItem++;
104 }
105 if (countItem - 1 == item) {
106 return fruit;
107 }
108 }
109 return null;
110 }
4
111
112 //display list order
113 static void displayListOrder(ArrayList<Order> lo) {
114 double total = 0;
115 System.out.printf("%15s%15s%15s%15s\n", "Product", "Quantity", "Price", "Amount");
116 for (Order order : lo) {
117 //Students write here appropriate statements to complete this function
118 }
119 System.out.println("Total: " + total);
120 }
121
122 //if order exist then update order
123 static void updateOrder(ArrayList<Order> lo, String id, int quantity) {
124 for (Order order : lo) {
125 if (order.getFruitId().equalsIgnoreCase(id)) {
126 order.setQuantity(order.getQuantity() + quantity);
127 return;
128 }
129 }
130 }
131 }
132

8 import java.util.ArrayList;
9 import java.util.Scanner;
10
11 /**
12 *
13 * @author Quynh Tran Ly
14 */
15 public class Validation {
16 private static final Scanner in = new Scanner(System.in);
17
18 //check user input number limit
19 public static int checkInputIntLimit(int min, int max) {
20 //loop until user input correct
21 while (true) {
22 try {
23 int result = Integer.parseInt(in.nextLine().trim());
24 if (result < min || result > max) {
25 throw new NumberFormatException();
26
27 }
28 return result;
29 } catch (NumberFormatException e) {
30 System.err.println("Please input number in rage [" + min + ", " + max + "]");
31 System.out.print("Enter again: ");
32 }
33 }
34 }
35
36 //check user input string
37 public static String checkInputString() {
38 //loop until user input correct
39 while (true) {
40 String result = in.nextLine().trim();
41 if (result.isEmpty()) {
42 System.err.println("Not empty");
43 System.out.print("Enter again: ");
44 } else {
45 return result;
46 }
47 }
48 }
5
49
50 //check user input int
51 public static int checkInputInt() {
52 //loop until user input correct
53 while (true) {
54 try {
55 int result = Integer.parseInt(in.nextLine().trim());
56 return result;
57 } catch (NumberFormatException e) {
58 System.err.println("Must be input integer.");
59 System.out.print("Enter again: ");
60 }
61 }
62 }
63
64 //check user input double
65 public static double checkInputDouble() {
66 //loop until user input correct
67 while (true) {
68 try {
69 double result = Double.parseDouble(in.nextLine());
70 return result;
71 } catch (NumberFormatException e) {
72 System.err.println("Must be input double");
73 System.out.print("Enter again: ");
74 }
75
76 }
77 }
78
79 //check user input yes/ no
80 public static boolean checkInputYN() {
81 System.out.print("Do you want to continue (Y/N)? ");
82 //loop until user input correct
83 while (true) {
84 String result = checkInputString();
85 //return true if user input y/Y
86 if (result.equalsIgnoreCase("Y")) {
87 return true;
88 }
89 //return false if user input n/N
90 if (result.equalsIgnoreCase("N")) {
91 return false;
92 }
93 System.err.println("Please input y/Y or n/N.");
94 System.out.print("Enter again: ");
95 }
96 }
97
98 //check id exist
99 public static boolean checkIdExist(ArrayList<Fruit> lf, String id) {
100 //Students write here appropriate statements to complete this function
101 return true;
102 }
103
104 //check item exist or not
105 public static boolean checkItemExist(ArrayList<Order> lo, String id) {
106 for (Order order : lo) {
107 if (order.getFruitId().equalsIgnoreCase(id)) {
108 return false;
109 }
110 }
111 return true;
112 }
6
113 }

12 public class Fruit {


13 //Students write here appropriate statements to complete this function
14
15 public Fruit() {
16 }
17
18 public Fruit(String fruitId, String fruitName, double price, int quantity, String origin) {
19 //Students write here appropriate statements to complete this function
20 }
21
22 public String getFruitId() {
23 return fruitId;
24 }
25
26 public void setFruitId(String fruitId) {
27 this.fruitId = fruitId;
28 }
29
30 public String getFruitName() {
31 return fruitName;
32 }
33
34 public void setFruitName(String fruitName) {
35 this.fruitName = fruitName;
36 }
37
38 public double getPrice() {
39 return price;
40 }
41
42 public void setPrice(double price) {
43 this.price = price;
44 }
45
46 public int getQuantity() {
47 return quantity;
48 }
49
50 public void setQuantity(int quantity) {
51 this.quantity = quantity;
52 }
53
54 public String getOrigin() {
55 return origin;
56 }
57
58 public void setOrigin(String origin) {
59 this.origin = origin;
60 }
61
62 }
63

12 public class Order {


13 private String fruitId;
14 private String fruitName;
15 private int quantity;
16 private double price;
17
18 public Order() {

7
19 }
20
21 public Order(String fruitId, String fruitName, int quantity, double price) {
22 //Students write here appropriate statements to complete this function
23 }
24
25 public String getFruitId() {
26 return fruitId;
27 }
28
29 public void setFruitId(String fruitId) {
30 this.fruitId = fruitId;
31 }
32
33 public String getFruitName() {
34 return fruitName;
35 }
36
37 public void setFruitName(String fruitName) {
38 this.fruitName = fruitName;
39 }
40
41 public int getQuantity() {
42 return quantity;
43 }
44
45 public void setQuantity(int quantity) {
46 this.quantity = quantity;
47 }
48
49 public double getPrice() {
50 return price;
51 }
52
53 public void setPrice(double price) {
54 this.price = price;
55 }
56
57 }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy