我目前是一名信息系统专业的学生,已经完成了最后一年的编程课程。我一点也不擅长编程,只能依靠谷歌和课堂笔记来执行最简单的任务。
我的任务是设计一个简单的Supermarket应用程序,它有10种商品。用户点击一个项目并选择数量/重量,然后在底部的文本框中添加到总数中。单击时,每个项目都会添加到总和中。
我已经做了我糟糕的技术所能做的,但我有两个错误。
-
我在程序中手动写入了10个项目。它们需要通过访问数据库进行连接。我在谷歌上搜索了这个方法,发现Netbeans在这个功能上确实有很大帮助,通常只需要一行,比如
DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver(*.mdb(};Dbq=d:\test.mdb"(;
我不知道如何连接数据库,也不知道在连接到Netbeans时如何提取或使用信息。
- 一开始,我对它进行了编程,一次只能选择一个项目。其余部分将变为空白,只有单击的项目的总和才会出现在下面的框中。就我的一生而言,我不知道如何在点击时添加项目。我很确定这是一个简单的SUM函数,但我很困惑
如有任何建议,我们将不胜感激。请尽可能简单地解释。我知道基本知识,但我的能力几乎没有达到"你好世界"程序lol
import static javax.swing.JOptionPane.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class Supermarket1 extends JFrame implements ActionListener {
Connection conn = null;
JButton oneP, twoP, fiveP, tenP, twentyP, fiftyP, onePound, twoPounds, fivePounds, tenPounds, twentyPounds, fiftyPounds, beans, flakes, sugar, tea, coffee, bread, sausage, egg, milk, potato;
JLabel messLabel = new JLabel("Amount to pay: ");
JTextField message = new JTextField(10);
int amount = 0; // payment in pence
DecimalFormat pounds = new DecimalFormat("£0.00");
public static void main(String[] args) {
Supermarket1 c = new Supermarket1();
c.setTitle("Supermarket payment simulator");
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setSize(500, 200);
c.setVisible(true);
}
Supermarket1() {
setLayout(new BorderLayout());
oneP = new JButton("1p"); oneP.addActionListener(this);
twoP = new JButton("2p"); twoP.addActionListener(this);
fiveP = new JButton("5p"); fiveP.addActionListener(this);
tenP = new JButton("10p"); tenP.addActionListener(this);
twentyP = new JButton("20p"); twentyP.addActionListener(this);
fiftyP = new JButton("50p"); fiftyP.addActionListener(this);
onePound = new JButton("£1"); onePound.addActionListener(this);
twoPounds = new JButton("£2"); twoPounds.addActionListener(this);
fivePounds = new JButton("£5"); fivePounds.addActionListener(this);
tenPounds = new JButton("£10"); tenPounds.addActionListener(this);
twentyPounds = new JButton("£20"); twentyPounds.addActionListener(this);
fiftyPounds = new JButton("£50"); fiftyPounds.addActionListener(this);
beans = new JButton("Baked Beans"); beans.addActionListener(this);
flakes = new JButton("Corn Flakes"); flakes.addActionListener(this);
sugar = new JButton("Sugar"); sugar.addActionListener(this);
tea = new JButton("Tea Bags"); tea.addActionListener(this);
coffee = new JButton("Instant Coffee"); coffee.addActionListener(this);
bread = new JButton("Bread"); bread.addActionListener(this);
sausage = new JButton("Sausage"); sausage.addActionListener(this);
egg = new JButton("Egg"); egg.addActionListener(this);
milk = new JButton("Milk"); milk.addActionListener(this);
potato = new JButton("Potatoes"); potato.addActionListener(this);
rightButtons();
JPanel leftSide = new JPanel();
leftSide.setLayout(new GridLayout(5, 1));
leftSide.add(oneP);
leftSide.add(twoP);
leftSide.add(fiveP);
leftSide.add(tenP);
leftSide.add(twentyP);
leftSide.add(fiftyP);
leftSide.add(onePound);
leftSide.add(twoPounds);
leftSide.add(fivePounds);
leftSide.add(tenPounds);
leftSide.add(twentyPounds);
leftSide.add(fiftyPounds);
add("West", leftSide);
JPanel rightSide = new JPanel();
rightSide.setLayout(new GridLayout(10, 1));
rightSide.add(beans);
rightSide.add(flakes);
rightSide.add(sugar);
rightSide.add(tea);
rightSide.add(coffee);
rightSide.add(bread);
rightSide.add(sausage);
rightSide.add(egg);
rightSide.add(milk);
rightSide.add(potato);
add("East", rightSide);
JPanel middle = new JPanel();
middle.setLayout(new FlowLayout());
middle.add(messLabel);
message.setEditable(false);
middle.add(message);
add("Center", middle);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == beans) { amount = 35; leftButtons(); }
if (e.getSource() == flakes) { amount = 100; leftButtons(); }
if (e.getSource() == sugar) { amount = 50; leftButtons(); }
if (e.getSource() == tea) { amount = 115; leftButtons(); }
if (e.getSource() == coffee) { amount = 250; leftButtons(); }
if (e.getSource() == bread) { amount = 50; leftButtons(); }
if (e.getSource() == sausage) { amount = 130; leftButtons(); }
if (e.getSource() == egg) { amount = 75; leftButtons(); }
if (e.getSource() == milk) { amount = 65; leftButtons(); }
if (e.getSource() == potato) { amount = 125; leftButtons(); }
if (e.getSource() == oneP) amount -= 1;
if (e.getSource() == twoP) amount -= 2;
if (e.getSource() == fiveP) amount -= 5;
if (e.getSource() == tenP) amount -= 10;
if (e.getSource() == twentyP) amount -= 20;
if (e.getSource() == fiftyP) amount -= 50;
if (e.getSource() == onePound) amount -= 100;
if (e.getSource() == twoPounds) amount -= 200;
if (e.getSource() == fivePounds) amount -= 500;
if (e.getSource() == tenPounds) amount -= 1000;
if (e.getSource() == twentyPounds) amount -= 2000;
if (e.getSource() == fiftyPounds) amount -= 5000;
if (amount > 0) message.setText(pounds.format(amount / 100.0));
else {
message.setText("");
if (amount < 0) {
int change = -amount;
showMessageDialog(this, "Your change is "
+ pounds.format(change / 100.0)
+ coins(change),
"Change", JOptionPane.INFORMATION_MESSAGE);
} else {
showMessageDialog(this, "Thank you",
"Exact amount", JOptionPane.INFORMATION_MESSAGE);
}
rightButtons();
}
}
// enable left buttons, disable right buttons
void leftButtons() {
oneP.setEnabled(true);
twoP.setEnabled(true);
fiveP.setEnabled(true);
tenP.setEnabled(true);
twentyP.setEnabled(true);
fiftyP.setEnabled(true);
onePound.setEnabled(true);
twoPounds.setEnabled(true);
fivePounds.setEnabled(true);
tenPounds.setEnabled(true);
twentyPounds.setEnabled(true);
fiftyPounds.setEnabled(true);
beans.setEnabled(true);
flakes.setEnabled(true);
sugar.setEnabled(true);
tea.setEnabled(true);
coffee.setEnabled(true);
bread.setEnabled(true);
sausage.setEnabled(true);
egg.setEnabled(true);
milk.setEnabled(true);
potato.setEnabled(true);
}
// enable right buttons, disable left buttons
void rightButtons() {
oneP.setEnabled(false);
twoP.setEnabled(false);
fiveP.setEnabled(false);
tenP.setEnabled(false);
twentyP.setEnabled(false);
fiftyP.setEnabled(false);
onePound.setEnabled(false);
twoPounds.setEnabled(false);
fivePounds.setEnabled(false);
tenPounds.setEnabled(false);
twentyPounds.setEnabled(false);
fiftyPounds.setEnabled(false);
beans.setEnabled(true);
flakes.setEnabled(true);
sugar.setEnabled(true);
tea.setEnabled(true);
coffee.setEnabled(true);
bread.setEnabled(true);
sausage.setEnabled(true);
egg.setEnabled(true);
milk.setEnabled(true);
potato.setEnabled(true);
}
String coins(int change) {
String answer = ":";
if (change >= 5000) {
answer += "nOne £50 note";
change -= 5000;
}
if (change >= 4000) {
answer += "nTwo £20 notes";
change -= 4000;
}
if (change >= 2000) {
answer += "nOne £20 note";
change -= 2000;
}
if (change >= 1000) {
answer += "nOne £10 note";
change -= 1000;
}
if (change >= 500) {
answer += "nOne £5 note";
change -= 500;
}
if (change >= 400) {
answer += "nTwo £2 coin";
change -= 400;
}
if (change >= 200) {
answer += "nOne £2 coin";
change -= 200;
}
if (change >= 100) {
answer += "nOne £1 coin";
change -= 100;
}
if (change >= 50) {
answer += "nOne 50p coin";
change -= 50;
}
if (change >= 40) {
answer += "nTwo 20p coins";
change -= 40;
}
if (change >= 20) {
answer += "nOne 20p coin";
change -= 20;
}
if (change >= 10) {
answer += "nOne 10p coin";
change -= 10;
}
if (change >= 5) {
answer += "nOne 5p coin";
change -= 5;
}
if (change >= 4) {
answer += "nTwo 2p coin";
change -= 4;
}
if (change >= 2) {
answer += "nOne 2p coin";
change -= 2;
}
if (change >= 1) {
answer += "nOne 1p coin";
change -= 1;
}
return answer;
}
}
NetBeans只是一个IDE;你没有将任何东西连接到NetBeans。
您的Java代码就是连接到Access数据库的代码。
您正在做的是建立JDBC连接。我推荐JDBC教程。
听起来你犯了一个典型的学生错误:分解能力差。
你可以通过把大问题分解成你能处理的小问题来解决。
您有一个用户界面、数据库访问和应用程序,所有这些都在这里进行。我建议您先从数据库访问开始。编写一个Java对象来处理您需要的内容。彻底测试它,然后使用它对用户界面进行下一步操作。
从修改代码
DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=d:test.mdb");
使用双反斜杠(\
(
DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=d:\test.mdb");
public class StudentFunctions implements StudentInterface
{
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=//////URL where your database is located//////";
String query = "";
Connection conn;
Statement s;
@Override
public boolean AddStudent(String name, String age, String contact, String program) {
try {
Connection conn = DriverManager.getConnection(database, "", "");
Statement s = conn.createStatement();
query ="insert into StudentRecord([stname],[age],[contact],[program])values('"+name+"','"+age+"','"+contact+"','"+program+"')";
if(s.executeUpdate(query) > 0)
{
JOptionPane.showMessageDialog(null,"Record successfully add in database");
return true;
} else {
return false;
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,ex.getMessage());
Logger.getLogger(StudentFunctions.class.getName()).log(Level.SEVERE, null, ex);
}
这是我连接访问我的学生记录项目的代码,希望它能对你有所帮助。