-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmazon.java
More file actions
168 lines (105 loc) · 3.06 KB
/
Amazon.java
File metadata and controls
168 lines (105 loc) · 3.06 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package part_3;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class Amazon extends JFrame implements ActionListener{
JLabel l1 , l2 , l3 , l4 , l5 ;
JButton b1 , b2 , b3;
JTextField t1 , t2 , t3;
JTextArea outputArea;
JPanel p1 , p2 ;
String display = "";
double sum = 0;
Book b[];
int count = 0;
DecimalFormat fmt = new DecimalFormat("#.###");
public Amazon() {
super("Amazon");
b = new Book[10];
Container c = getContentPane();
p1 = new JPanel();
b1 = new JButton("ADD");
b2 = new JButton("Display all");
b3 = new JButton("Calculate Total");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
p1.setLayout(new GridLayout(3 , 1));
p1.add(b1); p1.add(b2); p1.add(b3);
c.add(p1 , BorderLayout.WEST);
l1 = new JLabel("Title: ");
l2 = new JLabel("Author: ");
l3 = new JLabel("Price: ");
l4 = new JLabel("Total Price: ");
l5 = new JLabel();
t1 = new JTextField(10);
t2 = new JTextField(10);
t3 = new JTextField(10);
p2 = new JPanel();
p2.setLayout(new GridLayout(4 , 2));
p2.add(l1);
p2.add(t1);
p2.add(l2);
p2.add(t2);
p2.add(l3);
p2.add(t3);
p2.add(l4);
p2.add(l5);
c.add(p2 , BorderLayout.EAST);
outputArea = new JTextArea(10 , 15);
outputArea.setEditable(false);
c.add(new JScrollPane(outputArea) , BorderLayout.SOUTH);
setSize(500,400);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b1) {
String author = t2.getText();
double price = Double.parseDouble(t3.getText());
b[count++] = new Book(t1.getText(), author ,price );
t1.setText("");
t2.setText("");
t3.setText("");
}
else if(e.getSource() == b2) {
sort(b);
for (int i = 0 ; i < count ; i++) {
display += b[i].toString();
outputArea.setText(display);
}}
else if(e.getSource() == b3) {
for(int i = 0; i < count ; i++) {
sum += b[i].getPrice();
}
l5.setText("$" + fmt.format(sum));
}
}
public void sort(Book b[]) {
for(int j = 1 ; j < count ; j++) {
for(int i = 0; i < count - 1 ; i++) {
if(b[i].getPrice() > b[i + 1].getPrice() ) {
Book hold = b[i];
b[i] = b[i + 1];
b[i + 1] = hold;}
}
}
}
public static void main(String[] args) {
Amazon app = new Amazon();
}
}
class Book{
private String name , author;
private double price;
private DecimalFormat fmt = new DecimalFormat("#.###");
public Book(String n , String a , double p) {
name = n;
author = a;
price = p;
}
public String getName() {return name;}
public String getAuthor() {return author;}
public double getPrice() {return price;}
public String toString() {return "Title: " + name + " ,\t" + "Author: " + author + " ,\t" + "Price to Purchase " + fmt.format(price) + "\n";}
}