import java.util.Vector; class Supermarket{ Vector market = null; Supermarket(){ market = new Vector(); } public void append(Goods g){ market.addElement(g); } public void delete(Goods g){ market.removeElement(g); } public void query(Goods g){ for(int i = 0;imarket.size();i++){ if((market.elementAt(i)).equals(g) == true){ g.showMe(); } } } } class Goods{ private String name; private double price; private int num; public Goods(String name,double price,int num){ this.name=name; this.price = price; this.num = num; } public void sale(double buyprice){ if(num==0){ System.out.println("购买商品已售空"); return; } if(buypricethis.price){ System.out.println("余额不足"); }else{ num--; } } public void add(int quantity){ num+=quantity; } public void showMe(){ System.out.println("商品名称:"+this.name+" 商品价格:"+this.price+"$ "+"商品个数:"+this.num); } } //很多地方给的不是很详细,只能这么凭理解写,测试main方法自己写。
成都创新互联公司是专业的肥城网站建设公司,肥城接单;提供成都做网站、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行肥城网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
2、public class Test11 {
public static void main(String[] args) {
System.out.println("滞贸旅客的人数" + (1189 - 108*10) );
}
}
3、public class Test11 {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
System.out.print("输入购买鸡蛋(斤):");
int number = scanner.nextInt();
if(number10){
throw new RuntimeException("不允许购买这么多!");
}
else {
System.out.println("用户应支付金额"+number*3.98);
}
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
}
用java Swing做的?给你个代码,还有运行结果图
package my.test.main;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Purchase extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JLabel label_9;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Purchase frame = new Purchase();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Purchase() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u5546\u54C11");
label.setBounds(56, 40, 54, 15);
contentPane.add(label);
JLabel label_1 = new JLabel("\u5546\u54C12");
label_1.setBounds(56, 70, 54, 15);
contentPane.add(label_1);
JLabel label_2 = new JLabel("\u5546\u54C13");
label_2.setBounds(56, 100, 54, 15);
contentPane.add(label_2);
JLabel label_3 = new JLabel("\u5546\u54C14");
label_3.setBounds(56, 130, 54, 15);
contentPane.add(label_3);
JLabel label_4 = new JLabel("\u5546\u54C15");
label_4.setBounds(56, 160, 54, 15);
contentPane.add(label_4);
JLabel label_5 = new JLabel("\u5546\u54C1\u540D\u79F0");
label_5.setBounds(56, 7, 54, 15);
contentPane.add(label_5);
JLabel label_6 = new JLabel("单价");
label_6.setBounds(179, 7, 54, 15);
contentPane.add(label_6);
JLabel label_7 = new JLabel("数量");
label_7.setBounds(307, 7, 54, 15);
contentPane.add(label_7);
JLabel label_8 = new JLabel("总价");
label_8.setBounds(145, 224, 54, 15);
contentPane.add(label_8);
label_9 = new JLabel("0");
label_9.setBounds(209, 224, 54, 15);
contentPane.add(label_9);
JLabel label_10 = new JLabel("10");
label_10.setBounds(185, 40, 54, 15);
contentPane.add(label_10);
JLabel label_11 = new JLabel("20");
label_11.setBounds(185, 70, 54, 15);
contentPane.add(label_11);
JLabel label_12 = new JLabel("30");
label_12.setBounds(185, 100, 54, 15);
contentPane.add(label_12);
JLabel label_13 = new JLabel("40");
label_13.setBounds(185, 130, 54, 15);
contentPane.add(label_13);
JLabel label_14 = new JLabel("50");
label_14.setBounds(185, 160, 54, 15);
contentPane.add(label_14);
textField = new JTextField();
textField.setBounds(295, 37, 66, 21);
contentPane.add(textField);
textField.setColumns(10);
textField.addKeyListener(new CalcTotle());
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(295, 67, 66, 21);
contentPane.add(textField_1);
textField_1.addKeyListener(new CalcTotle());
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(295, 97, 66, 21);
contentPane.add(textField_2);
textField_2.addKeyListener(new CalcTotle());
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(295, 127, 66, 21);
contentPane.add(textField_3);
textField_3.addKeyListener(new CalcTotle());
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(295, 157, 66, 21);
contentPane.add(textField_4);
textField_4.addKeyListener(new CalcTotle());
}
class CalcTotle extends KeyAdapter {
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
int count1, count2, count3, count4, count5;
if (!textField.getText().isEmpty()) {
count1 = Integer.parseInt(textField.getText()) * 10;
} else {
count1 = 0;
}
if (!textField_1.getText().isEmpty()) {
count2 = Integer.parseInt(textField_1.getText()) * 20;
} else {
count2 = 0;
}
if (!textField_2.getText().isEmpty()) {
count3 = Integer.parseInt(textField_2.getText()) * 30;
} else {
count3 = 0;
}
if (!textField_3.getText().isEmpty()) {
count4 = Integer.parseInt(textField_3.getText()) * 40;
} else {
count4 = 0;
}
if (!textField_4.getText().isEmpty()) {
count5 = Integer.parseInt(textField_4.getText()) * 50;
} else {
count5 = 0;
}
final int totle = count1 + count2 + count3 + count4 + count5;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
label_9.setText(totle + "");
}
});
}
}
}
package entity;
public class Market {
private int id;//id
private int num;//数量
private String goods;//商品
private double price;//价格
public Market(int id, int num, String goods, double price) {
super();
this.id = id;
this.num = num;
this.goods = goods;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getGoods() {
return goods;
}
public void setGoods(String goods) {
this.goods = goods;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double calc( ){
double sum=price*num;
System.out.println("您消费共计:"+sum+"¥");
return sum;
}
}
package test;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import entity.Market;
public class Test {
private static MapInteger,Market goods=new HashMapInteger, Market();
public static void main(String[] args) {
System.out.println("-------超市计价系统-------");
String goods1="可口可乐";
String goods2="爆米花";
String goods3="益达";
printTable("编号","商品","价格");
printTable("1",goods1,"3.0¥");
printTable("2",goods2,"5.0¥");
printTable("3",goods3,"10.0¥");
goods.put(1, new Market(1, 1, goods1, 3.0));
goods.put(2, new Market(2, 1, goods2, 5.0));
goods.put(3, new Market(3, 1, goods3, 10.0));
Scanner input = new Scanner(System.in);
System.out.println("请输入商品的编号:");
int num = input.nextInt();
System.out.println("请输入商品的数量");
int amount = input.nextInt();
Market market = goods.get(num);
market.setNum(amount);
market.calc();
}
private static void printTable(String row1,String row2,String row3 ) {
System.out.print(row1);
int times=12;
if (row2!="商品") {
times=5;
}
for (int i = 0; i times; i++) {
System.out.print(" ");
}
System.out.print(row2);
for (int i = 0; i 10; i++) {
System.out.print(" ");
}
System.out.print(row3);
System.out.println("\n");
}
}
//测试结果:
-------超市计价系统-------
编号 商品 价格
1 可口可乐 3.0¥
2 爆米花 5.0¥
3 益达 10.0¥
请输入商品的编号:
3
请输入商品的数量
5
您消费共计:50.0¥
以前学习java又做个实例,挺值得学习的。
1.首先我先列出我们所需要的java类结构。
1)Database.java --------- 模拟存储商品的数据库。
2)McBean.java ------------ 商品实体类,一个普通的javabean,里面有商品的基本属性。
3)OrderItemBean.java --- 订单表。
4)ShoppingCar.java ------ 这个就是最主要的购物车,当然比较简单。
5)TestShoppingCar.java --- 这个是测试类。
2.下面贴出具体代码并带关键注释。
---Database.java
public class Database {
/*采用Map存储商品数据,为什么呢?我觉得这个问题你自己需要想下。
* Integer 为Map的key值,McBean为Map的value值。
*/
private static MapInteger, McBean data = new HashMapInteger, McBean();
public Database() {
McBean bean = new McBean();
bean.setId(1);
bean.setName("地瓜");
bean.setPrice(2.0);
bean.setInstuction("新鲜的地瓜");
data.put(1, bean);//把商品放入Map
bean = new McBean();
bean.setId(2);
bean.setName("土豆");
bean.setPrice(1.2);
bean.setInstuction("又好又大的土豆");
data.put(2, bean);//把商品放入Map
bean = new McBean();
bean.setId(3);
bean.setName("丝瓜");
bean.setPrice(1.5);
bean.setInstuction("本地丝瓜");
data.put(3, bean);//把商品放入Map
}
public void setMcBean(McBean mcBean){
data.put(mcBean.getId(),mcBean);
}
public McBean getMcBean(int nid) {
return data.get(nid);
}
}
---McBean.java
public class McBean {
private int id;//商品编号
private String name;//商品名
private double price;//商品价格
private String instuction;//商品说明
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getInstuction() {
return instuction;
}
public void setInstuction(String instuction) {
this.instuction = instuction;
}
}
---ShoppingCar.java
public class ShoppingCar {
private double totalPrice; // 购物车所有商品总价格
private int totalCount; // 购物车所有商品数量
private MapInteger, OrderItemBean itemMap; // 商品编号与订单项的键值对
public ShoppingCar() {
itemMap = new HashMapInteger, OrderItemBean();
}
public void buy(int nid) {
OrderItemBean order = itemMap.get(nid);
McBean mb;
if (order == null) {
mb = new Database().getMcBean(nid);
order = new OrderItemBean(mb, 1);
itemMap.put(nid, order);
update(nid, 1);
} else {
order.setCount(order.getCount() + 1);
update(nid, 1);
}
}
public void delete(int nid) {
OrderItemBean delorder = itemMap.remove(nid);
totalCount = totalCount - delorder.getCount();
totalPrice = totalPrice - delorder.getThing().getPrice() * delorder.getCount();
}
public void update(int nid, int count) {
OrderItemBean updorder = itemMap.get(nid);
totalCount = totalCount + count;
totalPrice = totalPrice + updorder.getThing().getPrice() * count;
}
public void clear() {
itemMap.clear();
totalCount = 0;
totalPrice = 0.0;
}
public void show() {
DecimalFormat df = new DecimalFormat("¤#.##");
System.out.println("商品编号\t商品名称\t单价\t购买数量\t总价");
Set set = itemMap.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
OrderItemBean order = itemMap.get(it.next());
System.out.println(order.getThing().getId() + "\t"
+ order.getThing().getName() + "\t"
+ df.format(order.getThing().getPrice()) + "\t" + order.getCount()
+ "\t" + df.format(order.getCount() * order.getThing().getPrice()));
}
System.out.println("合计: 总数量: " + df.format(totalCount) + " 总价格: " + df.format(totalPrice));
System.out.println("**********************************************");
}
}
---OrderItemBean.java
public class OrderItemBean {
private McBean thing;//商品的实体
private int count;//商品的数量
public OrderItemBean(McBean thing, int count) {
super();
this.thing = thing;
this.count = count;
}
public McBean getThing() {
return thing;
}
public void setThing(McBean thing) {
this.thing = thing;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
---TestShoppingCar.java
package com.shop;
public class TestShoppingCar {
public static void main(String[] args) {
ShoppingCar s = new ShoppingCar();
s.buy(1);//购买商品编号1的商品
s.buy(1);
s.buy(2);
s.buy(3);
s.buy(1);
s.show();//显示购物车的信息
s.delete(1);//删除商品编号为1的商品
s.show();
s.clear();
s.show();
}
}
3.打印输出结果
商品编号 商品名称 单价 购买数量 总价
1 地瓜 ¥2 3 ¥6
2 土豆 ¥1.2 1 ¥1.2
3 丝瓜 ¥1.5 1 ¥1.5
合计: 总数量: ¥5 总价格: ¥8.7
**********************************************
商品编号 商品名称 单价 购买数量 总价
2 土豆 ¥1.2 1 ¥1.2
3 丝瓜 ¥1.5 1 ¥1.5
合计: 总数量: ¥2 总价格: ¥2.7
**********************************************
商品编号 商品名称 单价 购买数量 总价
合计: 总数量: ¥0 总价格: ¥0
**********************************************
4.打字太累了,比较匆忙,但是主要靠你自己领会。哪里不清楚再提出来。
哎,我还是自己代码黏贴给你吧!免得总通不过!装不下这么多字,详情自己找我。 连接数据库类(添加修改会员信息) package com.to.java; import java.util.Scanner; import java.sql.*; public class Vds { Scanner Input=new Scanner(System.in); ZhuCK1 zhuck1=new ZhuCK1(); public void x(){ try{ Connection con=null; Statement stmt=null; ResultSet rs=null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String ur1="jdbc:odbc:gg"; con=DriverManager.getConnection(ur1); stmt=con.createStatement(); String sqlstmt="select id,mm from gg"; rs=stmt.executeQuery(sqlstmt); System.out.println("----------查询结果---------"); System.out.println(); System.out.println("会员号--密码"); while(rs.next()){ String id=rs.getString("id"); String mm=rs.getString("mm"); System.out.println(id+" "+mm); } con.close(); stmt.close(); }catch(Exception e){ System.out.println(e);} System.out.println("是否返回上级菜单"); System.out.println("1.是"); System.out.println("2.否"); int m=Input.nextInt(); switch(m){ case 1: zhuck1.ZhucK1(); break; case 2: { System.out.println("欢迎下次再次使用");System.exit(-1);}; break; }} public void j(){ try{ Connection con=null; Statement stmt=null; ResultSet rs=null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String ur1="jdbc:odbc:gg"; con=DriverManager.getConnection(ur1); stmt=con.createStatement(); String sqlstmt="select id,mm from gg"; rs=stmt.executeQuery(sqlstmt); while(rs.next()){ String id=rs.getString("id"); String mm=rs.getString("mm"); System.out.println(id+" "+mm); } Scanner input=new Scanner(System.in); System.out.println("请输入会员信息:"); System.out.println("请输入会员号:"); String f=input.next(); System.out.println("请输入密码:"); String q=input.next(); String sqlst="insert into gg values('"+f+"','"+q+"')"; Statement tatement=con.createStatement(); int result=tatement.executeUpdate(sqlst); System.out.println("操作成功!"); con.close(); stmt.close(); }catch(Exception e){ System.out.println(e); } System.out.println("是否返回上级菜单"); System.out.println("1.是"); System.out.println("2.否"); int m=Input.nextInt(); switch(m){ case 1: zhuck1.ZhucK1(); break; case 2: { System.out.println("欢迎下次再次使用");System.exit(-1);}; break; } } public void xg(){ try{ Connection con=null; Statement stmt=null; ResultSet rs=null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String ur1="jdbc:odbc:gg"; con=DriverManager.getConnection(ur1); stmt=con.createStatement(); String sqlstmt="select id,mm from gg"; rs=stmt.executeQuery(sqlstmt); while(rs.next()){ String id=rs.getString("id"); String mm=rs.getString("mm"); System.out.println(id+" "+mm); } Scanner input=new Scanner(System.in); System.out.println("请输入会员信息:"); System.out.println("请输入要修改的会员号:"); String f=input.next(); System.out.println("请输入修改的密码:"); String q=input.next(); String sqlst="update gg set mm='"+q+"' where id='"+f+"'"; Statement tatement=con.createStatement(); int result=tatement.executeUpdate(sqlst); System.out.println("操作成功!"); con.close(); stmt.close(); }catch(Exception e){ System.out.println(e); } System.out.println("是否返回上级菜单"); System.out.println("1.是"); System.out.println("2.否"); int m=Input.nextInt(); switch(m){ case 1: zhuck1.ZhucK1(); break; case 2: { System.out.println("欢迎下次再次使用");System.exit(-1);}; break; } } } package com.to.java; import java.util.Scanner; public class KeHu { Vds sj=new Vds(); public void kehu1(){ Scanner Input=new Scanner(System.in); ZhuCK1 zhuck1=new ZhuCK1(); System.out.println("******************************************************"+"\n\n"); System.out.println("1.显示所有客户信息"); System.out.println("2.添加客户信息"); System.out.println("3.修改客户信息"); System.out.println("4.查询客户信息"); System.out.println("5.返回上级菜单"); System.out.println("******************************************************"+"\n\n"); System.out.println("请输入数字:"); int a=Input.nextInt(); switch(a){ case 1: sj.x(); break; case 2: sj.j(); break; case 3: sj.xg(); break; case 4: sj.x();break; case 5: zhuck1.ZhucK1();break; } } } package com.to.java; public class GouWuXiTong { public static void main(String[] args){ ZhuCK zhuck=new ZhuCK(); zhuck.ZhucK(); } } package com.to.java; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Scanner; public class ZhuCK { Scanner Input=new Scanner(System.in); public void ZhucK(){ ZhuCK1 zhuck1=new ZhuCK1(); System.out.println("\t\t"+"欢迎使用我心我素购物系统"); System.out.println("******************************************************"+"\n\n"); System.out.println("\t\t"+"1.登陆系统"); System.out.println(); System.out.println("\t\t"+"2.退出"+"\n\n"); System.out.println("******************************************************"); int d=1; while(d!=2){ System.out.println("请输入会员号:"); String p=Input.next(); System.out.println("请输入密码:"); String f=Input.next(); try{ Connection con=null; Statement stmt=null; ResultSet rs=null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String ur1="jdbc:odbc:gg"; con=DriverManager.getConnection(ur1); stmt=con.createStatement(); String sqlstmt="select * from gg"; rs=stmt.executeQuery(sqlstmt); while(rs.next()){ String id=rs.getString("id"); String mm=rs.getString("mm"); if(id.equals(p)mm.equals(f)){ { System.out.println("请输入数字:"); int a=Input.nextInt(); switch(a){ case 1: zhuck1.ZhucK1(); break; case 2: break; }d=2; } } } }catch(Exception e){ System.out.println(e);} {System.out.println("输入错误请再次输入:");d=1;} } }} }