[笔记]类与集合练习之点名器+库存清单-06day
本帖最后由 q59541511 于 2017-2-16 01:41 编辑学生类
public class Student{
String name;
int ages;
}
import java.util.ArrayList; //集合类
import java.util.Random;//生成随机数类
public class CallNames{
public static void main(String[] args){
ArrayList<Student> array = new ArrayList<Student>();
addnames(array);
printnames(array);
PointsNames(array);
}
//创建学生数据录入集合的方法 传入集合类型 无返回值
public static void addnames(ArrayList<Student> arr){
//内存中创建6个学生
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
Student s4 = new Student();
Student s5 = new Student();
Student s6 = new Student();
//对学生进行赋值初始化
s1.name = "张三";
s1.ages = 15;
s2.name = "小花";
s2.ages = 17;
s3.name = "王二麻";
s3.ages = 18;
s4.name = "哈哈";
s4.ages = 21;
s5.name = "小狗";
s5.ages = 31;
s6.name = "瞎猫";
s6.ages = 41;
//将学生放进集合内
arr.add(s1);
arr.add(s2);
arr.add(s3);
arr.add(s4);
arr.add(s5);
arr.add(s6);
}
//从集合中遍历学生显示所有学生 传入集合类型 无返回值
public static void printnames(ArrayList<Student> arr){
for(int i = 0;i < arr.size();i++){
//将集合中的学生转化为学生类型,从而显示学生信息
Student s = arr.get(i);
System.out.println(s.name+" "+s.ages);
}
}
//生成随机数进行学生点名 传入集合类型 有返回值String
public static void PointsNames(ArrayList<Student> arr){
Random r = new Random();
Student s = arr.get(r.nextInt(arr.size()));
System.out.println("点名如下\r 姓名:"+s.name+" 年龄:"+s.ages);
}
} 这个是昨天库存升级版运用的ArrayList类
Goods类
public class Goods{
String name ;
double price ;
int count ;
}
main类
import java.util.ArrayList;
import java.util.Scanner;
public class Shop{
public static void main(String[] args){
ArrayList<Goods> array = new ArrayList<Goods>();
addGoods(array);
while(true){
switch(choose()){
case 1:
printgoods(array);
break;
case 2:
changeconut(array);
break;
case 3:
return;
default:
System.out.println("您输入的数字不正确哦");
}
}
}
//选择方法
public static int choose(){
System.out.println("1.查看所有库存清单");
System.out.println("2.修改库存清单");
System.out.println("3.退出");
System.out.println("请输入相对应的数字进行选择");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
return number;
}
//创建商品方法
public static void addGoods(ArrayList<Goods> arr){
//实例化商品
Goods G1 = new Goods();
Goods G2 = new Goods();
Goods G3 = new Goods();
Goods G4 = new Goods();
//对商品进行赋值
G1.name = "芬达";
G1.price = 3.3;
G1.count = 0;
G2.name = "可乐";
G2.price = 3.5;
G2.count = 0;
G3.name = "脉动";
G3.price = 4;
G3.count = 0;
G4.name = "中沃";
G4.price = 5;
G4.count = 0;
//将商品录入数组中
arr.add(G1);
arr.add(G2);
arr.add(G3);
arr.add(G4);
}
//循环遍历集合 并算出总价总数
public static void printgoods(ArrayList<Goods> arr){
System.out.println("---------商品如下--------");
System.out.println("商品名称 "+"商品价格 "+"商品库存");
double sumprice = 0;
int sumgoods = 0;
for(int i = 0;i < arr.size();i++){
Goods g = arr.get(i);
System.out.println(g.name+" "+g.price+" "+g.count);
sumprice += g.price * g.count;
sumgoods += g.count;
}
System.out.println("-----------------------\n商品总价为:"+sumprice+"\n商品总数量为"+sumgoods);
}
//修改库存方法
public static void changeconut(ArrayList<Goods> arr){
Scanner sc = new Scanner(System.in);
for(int i = 0 ;i < arr.size();i++){
Goods G = arr.get(i);
System.out.println("请您修改商品"+G.name+"的数量");
G.count = sc.nextInt();
}
}
} {:301_1009:}用array的自带方法啊 貌似很高深的样子! 刚学到数组。三天,刚到数组,学的有点细了,每个东西都在琢磨好半天,然后再去啃经典例题,速度甚是不能看。
页:
[1]