重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
参考了别人的代码。略作修改,已经很简单了:
创新互联是一家专业提供泰安企业网站建设,专注与成都网站制作、成都做网站、H5建站、小程序制作等业务。10年已为泰安众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。
InfoATM.java:
public class InfoATM {
double money = 0;
public InfoATM(double cash) {
super();
this.money = cash;
}
// 存款的方法
public void save(double count) {
money += count;
}
// 取款的方法
public void draw(double count) {
money -= count;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
TestATM.java:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestATM extends JFrame {
private static final long serialVersionUID = 2531222181184935595L;
// 主面板pnBasic是用来装pnDate和标签文字的。
private JPanel pnBasic;
// 添加到主面板中的中间 pnDate面板是为了装表单的。
private JPanel pnDate;
// 添加到主面板中的北边 pnLabel面板是为了装欢迎词的
private JPanel pnLabel;
InfoATM atm = new InfoATM(0);
public TestATM() {
pnBasic = new JPanel();
// 主面板pnBasic是用来装pnDate和标签文字的。
pnDate = new JPanel(new GridLayout(2, 2));
// pnDate面板是为了装表单的。
pnLabel = new JPanel();
JLabel top = new JLabel("欢迎来到中国银行!");
pnLabel.add(top);
// 先将数值添加在一个容器中并设置其在容器的右边,在将容器添加在网格的第一格
JPanel jp1 = new JPanel();
JLabel number = new JLabel("数值:");
final JTextField box = new JTextField(5);
jp1.add(number);
jp1.add(box);
JPanel jp2 = new JPanel();
JButton create = new JButton("新建银行账户");
jp2.add(create);
JButton take = new JButton("取款");
JButton in = new JButton("存款");
pnDate.add(jp1);
pnDate.add(jp2);
pnDate.add(take);
pnDate.add(in);
// 加一句下面的就好了
JPanel jpS = new JPanel();
final JLabel total = new JLabel("您现在的账户余额是:0 元");
jpS.add(total);
pnBasic.setLayout(new BorderLayout());
pnBasic.add(pnLabel, BorderLayout.NORTH);
pnBasic.add(pnDate, BorderLayout.CENTER);
pnBasic.add(jpS, BorderLayout.SOUTH);
setContentPane(pnBasic);
setBounds(400, 250, 500, 500);
pack();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
pack();
in.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (box.getText() != null box.getText() != "") {
try {
double count = Double.parseDouble(box.getText());
if (count 0) {
atm.save(count);
total.setText("您现在的账户余额是:" + atm.getMoney() + "元");
box.setText("");
}
} catch (Exception e1) {
System.out.println("您输入的数值必须是数字");
}
}
}
});
take.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (box.getText() != null box.getText() != "") {
try {
double count = Double.parseDouble(box.getText());
if (count = 0 count = atm.getMoney()) {
atm.draw(count);
total.setText("您现在的账户余额是:" + atm.getMoney() + "元");
box.setText("");
} else {
System.out.println("你的余额不足,取款失败");
}
} catch (Exception e1) {
System.out.println("您输入的数值必须是数字");
}
}
}
});
create.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
total.setText("您现在的账户余额是:0元");
atm.setMoney(0);
box.setText("");
}
});
}
public static void main(String[] args) {
new TestATM();
}
}
最佳答案代码如下:
public class ElevatorModel implenent ElevatorModelListener{
ElevatorShaft elevatorShaft;//电梯车箱
Floor firstFloor;
Floor secondFloor;
public void addPerson(Person person);
//实现ElevatorModelListener接口的所有方法
}
public abstract class Location{
String locationName;//可能占据的三个位置firstFloor; secondFloor;elevator中的其中一个位置。
public Button getButton();
public Door getDoor();
}
public class Floor extends Location{
public Floor(String firstFloor,String secondFloor);
public Button getButton();//具体实现
public Door getDoor();//具体实现
public ElevatorShaft getElevatorShaft();
}
public class Door implenent ElevatorMoveListener{
boolean open;//门是开的关的
public void openDoor();//开门
public viod clossDoor();//关门
}
public class Button implenent ElevatorMoveListener{
boolean pressed;
public void pressButton();//按下按钮
public void resetButton();//释放按钮
public void elevatorArrived();//电梯到来
}
public class ElevatorShaft{//电梯乘箱
Elevator elevator;//电梯
Button firstFloorButton,secondFloorButton;//出发层的按钮和到达层的按钮 Door firstFloorDoor,secondFloorDoor;//出发的门和到达的门
Light firstFloorLight,secondFloorLight;//灯
//监听器
DoorListener doorListener;
ButtonListener buttonListener;
LightListener ligthListener;
BellListener bellListener;
ElevatorMoveListener elevatorMoveListener;
Set set;
}
public class Light implenent ElevatorMoveListener{
boolean lightOn;//代表灯的状态,亮或灭
public void turnOnLight();
public void turnOffLight();
public void elevatorDeparted();//具体实现
public void elevatorArrived();//具体实现
}
public class Bell implenent ElevatorMoveListener{
public void ringBell();
public void elevatorDeparted();//具体实现
public void elevatorArrived();//具体实现
}
public class Elevator extends Location implenets
ButtonListener,DoorListener,BellListener,Runable
{//电梯乘箱
boolean moving;
boolean summoned;//描述Elevator是在移动还是已经补召唤
Floor currentFloor;//当前正在提供服务的Floor
Floor destinationFloor ; //代表将要到达的Floor
Button elevatorButton;
Door elevatorDoor;
Bell bell;
public void run();// 实现
public void requestElevator();//请求Elevator并产生排队请求。 }
public class Person extends Thread{
Location类对象(或是位于Floor上,或是出处在Elevator中); int ID;
boolean moving;
public void run();//具体实现
public void enterAndRideElevator();
}
//TV类
public class Tv {
public void open(){
System.out.println("打开电视机");
}
public void close(){
System.out.println("关闭电视机");
}
public void search(int x){
System.out.println("您搜索到的是"+x+"号频道");
}
public void ctrlVol(String s){
int vol=10;
if(s.equals("+"))
vol++;
if(s.equals("-"))
vol--;
System.out.println("当前音量为"+vol);
}
public void changeChannel(int x){
if( x 29 )
System.out.println("没有该频道");
else
System.out.println("当前"+x+"号频道");
}
public void changeChannel(String s){
int x=0;
if(s.equals("+"))
x++;
if(s.equals("-"))
x--;
System.out.println("当前"+x+"号频道");
}
}
//Controller遥控器类
public class Controller{
public void openTv(Tv tv){
tv.open();
}
public void closeTv(Tv tv){
tv.close();
}
public void searchTv(Tv tv){
int x=(int)(Math.random()*30);
tv.search(x);
}
public void ctrlTvVol(Tv tv){
tv.ctrlVol("+");
}
public void changeTvChannel(Tv tv){
tv.changeChannel("+");
tv.changeChannel(23);
}
}
我的思路:
1.定义一个开关的接口(ISWitcher),用于设定开关的功能
2.定义开关类实现开关接口的功能
3.定义电器类,所有电器的父类
4.对电灯,投影仪,风扇等电器进行定义
5.进行测试
}else if(comm.equals("SORT")){
String course=s.next();
sort(course);
}else if(comm.equals("GET")){
String student=s.next();
String course=s.next();
get(student,course);
}else if(comm.equals("EXIT")){
break;
}else{
System.out.println("命令格式不正确,请重新输入!");
}
}