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, "护腕", 100);
arr.add(g1);
arr.add(g2);
arr.add(g3);
System.out.println("***********************商品查询系统*********************");
System.out.println("1.羽毛球 2.羽毛球拍 3.护腕");
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;
}
}
很辛苦写的希望能采纳
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¥
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方法自己写。
/**
* 超市管理系统
* @author Administrator
*
*/
public class ChaoShiSystemManager {
public static void main(String[] args) {
new HuoWuDomImpl().start();
}
}
class HuoWu{
public String bianhao; //商品编号
public String name; //商品名称
public double jiage; //商品价格
}
class HuoWuDomImpl{
private static java.util.ArrayListHuoWu list = new java.util.ArrayListHuoWu();
public void start(){
int num = 0;
System.out.println("--欢迎登录超市管理系统--");
System.out.println("1、添加商品");
System.out.println("2、查询商品");
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.println("请输入模块名称:");
try {
num = sc.nextInt();
if(num == 1){
setHuoWu();
new HuoWuDomImpl().start();
}else if(num == 2){
getSeleteHuoWu();
new HuoWuDomImpl().start();
}else{
throw new RuntimeException("提示:抱歉,模块正在开发中");
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("提示:对不起,您的输入有误!");
}finally{
sc.close();
}
}
public void setHuoWu(){
HuoWu hw = new HuoWu();
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.println("请输入商品编号:");
hw.bianhao = sc.next();
System.out.println("请输入商品名称:");
hw.name = sc.next();
System.out.println("请输入商品价格");
try {
hw.jiage = sc.nextDouble();
} catch (Exception e) {
throw new RuntimeException("提示:商品价格输入错误!");
}finally{
sc.close();
}
list.add(hw);
System.out.println("提示:恭喜,添加成功!");
}
public void getSeleteHuoWu(){
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.println("请输入商品编号...");
String bianhao = sc.next();
java.util.Iterator it = list.iterator();
while(it.hasNext()){
if(bianhao.equals(((HuoWu)it.next()).bianhao)){
System.out.println("商品编号:" + ((HuoWu)it.next()).bianhao);
System.out.println("商品名称:" + ((HuoWu)it.next()).name);
System.out.println("商品价格:" + ((HuoWu)it.next()).jiage);
}
}
}
}
PS:程序可能还有点问题,自己调试一下吧。改天有空我再来更新。
要使用Java编写超市购物程序,您需要使用Java数据库编程技术来连接到您的数据库并执行相关的数据库操作。在Java中,您可以使用JDBC(Java数据库连接)API来连接到数据库并执行SQL语句。例如,您可以使用JDBC API来执行以下操作:
创建数据库连接
执行增删改查(CRUD)操作
提交或回滚事务
执行存储过程和函数
查询数据库元数据
此外,您还可以使用Java的面向对象编程技术来封装数据库操作,以便更方便地在您的程序中使用。例如,您可以创建一个类来表示超市商品,并定义一些方法来执行商品的增删改查操作。这样,您就可以在程序中通过调用这些方法来简单地完成对数据库的操作,而不需要编写复杂的SQL语句。
import java.util.Scanner;
public class colPrice{
public static void main(String[] args){
double[] price = new double[10];
double totalPrice = 0;
System.out.println("请输入商品价格:");
for(int i=0;i10;i++){
System.out.print(i+1+": ");
Scanner in = new Scanner( System.in);
price[i] = in.nextDouble();
}
System.out.println("");
for(int i= 0;iprice.length;i++){
totalPrice+=price[i];
}
System.out.println("商品总价格是:"+totalPrice);
}
}