package?entity;
創(chuàng)新互聯(lián)專注于張北網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供張北營銷型網(wǎng)站建設(shè),張北網(wǎng)站制作、張北網(wǎng)頁設(shè)計、張北網(wǎng)站官網(wǎng)定制、小程序制作服務(wù),打造張北網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供張北網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
public?class?Market?{
private?int?id;//id
private?int?num;//數(shù)量
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("您消費(fèi)共計:"+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("-------超市計價系統(tǒng)-------");
String?goods1="可口可樂";
String?goods2="爆米花";
String?goods3="益達(dá)";
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("請輸入商品的數(shù)量");
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");
}
}
//測試結(jié)果:
-------超市計價系統(tǒng)-------
編號????????????商品??????????價格
1?????可口可樂??????????3.0¥
2?????爆米花??????????5.0¥
3?????益達(dá)??????????10.0¥
請輸入商品的編號:
3
請輸入商品的數(shù)量
5
您消費(fèi)共計:50.0¥
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;public class ShoppingCartManager {
HashMapString, String hm=new HashMapString, String();
float totlePrice=0;
//添加book到購物車
public void addBook(String bookId,String bookQuantity){
if(hm.containsKey(bookId)){
int value=Integer.parseInt(hm.get(bookId));
value+=Integer.parseInt(bookQuantity);
hm.put(bookId, value+"");
}else{
hm.put(bookId, bookQuantity);
}
}
//修改數(shù)量
public void updateQuantity(String bookId,String bookQuantity){
hm.put(bookId, bookQuantity);
}
//獲取購物車的所有信息 并計算總價
public ArrayListBookBean getShoppingCart(){
ArrayListBookBean al=new ArrayListBookBean();
IteratorString i=hm.keySet().iterator();
String ids="";
BookTableManager btm=new BookTableManager();
while(i.hasNext()){
ids=ids+","+i.next();
}
al= btm.selectByBookIds(ids);
totlePrice=0; //清空總價,防止無限累計
for(int j=0;jal.size();j++){
BookBean bb=al.get(j);
totlePrice+=bb.getPrice()*Integer.parseInt(getQuantityById(bb.getBookId()+""));
}
return al;
}
//獲取總價
public float getTotlePrice(){
return totlePrice;
}
//根據(jù)ID獲取數(shù)量
public String getQuantityById(String id){
String quantity=hm.get(id);
return quantity;
}
//清空購物車
public void clear(){
hm.clear();
}
//刪除購物車中的一本書
public void deleteById(String id){
hm.remove(id);
}
}
package?sum;
import?java.util.ArrayList;
import?java.util.Scanner;
public?class?TestDemo?{
public?static?void?main(String[]?args){
ArrayListGoods?arr?=?new?ArrayListGoods();
Goods?g1?=?new?Goods(1,?"羽毛球",?237);
Goods?g2?=?new?Goods(2,?"羽毛球拍",?113);
Goods?g3?=?new?Goods(3,?"護(hù)腕",?100);
arr.add(g1);
arr.add(g2);
arr.add(g3);
System.out.println("***********************商品查詢系統(tǒng)*********************");
System.out.println("1.羽毛球???2.羽毛球拍??3.護(hù)腕");
System.out.println("*****************************************************");
Scanner?sc?=?new?Scanner(System.in);
outer:?while(true){
System.out.print("請選擇商品號:");
String?num?=?sc.next();
for(int?i?=?0;?i??arr.size();?i++){
if((arr.get(i).getId()?+?"").equals(num)){
System.out.println(arr.get(i).getName()?+?"?"?+?arr.get(i).getPrice()?+?"元"?);
}else?if(num.equals("n")){
System.out.println("謝謝使用");
break?outer;
}
}
}
}
}
class?Goods{
public?Goods(int?id,?String?name,?int?price){
this.id?=?id;
this.name?=?name;
this.price?=?price;
}
private?int?id;
private?String?name;
private?int?price;
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?int?getPrice()?{
return?price;
}
public?void?setPrice(int?price)?{
this.price?=?price;
}
}
很辛苦寫的希望能采納
使用結(jié)構(gòu)體+數(shù)組,就可以 了
~
~
~
~
~~~~~~~~~~~~~~~~~~~~~~~~~
以前學(xué)習(xí)java又做個實(shí)例,挺值得學(xué)習(xí)的。
1.首先我先列出我們所需要的java類結(jié)構(gòu)。
1)Database.java --------- 模擬存儲商品的數(shù)據(jù)庫。
2)McBean.java ------------ 商品實(shí)體類,一個普通的javabean,里面有商品的基本屬性。
3)OrderItemBean.java --- 訂單表。
4)ShoppingCar.java ------ 這個就是最主要的購物車,當(dāng)然比較簡單。
5)TestShoppingCar.java --- 這個是測試類。
2.下面貼出具體代碼并帶關(guān)鍵注釋。
---Database.java
public class Database {
/*采用Map存儲商品數(shù)據(jù),為什么呢?我覺得這個問題你自己需要想下。
* 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; // 購物車所有商品數(shù)量
private MapInteger, OrderItemBean itemMap; // 商品編號與訂單項(xiàng)的鍵值對
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購買數(shù)量\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("合計: 總數(shù)量: " + df.format(totalCount) + " 總價格: " + df.format(totalPrice));
System.out.println("**********************************************");
}
}
---OrderItemBean.java
public class OrderItemBean {
private McBean thing;//商品的實(shí)體
private int count;//商品的數(shù)量
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.打印輸出結(jié)果
商品編號 商品名稱 單價 購買數(shù)量 總價
1 地瓜 ¥2 3 ¥6
2 土豆 ¥1.2 1 ¥1.2
3 絲瓜 ¥1.5 1 ¥1.5
合計: 總數(shù)量: ¥5 總價格: ¥8.7
**********************************************
商品編號 商品名稱 單價 購買數(shù)量 總價
2 土豆 ¥1.2 1 ¥1.2
3 絲瓜 ¥1.5 1 ¥1.5
合計: 總數(shù)量: ¥2 總價格: ¥2.7
**********************************************
商品編號 商品名稱 單價 購買數(shù)量 總價
合計: 總數(shù)量: ¥0 總價格: ¥0
**********************************************
4.打字太累了,比較匆忙,但是主要靠你自己領(lǐng)會。哪里不清楚再提出來。
哎,我還是自己代碼黏貼給你吧!免得總通不過!裝不下這么多字,詳情自己找我。 連接數(shù)據(jù)庫類(添加修改會員信息) 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("----------查詢結(jié)果---------"); 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("請輸入數(shù)字:"); 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"+"歡迎使用我心我素購物系統(tǒng)"); System.out.println("******************************************************"+"\n\n"); System.out.println("\t\t"+"1.登陸系統(tǒng)"); 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("請輸入數(shù)字:"); 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;} } }} }