-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagazineImp.java
More file actions
125 lines (108 loc) · 4.89 KB
/
MagazineImp.java
File metadata and controls
125 lines (108 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//import javax.print.attribute.standard.JobHoldUntil;
import javax.swing.JOptionPane;
public class MagazineImp {
public static void main(String[] args) {
final int maxNumOfMagazine = 100;
// define the array og magazine
Magazine[] allMagazine = new Magazine[maxNumOfMagazine];// create the array of magazine
int choice = 0;
// create the menu using a switch case
do {
choice = Integer.parseInt(JOptionPane.showInputDialog(
"Enter on of the following choice\n1.Add Magazine\n2.Search Magazine\n3.Print All Magazine\n4.Delete magazine\n5.Exit Magazine"));
switch (choice) {
case 1:
allMagazine[Magazine.getNumOfMagazine()] = createMagazine();
break;
case 2:
String searchkey = JOptionPane.showInputDialog("Enter the magazine title you are looking for ");
int found = searchMagazine(allMagazine, searchkey);
if (found > -1)
JOptionPane.showMessageDialog(null, "The magazine found " + allMagazine[found].toString());
else
JOptionPane.showMessageDialog(null, "The magazine not found");
break;
case 3:
printMagazine(allMagazine);
break;
case 4:
removeMagazine(allMagazine);
break;
case 5:
System.exit(0);
default:
JOptionPane.showMessageDialog(null, "Incorrect choice! Try again");
}
} while (choice >= 1 && choice <= 5);
printMagazine(allMagazine);
} // main method
// remove magazine method
public static void removeMagazine(Magazine[] allMagazines) {
printMagazine(allMagazines);
String magazineToRemove = JOptionPane.showInputDialog("Enter the magazine title to delate");
int index = searchMagazine(allMagazines, magazineToRemove);
if (index > -1) {
for (int i = index; i < Magazine.getNumOfMagazine(); i++) {
allMagazines[i] = allMagazines[i + 1];
}
allMagazines[Magazine.getNumOfMagazine()] = null;
Magazine.updateNumOfMagazine();
} else {
JOptionPane.showMessageDialog(null, "Error! No magazine found");
}
}
// search a magazine
public static int searchMagazine(Magazine[] allMagazine, String title) {
int searchIndex = -1;
for (int i = 0; i < Magazine.getNumOfMagazine(); i++) {
if ((allMagazine[i].getTitle()).equalsIgnoreCase(title)) {
searchIndex = i;
break;
}
}
return searchIndex;
}
// create a magazine and return it.
public static Magazine createMagazine() {
// get user inputs and set it for the title
Magazine aMagazine = new Magazine();
try {
String title = JOptionPane.showInputDialog("Enter the magazine title");
aMagazine.setTitle(title);
} catch (IllegalArgumentException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
// get the user input for cost and set it
try {
double cost = Double.parseDouble(JOptionPane.showInputDialog("Enter the the price of the magazine"));
aMagazine.setCost(cost);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Error! cost must be a number");
} catch (IllegalArgumentException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
// get the user input for stock and set it.
try {
int stock = Integer.parseInt(JOptionPane.showInputDialog("Enter the the stock of the magazine"));
aMagazine.setCost(stock);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Error! stock must be a number");
} catch (IllegalArgumentException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
return aMagazine;
}// end of create magazine
public static void printMagazine(Magazine[] allMagazine) {
double totalCost = 0;
int numOfMagazine = Magazine.getNumOfMagazine();
String output = "XYZ BookStore\nMagazine Title | MagazineCost | Magazine Stock\n";
for (int i = 0; i < Magazine.getNumOfMagazine(); i++) {
totalCost += allMagazine[i].getCost();
output += allMagazine[i].toString() + "\n";
}
output += "The total number of Magazine " + Magazine.getNumOfMagazine();
output += "The average cost of the magazines " + (totalCost / numOfMagazine);
JOptionPane.showMessageDialog(null, output);
}
}// end class
// for (int i = 0; i < numOfMagazine; i++) {