oop 商品信息按商品名称查询 商品按价格排序 内含测试类
来源:互联网 发布:uwp下载软件 编辑:程序博客网 时间:2024/04/28 01:30
class Product
public class Product{
private int pid;//商品编号
private String pname;//商品名称
private double price;//商品价格
private int pnum;//商品数量
public Product(){
}
public Product(int pid,String pname,double price,int pnum){
this.pid=pid;
this.pname=pname;
this.price=price;
this.pnum=pnum;
}
public double getPrice(){
return this.price;
}
public String getPname(){
return this.pname;
}
public void showInfo(){
System.out.println("商品编号"+this.pid+"商品名称"+this.pname+"商品价格"+this.price+"商品数量"+this.pnum);
}
}
class Test
import java.util.Random;import java.util.Scanner;public class Test {public static void main(String [] args){//存储商品信息 声明一个存储商品信息的数据Product [] products=new Product[5];Random rd=new Random();//从键盘上输入Scanner sc=new Scanner (System.in);for(int i=0;i<products.length;i++){System.out.println("请输入商品编号");int pid=sc.nextInt();sc.nextLine();System.out.println("请输入商品名称");String pname=sc.nextLine();System.out.println("请输入商品单价");double price=sc.nextDouble();System.out.println("请输入商品数量");int pnum=sc.nextInt();//使用数组下标访问每个商品信息products[i]=new Product(pid,pname,price,pnum);}//打印商品信息print(products);sc.nextLine();System.out.println("请输入你要查找的商品信息");String name=sc.nextLine();//调用查找的方法int index=findByName(products,name);if(index<0){System.out.println("查无此商品");}else{products[index].showInfo();//打印商品信息}System.out.println("请对商品进行排序(desc/asc):");String input=sc.nextLine();order(products,input);System.out.println("排序之后的结果:");print(products);}//显示所有商品信息public static void print(Product [] products){if(null==products){System.out.println("数组不能为空");return ;}for(Product p:products){p.showInfo();//显示信息}}//根据商品名称查找商品信息public static int findByName(Product []products,String name){if(null==products){System.out.println("数组为空");return -1;}//去除字符串的空格for(int i=0;i<products.length;i++){if(name.trim().equals(products[i].getPname().trim())){return i;}}return -1;}//排序public static void order(Product [] products,String str){if(null==products){System.out.println("数组为空");return ;}//降序if("desc".equals(str)){for(int i=0;i<products.length-1;i++){for(int j=0;j<products.length-i-1;j++){Product temp=new Product();if(products[j].getPrice()<products[j+1].getPrice()){temp=products[j];products[j]=products[j+1];products[j+1]=temp;}}}}//升序if("asc".equals(str)){for(int i=0;i<products.length-1;i++){for(int j=0;j<products.length-i-1;j++){Product temp=new Product();if(products[j].getPrice()>products[j+1].getPrice()){temp=products[j];products[j]=products[j+1];products[j+1]=temp;}}}}}}
0 0
- oop 商品信息按商品名称查询 商品按价格排序 内含测试类
- 抓取凡客一万多商品,链接,商品名称,价格,图片~~~版本1
- angular——按名称找商品,价格排序
- 抓取淘宝某类商品名称和价格
- 用oop 写 单机剪刀石头布游戏(内含有测试类)
- 按价格排序
- 表头排序 价格区间查询
- 查询自动售货机中商品的价格
- mybatis 分页查询 内含有测试类实体类映射文件与映射接口
- 根据销量排序,获取商品的数据(价格,名称,url,商品图片)
- java 输入输出--抓取凡客一万多商品名称和价格,图片~用到的
- python抓取京东商城的商品名称和价格
- python beautifulsoup抓取京东商品名称和价格
- Python爬虫学习之抓取商品名称和价格
- 看商品猜价格
- 如何按商品所售数量排序
- ECshop商品列表页按销量排序
- 商品信息表,除了商品号,商品名,…
- 2016 多校3 1009 Palindrome Bo 回文串dp+优化
- Android静态安全检测 -> 证书弱校验
- iOS下的智能硬件开发遇到的问题总结
- HDu1166敌兵布阵(树状数组)
- SpringMVC报错The request sent by the client was syntactically incorrect ()
- oop 商品信息按商品名称查询 商品按价格排序 内含测试类
- Android Studio 使用git
- springmvc用不了jquery问题解决
- Android系统性能调优工具介绍
- 【Linux\云服务器】CentOS 7.2 64位 可视化桌面的安装与卸载
- Linux兴趣小组暑假留校第三次讲座总结
- 我眼中的Android MVP
- hdu 2814 Interesting Fibonacci
- HDU 2830 Matrix Swapping II(最大完全子矩阵之可移动列)